Merge "[Fix] Use localtime_r() instead of localtime()" into tizen accepted/tizen/unified/20181120.162734 submit/tizen/20181120.053838
authorJaehyun Kim <jeik01.kim@samsung.com>
Tue, 20 Nov 2018 05:10:33 +0000 (05:10 +0000)
committerGerrit Code Review <gerrit@review.ap-northeast-2.compute.internal>
Tue, 20 Nov 2018 05:10:33 +0000 (05:10 +0000)
53 files changed:
data/exceptions
include/stc-manager.h
packaging/stc-manager.spec
plugin/appstatus/stc-plugin-appstatus.c
plugin/exception/stc-plugin-exception.c
plugin/procfs/include/stc-plugin-procfs.h
plugin/procfs/stc-plugin-procfs.c [changed mode: 0644->0755]
plugin/tether/stc-plugin-tether.c
src/database/db-common.c
src/database/include/table-restrictions.h
src/database/tables/table-counters.c
src/database/tables/table-firewall.c [changed mode: 0755->0644]
src/database/tables/table-restrictions.c
src/database/tables/table-statistics.c
src/helper/helper-cgroup.c [changed mode: 0755->0644]
src/helper/helper-file.c
src/helper/helper-file.h [changed mode: 0755->0644]
src/helper/helper-firewall.c [changed mode: 0755->0644]
src/helper/helper-firewall.h [changed mode: 0755->0644]
src/helper/helper-iptables.c
src/helper/helper-iptables.h
src/helper/helper-net-cls.c
src/helper/helper-nfacct-rule.c [changed mode: 0644->0755]
src/helper/helper-nfacct-rule.h
src/monitor/include/stc-connection.h [moved from src/monitor/include/stc-default-connection.h with 77% similarity]
src/monitor/include/stc-monitor-app.h [new file with mode: 0644]
src/monitor/include/stc-monitor-context.h [new file with mode: 0644]
src/monitor/include/stc-monitor-ipt.h [new file with mode: 0644]
src/monitor/include/stc-monitor-proc.h [new file with mode: 0644]
src/monitor/include/stc-monitor-rstn.h [new file with mode: 0644]
src/monitor/include/stc-monitor.h [changed mode: 0755->0644]
src/monitor/stc-connection.c [moved from src/monitor/stc-default-connection.c with 62% similarity]
src/monitor/stc-monitor-app.c [new file with mode: 0644]
src/monitor/stc-monitor-ipt.c [new file with mode: 0644]
src/monitor/stc-monitor-proc.c [new file with mode: 0644]
src/monitor/stc-monitor-rstn.c [new file with mode: 0644]
src/monitor/stc-monitor.c [changed mode: 0755->0644]
src/stc-firewall.c [changed mode: 0755->0644]
src/stc-manager-gdbus.c
src/stc-manager-util.c
src/stc-manager.c [changed mode: 0755->0644]
src/stc-restriction.c
unittest/CMakeLists.txt
unittest/common.cpp [new file with mode: 0644]
unittest/common.h [new file with mode: 0644]
unittest/firewall.cpp [new file with mode: 0644]
unittest/firewall.h [new file with mode: 0644]
unittest/gdbus.h
unittest/restriction.cpp
unittest/restriction.h
unittest/statistics.cpp
unittest/stcmgr.h
unittest/unittest.cpp

index 178bd7e..ef89dd3 100644 (file)
@@ -49,3 +49,6 @@ sdbd:sys
 sdbd-user:sys
 pushd:sys
 dbus-daemon:sys
+dlogsend:sys
+sync:sys
+reboot:sys
index 94c57a1..d1f4d93 100644 (file)
@@ -25,7 +25,7 @@
 
 #define STC_BACKGROUND_APP_SUFFIX "_BACKGROUND"
 #define STC_TETHERING_APP_SUFFIX "_TETHERING"
-#define STC_BACKGROUND_APP_ID "BACKGROUND"
+#define STC_TOTAL_BACKGROUND "TOTAL_BACKGROUND"
 #define STC_TOTAL_DATACALL "TOTAL_DATACALL"
 #define STC_TOTAL_WIFI "TOTAL_WIFI"
 #define STC_TOTAL_BLUETOOTH "TOTAL_BLUETOOTH"
@@ -36,6 +36,7 @@
 #define STC_IPTABLES "/usr/sbin/iptables"
 #define STC_IP6TABLES "/usr/sbin/ip6tables"
 
+#define STC_CMD_INSERT "-I"
 #define STC_CMD_SIZE 256
 
 #define STC_DEBUG_LOG (stc_util_get_debuglog())
@@ -174,10 +175,8 @@ typedef enum {
  * @brief Network roaming type
  */
 typedef enum {
-       STC_ROAMING_UNKNOWN,   /**< can't define roaming - roaming unknown */
-       STC_ROAMING_ENABLE,    /**< in roaming */
        STC_ROAMING_DISABLE,   /**< not in roaming */
-       STC_ROAMING_LAST_ELEM,
+       STC_ROAMING_ENABLE,    /**< in roaming */
 } stc_roaming_type_e;
 
 /**
index b8ab1cf..9f4e7bd 100644 (file)
@@ -1,6 +1,6 @@
 Name:       stc-manager
 Summary:    STC(Smart Traffic Control) manager
-Version:    0.0.78
+Version:    0.0.81
 Release:    0
 Group:      Network & Connectivity/Other
 License:    Apache-2.0
@@ -72,6 +72,10 @@ chmod 644 %{SOURCE0}
 export CFLAGS="$CFLAGS -D_GNU_SOURCE"
 export CXXFLAGS="$CXXFLAGS -D_GNU_SOURCE"
 
+%if "%{tizen_profile_name}" == "tv"
+       CFLAGS+=" -DTIZEN_TV_EXT"
+%endif
+
 %cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} \
        -DBIN_DIR=%{_bindir} \
        -DLIB_PATH=%{_lib} \
index 672eb8c..2fd1b9e 100644 (file)
@@ -88,8 +88,8 @@ static void __stc_gdbus_handle_aul_changestate(GDBusConnection *connection,
                apptype = STC_APP_TYPE_GUI;
 
        if (STC_DEBUG_LOG) {
-               STC_LOGD("\033[1;36mAPP STATUS\033[0;m: Pkg ID [\033[0;34m%s\033[0;m], "
-                       "App ID [\033[0;32m%s\033[0;m], PID [\033[1;33m%d\033[0;m], Status [%s], Type [%s]",
+               STC_LOGD("\033[1;34mAPP STATUS\033[0;m: PkgID[\033[0;34m%s\033[0;m] "
+                       "AppID[\033[0;32m%s\033[0;m] PID[\033[1;33m%d\033[0;m] Status[%s] Type[%s]",
                        pkgid, appid, pid, statstr, pkgtype);
        }
 
index db22b2f..4769c3e 100644 (file)
@@ -99,8 +99,9 @@ static void __excn_hash_foreach_print(gpointer key, gpointer value,
        const char *process_name = key;
        const char *exe_type = value;
 
-       STC_LOGI("excn info => process_name [%s] exe_type [%s]",
-               process_name, exe_type);
+       if (STC_DEBUG_LOG)
+               STC_LOGI("Process_name[%s] exe_type[%s]",
+                       process_name, exe_type);
 }
 
 static void __excn_hash_printall(void)
index 7375041..d548c1f 100644 (file)
@@ -20,6 +20,7 @@
 #include <glib.h>
 #include "stc-error.h"
 #include "stc-manager.h"
+#include "stc-monitor.h"
 
 typedef struct {
        int (*initialize_plugin) (void);
@@ -34,6 +35,9 @@ int stc_plugin_procfs_deinitialize(void);
 
 int stc_plugin_procfs_load(void);
 stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd,
-                               pid_t pid, const gchar *app_id, const gchar *pkg_id, stc_app_type_e app_type);
+                                               pid_t pid,
+                                               const gchar *app_id,
+                                               const gchar *pkg_id,
+                                               stc_app_type_e app_type);
 
 #endif /* __STC_PLUGIN_PROCFS_H__ */
old mode 100644 (file)
new mode 100755 (executable)
index 82b301c..4aa36c6
@@ -29,6 +29,7 @@
 
 #include "stc-plugin-procfs.h"
 #include "stc-monitor.h"
+#include "helper-net-cls.h"
 #include "helper-procfs.h"
 
 //LCOV_EXCL_START
@@ -61,7 +62,6 @@ static int nl_connector_sock = -1;
 static guint nl_connector_gsource_id = 0;
 static GTree *proc_tree;
 
-
 static gboolean __process_nl_connector_message(GIOChannel *source,
                                               GIOCondition condition,
                                               gpointer user_data);
@@ -109,7 +109,7 @@ static gboolean __proc_tree_foreach_print(gpointer key, gpointer value,
        proc_key_s *proc_key = (proc_key_s *)key;
        proc_value_s *proc_value = (proc_value_s *)value;
 
-       STC_LOGD("Proc pid [\033[1;33m%d\033[0;m] ppid [\033[1;35m%s\033[0;m] "
+       STC_LOGD("Proc pid [\033[1;33m%d\033[0;m] ppid [%s] "
                "cmdline [\033[0;34m%s\033[0;m]", proc_key->pid,
                proc_value->status[PROC_STATUS_PPID], proc_value->cmdline);
 
@@ -137,8 +137,8 @@ static proc_value_s * __proc_tree_find_parent(proc_value_s *value)
 
        if (STC_DEBUG_LOG) {
                if (parent != NULL)
-                       STC_LOGD("\033[0;35mPARENT\033[0;m: tgid[\033[1;33m%s\033[0;m] pid[%s] "
-                               "ppid[\033[1;35m%s\033[0;m] cmdline[\033[0;34m%s\033[0;m] name[%s]",
+                       STC_LOGD("\033[0;35mPARENT\033[0;m: tgid[\033[1;33m%s\033[0;m] "
+                               "pid[%s] ppid[%s] cmdline[\033[0;34m%s\033[0;m] name[%s]",
                                parent->status[PROC_STATUS_TGID], parent->status[PROC_STATUS_PID],
                                parent->status[PROC_STATUS_PPID], parent->cmdline,
                                parent->status[PROC_STATUS_NAME]);
@@ -198,15 +198,16 @@ static void __proc_tree_add(proc_key_s *key, proc_value_s *value)
                        proc_value->cmdline, proc_value->cmdline, STC_APP_TYPE_SERVICE);
 }
 
-static void __proc_tree_remove(const proc_key_s *key)
+static void __proc_tree_remove(const proc_key_s *key,
+                                               const proc_value_s *value)
 {
        if (proc_tree == NULL) {
                STC_LOGE("tree is null");
                return;
        }
 
-       stc_plugin_procfs_status_changed(STC_CMD_SET_TERMINATED, key->pid, NULL,
-                                      NULL, STC_APP_TYPE_NONE);
+       stc_plugin_procfs_status_changed(STC_CMD_SET_TERMINATED, key->pid,
+                       value->cmdline, value->cmdline, STC_APP_TYPE_NONE);
 
        g_tree_remove(proc_tree, key);
 
@@ -302,11 +303,8 @@ static void __process_event_fork(int tgid, int pid)
        if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) &&
            STC_ERROR_NONE == proc_get_status(pid, status)) {
 
-               if (__check_excn(cmdline)) {
-                       if (STC_DEBUG_LOG)
-                               STC_LOGD("[%s] monitoring is excepted", cmdline);
+               if (__check_excn(cmdline))
                        return;
-               }
 
                unsigned int i;
                proc_key_s key;
@@ -321,8 +319,9 @@ static void __process_event_fork(int tgid, int pid)
                g_strlcpy(value.cmdline, cmdline, sizeof(value.cmdline));
 
                if (STC_DEBUG_LOG)
-                       STC_LOGD("\033[1;34mFORK\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] "
-                               "cmdline[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], cmdline, pid);
+                       STC_LOGD("\033[1;32mFORK\033[0;m: tgid[\033[1;33m%d\033[0;m] "
+                       "ppid=[%s] cmdline[\033[0;34m%s\033[0;m] pid[%d]",
+                       tgid, status[PROC_STATUS_PPID], cmdline, pid);
 
                __proc_tree_add(&key, &value);
        }
@@ -342,11 +341,8 @@ static void __process_event_exec(int tgid, int pid)
        if (STC_ERROR_NONE == proc_get_cmdline(pid, cmdline) &&
            STC_ERROR_NONE == proc_get_status(pid, status)) {
 
-               if (__check_excn(cmdline)) {
-                       if (STC_DEBUG_LOG)
-                               STC_LOGD("[%s] monitoring is excepted", cmdline);
+               if (__check_excn(cmdline))
                        return;
-               }
 
                unsigned int i;
                proc_key_s key;
@@ -362,8 +358,9 @@ static void __process_event_exec(int tgid, int pid)
                g_strlcpy(value.cmdline, cmdline, sizeof(value.cmdline));
 
                if (STC_DEBUG_LOG)
-                       STC_LOGD("\033[1;32mEXEC\033[0;m: tgid[\033[1;33m%d\033[0;m] ppid=[\033[1;35m%s\033[0;m] "
-                               "cmdline[\033[0;34m%s\033[0;m] pid[%d]", tgid, status[PROC_STATUS_PPID], cmdline, pid);
+                       STC_LOGD("\033[1;32mEXEC\033[0;m: tgid[\033[1;33m%d\033[0;m] "
+                       "ppid[%s] cmdline[\033[0;34m%s\033[0;m] pid[%d]",
+                       tgid, status[PROC_STATUS_PPID], cmdline, pid);
 
                __proc_tree_add(&key, &value);
        }
@@ -384,9 +381,10 @@ static void __process_event_exit(int tgid, int pid, int exit_code)
 
        if (STC_DEBUG_LOG)
                STC_LOGD("\033[1;31mEXIT\033[0;m: tgid[\033[1;33m%d\033[0;m] "
-                       "pid[%d] exitcode[\033[0;31m%d\033[0;m]", tgid, pid, exit_code);
+                       "cmdline[\033[0;34m%s\033[0;m] pid[%d] exitcode[%d]",
+                       tgid, lookup->cmdline, pid, exit_code);
 
-       __proc_tree_remove(&key);
+       __proc_tree_remove(&key, lookup);
 }
 
 static gboolean __process_nl_connector_message(GIOChannel *source,
@@ -433,7 +431,7 @@ static gboolean __process_nl_connector_message(GIOChannel *source,
                                     msg.proc_ev.event_data.exit.exit_code);
                break;
        default:
-               ; /* Do nothing */
+               break;
        }
 
        return TRUE;
@@ -585,115 +583,140 @@ stc_error_e stc_plugin_procfs_load(void)
        return STC_ERROR_NONE;
 }
 
-stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd, pid_t pid,
-                                            const gchar *app_id,
-                                            const gchar *pkg_id,
-                                            stc_app_type_e app_type)
+stc_error_e stc_plugin_procfs_status_changed(stc_cmd_type_e cmd,
+                                               pid_t pid,
+                                               const gchar *app_id,
+                                               const gchar *pkg_id,
+                                               stc_app_type_e app_type)
 {
        stc_error_e ret = STC_ERROR_NONE;
 
        if ((pkg_id && app_id) && STC_DEBUG_LOG)
-               STC_LOGD("cmd [%d] pkgid [%s] appid [%s] pid[%d] type [%d]",
+               STC_LOGD("cmd[%d] pkgid[%s] appid[%s] pid[%d] type[%d]",
                        cmd, pkg_id, app_id, pid, app_type);
 
        switch (cmd) {
        case STC_CMD_SET_FOREGRD:
        {
-               stc_app_key_s app_key;
+               uint32_t fg_classid;
+               uint32_t bg_classid;
+               char *bg_app_id;
                stc_app_value_s app_value;
-               stc_process_key_s proc_key;
-               stc_process_value_s proc_value;
+               stc_proc_value_s proc_value;
 
-               memset(&app_key, 0, sizeof(stc_app_key_s));
                memset(&app_value, 0, sizeof(stc_app_value_s));
-               memset(&proc_key, 0, sizeof(stc_process_key_s));
-               memset(&proc_value, 0, sizeof(stc_process_value_s));
+               memset(&proc_value, 0, sizeof(stc_proc_value_s));
 
-               app_key.pkg_id = g_strdup(pkg_id);
-               app_key.app_id = g_strdup(app_id);
+               bg_app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, NULL);
 
                app_value.type = app_type;
+               app_value.state = STC_APP_STATE_FOREGROUND;
                app_value.processes = NULL;
 
-               proc_key.pid = pid;
-
+               proc_value.pid = pid;
                proc_value.ground = STC_APP_STATE_FOREGROUND;
 
-               stc_monitor_application_add(app_key, app_value);
-               stc_monitor_process_add(app_key, proc_key, proc_value);
-               stc_monitor_process_update_ground(app_key, proc_key,
-                                                 STC_APP_STATE_FOREGROUND);
+               bg_classid = get_classid_by_app_id(bg_app_id, FALSE);
+               fg_classid = get_classid_by_app_id(app_id, TRUE);
+
+               stc_monitor_app_add(fg_classid, app_id, pkg_id, app_value);
+
+               stc_monitor_proc_move(bg_classid, fg_classid);
 
-               FREE(app_key.pkg_id);
-               FREE(app_key.app_id);
+               stc_monitor_proc_add(fg_classid, app_id, proc_value);
+               stc_monitor_proc_update_ground(fg_classid, app_id, proc_value);
+
+               FREE(bg_app_id);
                break;
        }
        case STC_CMD_SET_BACKGRD:
        {
-               stc_app_key_s app_key;
+               uint32_t bg_classid;
+               uint32_t fg_classid;
+               char *bg_app_id;
                stc_app_value_s app_value;
-               stc_process_key_s proc_key;
-               stc_process_value_s proc_value;
+               stc_proc_value_s proc_value;
 
-               memset(&app_key, 0, sizeof(stc_app_key_s));
                memset(&app_value, 0, sizeof(stc_app_value_s));
-               memset(&proc_key, 0, sizeof(stc_process_key_s));
-               memset(&proc_value, 0, sizeof(stc_process_value_s));
+               memset(&proc_value, 0, sizeof(stc_proc_value_s));
 
-               app_key.pkg_id = g_strdup(pkg_id);
-               app_key.app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX,
-                                            NULL);
+               bg_app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, NULL);
 
                app_value.type = app_type;
+               app_value.state = STC_APP_STATE_BACKGROUND;
                app_value.processes = NULL;
 
-               proc_key.pid = pid;
-
+               proc_value.pid = pid;
                proc_value.ground = STC_APP_STATE_BACKGROUND;
 
-               stc_monitor_application_add(app_key, app_value);
-               stc_monitor_process_add(app_key, proc_key, proc_value);
-               stc_monitor_process_update_ground(app_key, proc_key,
-                                                 STC_APP_STATE_BACKGROUND);
+               fg_classid = get_classid_by_app_id(app_id, FALSE);
+               bg_classid = get_classid_by_app_id(bg_app_id, TRUE);
+
+               stc_monitor_app_add(bg_classid, bg_app_id, pkg_id, app_value);
 
-               FREE(app_key.pkg_id);
-               FREE(app_key.app_id);
+               stc_monitor_proc_move(fg_classid, bg_classid);
+
+               stc_monitor_proc_add(bg_classid, bg_app_id, proc_value);
+               stc_monitor_proc_update_ground(bg_classid, bg_app_id, proc_value);
+
+               FREE(bg_app_id);
                break;
        }
        case STC_CMD_SET_SERVICE_LAUNCHED:
        {
-               stc_app_key_s app_key;
+               uint32_t classid;
+               char *bg_app_id;
                stc_app_value_s app_value;
-               stc_process_key_s proc_key;
-               stc_process_value_s proc_value;
+               stc_proc_value_s proc_value;
+               gboolean is_exist;
 
-               memset(&app_key, 0, sizeof(stc_app_key_s));
                memset(&app_value, 0, sizeof(stc_app_value_s));
-               memset(&proc_key, 0, sizeof(stc_process_key_s));
-               memset(&proc_value, 0, sizeof(stc_process_value_s));
+               memset(&proc_value, 0, sizeof(stc_proc_value_s));
 
-               app_key.pkg_id = g_strdup(pkg_id);
-               app_key.app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX,
-                                            NULL);
+               classid = get_classid_by_app_id(app_id, FALSE);
+               is_exist = stc_monitor_app_lookup(classid);
+               if (is_exist) {
+                       app_value.type = app_type;
+                       app_value.state = STC_APP_STATE_FOREGROUND;
+                       app_value.processes = NULL;
 
-               app_value.type = app_type;
-               app_value.processes = NULL;
+                       proc_value.pid = pid;
+                       proc_value.ground = STC_APP_STATE_FOREGROUND;
 
-               proc_key.pid = pid;
+                       stc_monitor_app_add(classid, app_id, pkg_id, app_value);
+                       stc_monitor_proc_add(classid, app_id, proc_value);
+               } else {
+                       bg_app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, NULL);
+                       classid = get_classid_by_app_id(bg_app_id, TRUE);
 
-               /* services will run always in background. */
-               proc_value.ground = STC_APP_STATE_BACKGROUND;
+                       app_value.type = app_type;
+                       app_value.state = STC_APP_STATE_BACKGROUND;
+                       app_value.processes = NULL;
+
+                       proc_value.pid = pid;
+                       proc_value.ground = STC_APP_STATE_BACKGROUND;
 
-               stc_monitor_application_add(app_key, app_value);
-               stc_monitor_process_add(app_key, proc_key, proc_value);
+                       stc_monitor_app_add(classid, bg_app_id, pkg_id, app_value);
+                       stc_monitor_proc_add(classid, bg_app_id, proc_value);
 
-               FREE(app_key.pkg_id);
-               g_free(app_key.app_id);
+                       FREE(bg_app_id);
+               }
                break;
        }
        case STC_CMD_SET_TERMINATED:
        {
-               stc_monitor_process_remove(pid);
+               uint32_t classid;
+               char *bg_app_id;
+
+               bg_app_id = g_strconcat(app_id, STC_BACKGROUND_APP_SUFFIX, NULL);
+               classid = get_classid_by_app_id(bg_app_id, FALSE);
+
+               if (classid == STC_UNKNOWN_CLASSID)
+                       classid = get_classid_by_app_id(app_id, FALSE);
+
+               stc_monitor_proc_remove(classid, pid);
+
+               FREE(bg_app_id);
                break;
        }
        default:
index c335f05..5270aed 100755 (executable)
@@ -35,7 +35,6 @@ static stc_error_e add_station_monitor(gchar *pkg_id, gchar *app_id,
                         const char *mac)
 {
        int ret;
-       stc_app_key_s app_key;
        stc_app_value_s app_value;
 
        if (pkg_id == NULL || app_id == NULL || mac == NULL) {
@@ -43,37 +42,34 @@ static stc_error_e add_station_monitor(gchar *pkg_id, gchar *app_id,
                return STC_ERROR_INVALID_PARAMETER;
        }
 
-       memset(&app_key, 0, sizeof(stc_app_key_s));
        memset(&app_value, 0, sizeof(stc_app_value_s));
-       app_key.pkg_id = g_strdup(pkg_id);
-       app_key.app_id = g_strconcat(app_id, STC_TETHERING_APP_SUFFIX, NULL);
+       app_value.pkg_id = g_strdup(pkg_id);
+       app_value.app_id = g_strconcat(app_id, STC_TETHERING_APP_SUFFIX, NULL);
        app_value.type = STC_APP_TYPE_TETHERING;
        app_value.processes = NULL;
        g_strlcpy(app_value.mac, mac, STATION_MAC_STR_LEN);
 
-       ret = stc_monitor_application_add(app_key, app_value);
-       FREE(app_key.pkg_id);
-       FREE(app_key.app_id);
+       ret = stc_monitor_app_add(STC_UNKNOWN_CLASSID, app_id, pkg_id, app_value);
+       FREE(app_value.pkg_id);
+       FREE(app_value.app_id);
        return ret;
 }
 
 static stc_error_e remove_station_monitor(gchar *pkg_id, gchar *app_id)
 {
        int ret;
-       stc_app_key_s app_key;
+       char *sta_app_id;
 
        if (pkg_id == NULL || app_id == NULL) {
                STC_LOGE("invalid station station info");
                return STC_ERROR_INVALID_PARAMETER;
        }
 
-       memset(&app_key, 0, sizeof(stc_app_key_s));
-       app_key.pkg_id = g_strdup(pkg_id);
-       app_key.app_id = g_strconcat(app_id, STC_TETHERING_APP_SUFFIX, NULL);
+       sta_app_id = g_strconcat(app_id, STC_TETHERING_APP_SUFFIX, NULL);
 
-       ret = stc_monitor_application_remove(app_key);
-       FREE(app_key.pkg_id);
-       FREE(app_key.app_id);
+       ret = stc_monitor_app_remove(STC_UNKNOWN_CLASSID, sta_app_id);
+
+       FREE(sta_app_id);
        return ret;
 }
 
index a4c2a3d..372eb03 100644 (file)
@@ -109,9 +109,11 @@ stc_error_e stc_db_initialize(void)
 
        stc_db_initialize_once();
 
+#ifndef TIZEN_TV_EXT
        EXEC(STC_ERROR_NONE, table_statistics_prepare(database));
        EXEC(STC_ERROR_NONE, table_restrictions_prepare(database));
        EXEC(STC_ERROR_NONE, table_counters_prepare(database));
+#endif
        EXEC(STC_ERROR_NONE, table_firewall_prepare(database));
        EXEC(STC_ERROR_NONE, stc_init_db_guard());
 
@@ -132,9 +134,11 @@ gboolean stc_db_deinitialize(void)
                return TRUE; //LCOV_EXCL_LINE
        }
 
+#ifndef TIZEN_TV_EXT
        table_statistics_finalize();
        table_restrictions_finalize();
        table_counters_finalize();
+#endif
        table_firewall_finalize();
        sqlite3_close(database);
 
index fb62bb9..e9d46bf 100644 (file)
@@ -59,7 +59,9 @@ stc_error_e table_restrictions_update(table_restrictions_info *info);
 
 stc_error_e table_restrictions_delete(const char *app_id,
                                      const stc_iface_type_e iftype,
-                                     const char *subscriber_id);
+                                     const char *ifname,
+                                     const char *subscriber_id,
+                                     const stc_roaming_type_e roaming);
 
 stc_error_e table_restrictions_prepare(sqlite3 *db);
 
index 9aec545..9bd49a2 100644 (file)
@@ -299,8 +299,11 @@ stc_error_e table_counters_get(uint64_t restriction_id,
                        info->weekly_counter = sqlite3_column_int64(stmt, 3);
                        info->daily_counter = sqlite3_column_int64(stmt, 4);
 
-                       STC_LOGD("rstn_id [%llu] data_counters [%lld] bytes",
-                                restriction_id, info->data_counter);
+                       STC_LOGD("rstn_id[%llu] data[%lld] warn[%lld] "
+                               "monthly[%lld] weekly[%lld] daily[%lld]",
+                                restriction_id, info->data_counter,
+                                info->warn_counter, info->monthly_counter,
+                                info->weekly_counter, info->daily_counter);
                        break;
                case SQLITE_ERROR:
                default:
old mode 100755 (executable)
new mode 100644 (file)
index bbbede0..929449d 100644 (file)
@@ -26,7 +26,8 @@
 
 /* DELETE statements */
 #define DELETE_RESTRICTIONS "DELETE FROM restrictions " \
-       " WHERE binpath = ? AND iftype = ? AND subscriber_id = ?"
+       " WHERE binpath = ? AND iftype = ? AND ifname = ? " \
+       " AND subscriber_id = ? AND roaming = ?"
 
 /* SELECT statements */
 #define SELECT_RESTRICTIONS "SELECT binpath, data_limit, " \
@@ -444,19 +445,24 @@ stc_error_e table_restrictions_get_restriction_type(const char *app_id,
 
 stc_error_e table_restrictions_delete(const char *app_id,
                                      const stc_iface_type_e iftype,
-                                     const char *subscriber_id)
+                                     const char *ifname,
+                                     const char *subscriber_id,
+                                     const stc_roaming_type_e roaming)
 {
        stc_error_e error_code = STC_ERROR_NONE;
        sqlite3_stmt *stmt = delete_restrictions;
 
-       STC_LOGD("app_id [%s], iftype [%d], subscriber_id [%s]",
-                app_id, iftype, subscriber_id);
+       STC_LOGD("app_id[%s] iftype[%d] ifname[%s] subscriber_id[%s] roaming[%d]",
+                app_id, iftype, ifname, subscriber_id, roaming);
 
        DB_ACTION(sqlite3_bind_text(stmt, 1, app_id ? app_id : "",
                                    -1, SQLITE_TRANSIENT));
        DB_ACTION(sqlite3_bind_int(stmt, 2, iftype));
-       DB_ACTION(sqlite3_bind_text(stmt, 3, subscriber_id ? subscriber_id : "", -1,
-                                   SQLITE_TRANSIENT));
+       DB_ACTION(sqlite3_bind_text(stmt, 3, ifname ? ifname : "",
+                                   -1, SQLITE_TRANSIENT));
+       DB_ACTION(sqlite3_bind_text(stmt, 4, subscriber_id ? subscriber_id : "",
+                                       -1, SQLITE_TRANSIENT));
+       DB_ACTION(sqlite3_bind_int(stmt, 5, roaming));
 
        if (sqlite3_step(stmt) != SQLITE_DONE) {
                STC_LOGE("Failed to remove restrictions by network interface %s\n", //LCOV_EXCL_LINE
index 6355f60..5983ad7 100644 (file)
@@ -327,8 +327,6 @@ static void __finalize_insert(void)
 
 static int __is_iftype_defined(const stc_iface_type_e iftype)
 {
-       __STC_LOG_FUNC_ENTER__;
-       __STC_LOG_FUNC_EXIT__;
        return iftype < STC_IFACE_LAST_ELEM &&
                iftype > STC_IFACE_UNKNOWN &&
                iftype != STC_IFACE_ALL;
@@ -352,11 +350,9 @@ static sqlite3_stmt **details_stms[] = {
 static sqlite3_stmt *__select_statement(const char *app_id,
                                        const table_statistics_select_rule *rule)
 {
-       __STC_LOG_FUNC_ENTER__;
        const int stm_index = __is_iftype_defined(rule->iftype) |
                ((strlen(app_id) > 0) ? 0 : 2) | (rule->granularity ? 4 : 0);
        STC_LOGD("stm index %d", stm_index);
-       __STC_LOG_FUNC_EXIT__;
        return *details_stms[stm_index];
 }
 
@@ -488,15 +484,12 @@ stc_error_e table_statistics_foreach_app(const table_statistics_select_rule *rul
 
                        if (info_cb(&data, user_data) == STC_CANCEL)
                                rc = SQLITE_DONE; //LCOV_EXCL_LINE
-                       __STC_LOG_FUNC_EXIT__;
                        break;
                case SQLITE_DONE:
-                       __STC_LOG_FUNC_EXIT__;
                        break;
                case SQLITE_ERROR:
                default:
                        error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
-                       __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                        break; //LCOV_EXCL_LINE
                }
        } while (rc == SQLITE_ROW);
@@ -569,15 +562,12 @@ stc_error_e table_statistics_per_app(const char *app_id,
 
                        if (info_cb(&data, user_data) == STC_CANCEL)
                                rc = SQLITE_DONE; //LCOV_EXCL_LINE
-                       __STC_LOG_FUNC_EXIT__;
                        break;
                case SQLITE_DONE:
-                       __STC_LOG_FUNC_EXIT__;
                        break;
                case SQLITE_ERROR:
                default:
                        error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
-                       __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                        break; //LCOV_EXCL_LINE
                }
        } while (rc == SQLITE_ROW);
@@ -595,6 +585,8 @@ stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key,
        stc_error_e error_code = STC_ERROR_NONE;
        sqlite3_stmt *stmt = update_statistics_query;
        stc_hw_net_protocol_type_e hw_net_protocol_type = STC_PROTOCOL_UNKNOWN;
+       int64_t rcv;
+       int64_t snd;
 
        if (!stat->rcv_count && !stat->snd_count) {
                error_code = STC_ERROR_INVALID_PARAMETER;
@@ -609,14 +601,16 @@ stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key,
        DB_ACTION(sqlite3_bind_int(stmt, 5, (int)(stat_key->iftype)));
        DB_ACTION(sqlite3_bind_int(stmt, 6, (int)(stat->is_roaming)));
        DB_ACTION(sqlite3_bind_int(stmt, 7, (int)hw_net_protocol_type));
-       DB_ACTION(sqlite3_bind_text(stmt, 8, stat_key->ifname, -1,
-                                   SQLITE_STATIC));
+       DB_ACTION(sqlite3_bind_text(stmt, 8, stat_key->ifname ? stat_key->ifname : "",
+                                       -1, SQLITE_STATIC));
        DB_ACTION(sqlite3_bind_text(stmt, 9,
-                                   stat_key->subscriber_id ? stat_key->subscriber_id : "" , -1,
-                                   SQLITE_STATIC));
+                                   stat_key->subscriber_id ? stat_key->subscriber_id : "" ,
+                                   -1, SQLITE_STATIC));
        DB_ACTION(sqlite3_bind_int(stmt, 10, (int)stat->ground));
 
        /*we want to reuse tree*/
+       rcv = stat->rcv_count;
+       snd = stat->snd_count;
        stat->rcv_count = 0;
        stat->snd_count = 0;
        if (sqlite3_step(stmt) != SQLITE_DONE) {
@@ -628,7 +622,8 @@ stc_error_e table_statistics_insert(stc_db_classid_iftype_key *stat_key,
        }
 
        if (STC_DEBUG_LOG)
-               STC_LOGD("App stat recorded [%s]", stat->app_id);
+               STC_LOGD("App stat recorded [\033[0;34m%s\033[0;m] "
+                       "rcv[%lld] snd[%lld]", stat->app_id, rcv, snd);
 
 handle_error:
        sqlite3_reset(stmt);
old mode 100755 (executable)
new mode 100644 (file)
index 73b93bd..636ef5d 100755 (executable)
@@ -24,17 +24,14 @@ int fwrite_str(const char *path, const char *str)
        _cleanup_fclose_ FILE *f = NULL;
        int ret;
        char * t;
-       struct stat stat_buf;
 
        assert(path);
        assert(str);
 
-       if (stat(path, &stat_buf) == 0) {
-               t = realpath(path, NULL);
-               ret_value_errno_msg_if(!t, -errno,
-                                      "Fail to get realpath %s", path);
-               free(t);
-       }
+       t = realpath(path, NULL);
+       ret_value_errno_msg_if(!t, -errno,
+                              "Fail to get realpath %s", path);
+       free(t);
 
        f = fopen(path, "w");
        ret_value_errno_msg_if(!f, -errno,
old mode 100755 (executable)
new mode 100644 (file)
old mode 100755 (executable)
new mode 100644 (file)
old mode 100755 (executable)
new mode 100644 (file)
index 445b627..9a2cde8 100644 (file)
@@ -397,6 +397,454 @@ static int __iptables_add_chain_jump_rule(const char *chain,
        return ret;
 }
 
+static stc_error_e _iptables_add_in_chain(stc_s *stc)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+
+       ret = __iptables_add_chain(stc->connection, STC_IN_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain(stc->connection, STC_IN_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain(stc->connection, STC_IN_FG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain(stc->connection, STC_IN_ACCEPT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain(stc->connection, STC_IN_BG_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain(stc->connection, STC_IN_BG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+done:
+       return ret;
+}
+
+static stc_error_e _iptables_add_out_chain(stc_s *stc)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+
+       ret = __iptables_add_chain(stc->connection, STC_OUT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain(stc->connection, STC_OUT_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain(stc->connection, STC_OUT_FG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain(stc->connection, STC_OUT_ACCEPT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain(stc->connection, STC_OUT_BG_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain(stc->connection, STC_OUT_BG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+done:
+       return ret;
+}
+
+static stc_error_e _ip6tables_add_in_chain(stc_s *stc)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+
+       ret = __ip6tables_add_chain(stc->connection, STC_IN_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_add_chain(stc->connection, STC_IN_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_add_chain(stc->connection, STC_IN_FG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_add_chain(stc->connection, STC_IN_ACCEPT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_add_chain(stc->connection, STC_IN_BG_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_add_chain(stc->connection, STC_IN_BG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+done:
+       return ret;
+}
+
+static stc_error_e _ip6tables_add_out_chain(stc_s *stc)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+
+       ret = __ip6tables_add_chain(stc->connection, STC_OUT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_add_chain(stc->connection, STC_OUT_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_add_chain(stc->connection, STC_OUT_FG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_add_chain(stc->connection, STC_OUT_ACCEPT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_add_chain(stc->connection, STC_OUT_BG_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_add_chain(stc->connection, STC_OUT_BG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+done:
+       return ret;
+}
+
+static stc_error_e _iptables_add_in_chain_jump_rule(void)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+
+       ret = __iptables_add_chain_jump_rule("INPUT", STC_IN_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain_jump_rule("INPUT", STC_IN_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain_jump_rule("INPUT", STC_IN_FG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain_jump_rule("INPUT", STC_IN_ACCEPT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain_jump_rule("INPUT", STC_IN_BG_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain_jump_rule("INPUT", STC_IN_BG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+done:
+       return ret;
+}
+
+static stc_error_e _iptables_add_out_chain_jump_rule(void)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+
+       ret = __iptables_add_chain_jump_rule("OUTPUT", STC_OUT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain_jump_rule("OUTPUT", STC_OUT_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain_jump_rule("OUTPUT", STC_OUT_FG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain_jump_rule("OUTPUT", STC_OUT_ACCEPT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain_jump_rule("OUTPUT", STC_OUT_BG_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_add_chain_jump_rule("OUTPUT", STC_OUT_BG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+done:
+       return ret;
+}
+
+static stc_error_e _iptables_remove_in_chain(stc_s *stc)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+
+       ret = __iptables_remove_chain(stc->connection, STC_IN_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_remove_chain(stc->connection, STC_IN_FG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_remove_chain(stc->connection, STC_IN_ACCEPT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_remove_chain(stc->connection, STC_IN_BG_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_remove_chain(stc->connection, STC_IN_BG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_remove_chain(stc->connection, STC_IN_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+done:
+       return ret;
+}
+
+static stc_error_e _iptables_remove_out_chain(stc_s *stc)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+
+       ret = __iptables_remove_chain(stc->connection, STC_OUT_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_remove_chain(stc->connection, STC_OUT_FG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_remove_chain(stc->connection, STC_OUT_ACCEPT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_remove_chain(stc->connection, STC_OUT_BG_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_remove_chain(stc->connection, STC_OUT_BG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_remove_chain(stc->connection, STC_OUT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+done:
+       return ret;
+}
+
+static stc_error_e _ip6tables_remove_in_chain(stc_s *stc)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+
+       ret = __ip6tables_remove_chain(stc->connection, STC_IN_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_remove_chain(stc->connection, STC_IN_FG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_remove_chain(stc->connection, STC_IN_ACCEPT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_remove_chain(stc->connection, STC_IN_BG_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_remove_chain(stc->connection, STC_IN_BG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_remove_chain(stc->connection, STC_IN_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+done:
+       return ret;
+}
+
+static stc_error_e _ip6tables_remove_out_chain(stc_s *stc)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+
+       ret = __ip6tables_remove_chain(stc->connection, STC_OUT_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_remove_chain(stc->connection, STC_OUT_FG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_remove_chain(stc->connection, STC_OUT_ACCEPT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_remove_chain(stc->connection, STC_OUT_BG_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_remove_chain(stc->connection, STC_OUT_BG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_remove_chain(stc->connection, STC_OUT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+done:
+       return ret;
+}
+
+static stc_error_e _iptables_flush_in_chain(stc_s *stc)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+
+       ret = __iptables_flush_chain(stc->connection, STC_IN_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_flush_chain(stc->connection, STC_IN_FG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_flush_chain(stc->connection, STC_IN_ACCEPT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_flush_chain(stc->connection, STC_IN_BG_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_flush_chain(stc->connection, STC_IN_BG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_flush_chain(stc->connection, STC_IN_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+done:
+       return ret;
+}
+
+static stc_error_e _iptables_flush_out_chain(stc_s *stc)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+
+       ret = __iptables_flush_chain(stc->connection, STC_OUT_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_flush_chain(stc->connection, STC_OUT_FG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_flush_chain(stc->connection, STC_OUT_ACCEPT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_flush_chain(stc->connection, STC_OUT_BG_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_flush_chain(stc->connection, STC_OUT_BG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __iptables_flush_chain(stc->connection, STC_OUT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+done:
+       return ret;
+}
+
+static stc_error_e _ip6tables_flush_in_chain(stc_s *stc)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+
+       ret = __ip6tables_flush_chain(stc->connection, STC_IN_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_flush_chain(stc->connection, STC_IN_FG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_flush_chain(stc->connection, STC_IN_ACCEPT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_flush_chain(stc->connection, STC_IN_BG_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_flush_chain(stc->connection, STC_IN_BG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_flush_chain(stc->connection, STC_IN_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+done:
+       return ret;
+}
+
+static stc_error_e _ip6tables_flush_out_chain(stc_s *stc)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+
+       ret = __ip6tables_flush_chain(stc->connection, STC_OUT_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_flush_chain(stc->connection, STC_OUT_FG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_flush_chain(stc->connection, STC_OUT_ACCEPT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_flush_chain(stc->connection, STC_OUT_BG_DROP_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_flush_chain(stc->connection, STC_OUT_BG_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+       ret = __ip6tables_flush_chain(stc->connection, STC_OUT_CHAIN);
+       if (ret != STC_ERROR_NONE)
+               goto done; //LCOV_EXCL_LINE
+
+done:
+       return ret;
+}
+
 stc_error_e iptables_add(iptables_rule_s *rule, iptables_ip_type_e iptype)
 {
        stc_error_e ret = STC_ERROR_NONE;
@@ -451,11 +899,11 @@ stc_error_e iptables_flush_chains(void)
        if (!stc || !stc->connection)
                return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
 
-       ret = __iptables_flush_chain(stc->connection, STC_IN_CHAIN);
+       ret = _iptables_flush_in_chain(stc);
        if (ret != STC_ERROR_NONE)
                goto done; //LCOV_EXCL_LINE
 
-       ret = __iptables_flush_chain(stc->connection, STC_OUT_CHAIN);
+       ret = _iptables_flush_out_chain(stc);
        if (ret != STC_ERROR_NONE)
                goto done; //LCOV_EXCL_LINE
 
@@ -467,11 +915,11 @@ stc_error_e iptables_flush_chains(void)
        if (ret != STC_ERROR_NONE)
                goto done; //LCOV_EXCL_LINE
 
-       ret = __ip6tables_flush_chain(stc->connection, STC_IN_CHAIN);
+       ret = _ip6tables_flush_in_chain(stc);
        if (ret != STC_ERROR_NONE)
                goto done; //LCOV_EXCL_LINE
 
-       ret = __ip6tables_flush_chain(stc->connection, STC_OUT_CHAIN);
+       ret = _ip6tables_flush_out_chain(stc);
        if (ret != STC_ERROR_NONE)
                goto done; //LCOV_EXCL_LINE
 
@@ -492,13 +940,13 @@ stc_error_e iptables_init(void)
                return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
        }
 
-       ret = __iptables_add_chain(stc->connection, STC_IN_CHAIN);
+       ret = _iptables_add_in_chain(stc);
        if (ret != STC_ERROR_NONE) {
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                goto done; //LCOV_EXCL_LINE
        }
 
-       ret = __iptables_add_chain(stc->connection, STC_OUT_CHAIN);
+       ret = _iptables_add_out_chain(stc);
        if (ret != STC_ERROR_NONE) {
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                goto done; //LCOV_EXCL_LINE
@@ -516,13 +964,13 @@ stc_error_e iptables_init(void)
                goto done; //LCOV_EXCL_LINE
        }
 
-       ret = __ip6tables_add_chain(stc->connection, STC_IN_CHAIN);
+       ret = _ip6tables_add_in_chain(stc);
        if (ret != STC_ERROR_NONE) {
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                goto done; //LCOV_EXCL_LINE
        }
 
-       ret = __ip6tables_add_chain(stc->connection, STC_OUT_CHAIN);
+       ret = _ip6tables_add_out_chain(stc);
        if (ret != STC_ERROR_NONE) {
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                goto done; //LCOV_EXCL_LINE
@@ -534,13 +982,13 @@ stc_error_e iptables_init(void)
                goto done; //LCOV_EXCL_LINE
        }
 
-       ret = __iptables_add_chain_jump_rule("INPUT", STC_IN_CHAIN);
+       ret = _iptables_add_in_chain_jump_rule();
        if (ret != STC_ERROR_NONE) {
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                goto done; //LCOV_EXCL_LINE
        }
 
-       ret = __iptables_add_chain_jump_rule("OUTPUT", STC_OUT_CHAIN);
+       ret = _iptables_add_out_chain_jump_rule();
        if (ret != STC_ERROR_NONE) {
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                goto done; //LCOV_EXCL_LINE
@@ -557,7 +1005,9 @@ stc_error_e iptables_init(void)
                __STC_LOG_FUNC_EXIT__;
                goto done;
        }
+
 done:
+       __STC_LOG_FUNC_ENTER__;
        return ret;
 }
 
@@ -573,13 +1023,13 @@ stc_error_e iptables_deinit(void)
                return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
        }
 
-       ret = __iptables_remove_chain(stc->connection, STC_IN_CHAIN);
+       ret = _iptables_remove_in_chain(stc);
        if (ret != STC_ERROR_NONE) {
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                goto done; //LCOV_EXCL_LINE
        }
 
-       ret = __iptables_remove_chain(stc->connection, STC_OUT_CHAIN);
+       ret = _iptables_remove_out_chain(stc);
        if (ret != STC_ERROR_NONE) {
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                goto done; //LCOV_EXCL_LINE
@@ -597,19 +1047,25 @@ stc_error_e iptables_deinit(void)
                goto done; //LCOV_EXCL_LINE
        }
 
-       ret = __ip6tables_remove_chain(stc->connection, STC_IN_CHAIN);
+       ret = _ip6tables_remove_in_chain(stc);
        if (ret != STC_ERROR_NONE) {
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                goto done; //LCOV_EXCL_LINE
        }
 
-       ret = __ip6tables_remove_chain(stc->connection, STC_OUT_CHAIN);
+       ret = _ip6tables_remove_out_chain(stc);
        if (ret != STC_ERROR_NONE) {
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                goto done; //LCOV_EXCL_LINE
        }
 
        ret = __ip6tables_remove_chain(stc->connection, STC_FRWD_CHAIN);
+       if (ret != STC_ERROR_NONE) {
+               __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
+               goto done; //LCOV_EXCL_LINE
+       }
+
 done:
+       __STC_LOG_FUNC_ENTER__;
        return ret;
 }
index 71b057b..6fa7908 100644 (file)
 #include "stc-manager.h"
 #include "stc-error.h"
 
-#define STC_IN_CHAIN     "STC_IN"
-#define STC_OUT_CHAIN    "STC_OUT"
+#define STC_IN_CHAIN      "STC_IN"
+#define STC_OUT_CHAIN     "STC_OUT"
 #define STC_FRWD_CHAIN    "STC_FRWD"
 #define STC_TETHER_CHAIN  "STC_TETHER"
 
+#define STC_IN_DROP_CHAIN     "STC_IN_DROP"
+#define STC_IN_FG_CHAIN       "STC_IN_FG"
+#define STC_IN_ACCEPT_CHAIN   "STC_IN_ACCEPT"
+#define STC_IN_BG_DROP_CHAIN  "STC_IN_BG_DROP"
+#define STC_IN_BG_CHAIN       "STC_IN_BG"
+
+#define STC_OUT_DROP_CHAIN    "STC_OUT_DROP"
+#define STC_OUT_FG_CHAIN      "STC_OUT_FG"
+#define STC_OUT_ACCEPT_CHAIN  "STC_OUT_ACCEPT"
+#define STC_OUT_BG_DROP_CHAIN "STC_OUT_BG_DROP"
+#define STC_OUT_BG_CHAIN      "STC_OUT_BG"
+
 typedef enum {
        IPTABLES_DIRECTION_NONE,
        IPTABLES_DIRECTION_IN,
index 238b7de..faa5942 100644 (file)
@@ -72,17 +72,20 @@ static int __place_classid_to_cgroup(const char *cgroup, const char *subdir,
        return cgroup_write_node_uint32(buf, CLASSID_FILE_NAME, result_classid);
 }
 
-static uint32_t __get_classid_from_cgroup(const char *cgroup,
-                                         const char *subdir)
+static stc_error_e __get_classid_from_cgroup(const char *cgroup,
+                                         const char *subdir, uint32_t *classid)
 {
        char buf[MAX_PATH_LENGTH];
-       uint32_t classid = STC_UNKNOWN_CLASSID;
        snprintf(buf, sizeof(buf), "%s/%s", cgroup, subdir);
 
-       int ret = cgroup_read_node_uint32(buf, CLASSID_FILE_NAME, &classid);
-       if (ret < 0)
+       int ret = cgroup_read_node_uint32(buf, CLASSID_FILE_NAME, classid);
+       if (ret < 0) {
                STC_LOGE("Can't read classid from cgroup %s", buf); //LCOV_EXCL_LINE
-       return classid;
+               *classid = STC_UNKNOWN_CLASSID;
+               return STC_ERROR_NO_DATA;
+       }
+
+       return STC_ERROR_NONE;
 }
 
 stc_error_e init_current_classid(void)
@@ -102,10 +105,10 @@ stc_error_e init_current_classid(void)
        return STC_ERROR_NONE;
 }
 
-uint32_t get_classid_by_app_id(const char *app_id, int create)
+API uint32_t get_classid_by_app_id(const char *app_id, int create)
 {
-       int ret = 0;
        bool exists;
+       int ret = STC_ERROR_NONE;
        uint32_t classid = STC_UNKNOWN_CLASSID;
        const char *path_to_net_cgroup_dir = NULL;
 
@@ -114,7 +117,7 @@ uint32_t get_classid_by_app_id(const char *app_id, int create)
                return STC_UNKNOWN_CLASSID; //LCOV_EXCL_LINE
        }
 
-       if (!strcmp(app_id, STC_BACKGROUND_APP_ID))
+       if (!strcmp(app_id, STC_TOTAL_BACKGROUND))
                return STC_BACKGROUND_APP_CLASSID;
 
        if (!strcmp(app_id, STC_TOTAL_DATACALL))
@@ -144,8 +147,11 @@ uint32_t get_classid_by_app_id(const char *app_id, int create)
 
        /* just read */
        if (!create)
-               classid = __get_classid_from_cgroup(path_to_net_cgroup_dir, //LCOV_EXCL_LINE
-                                                   app_id);
+               ret = __get_classid_from_cgroup(path_to_net_cgroup_dir, //LCOV_EXCL_LINE
+                                                       app_id, &classid);
+
+       if (ret != STC_ERROR_NONE)
+               return STC_UNKNOWN_CLASSID;
 
        if (classid != STC_UNKNOWN_CLASSID)
                return classid;
@@ -156,8 +162,8 @@ uint32_t get_classid_by_app_id(const char *app_id, int create)
                goto handle_error;
 
        if (exists)
-               classid = __get_classid_from_cgroup(path_to_net_cgroup_dir,
-                                                   app_id);
+               ret = __get_classid_from_cgroup(path_to_net_cgroup_dir,
+                                                   app_id, &classid);
        else
                ret = __place_classid_to_cgroup(path_to_net_cgroup_dir,
                                                (char *)app_id, &classid, NULL);
@@ -184,9 +190,7 @@ stc_error_e place_pids_to_net_cgroup(const int pid, const char *app_id)
                return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
        }
 
-       if (!strcmp(app_id, STC_BACKGROUND_APP_ID))
-               path_to_net_cgroup_dir = STC_CGROUP_NETWORK;
-       else if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX))
+       if (strstr(app_id, STC_BACKGROUND_APP_SUFFIX))
                path_to_net_cgroup_dir = BACKGROUND_CGROUP_NETWORK;
        else if (strstr(app_id, STC_TETHERING_APP_SUFFIX))
                path_to_net_cgroup_dir = TETHERING_CGROUP_NETWORK;
old mode 100644 (file)
new mode 100755 (executable)
index a3e4647..fbe17ca
@@ -417,14 +417,45 @@ static char *get_iptables_cmd(const nfacct_rule_action action)
        return "";
 }
 
-static char *get_iptables_chain(const nfacct_rule_direction iotype)
+static char *get_iptables_chain(uint32_t classid,
+                               const nfacct_rule_direction iotype,
+                               const stc_app_state_e app_state,
+                               const nfacct_rule_intend intend)
 {
-       if (iotype == NFACCT_COUNTER_IN)
-               return STC_IN_CHAIN;
-       else if (iotype == NFACCT_COUNTER_OUT)
-               return STC_OUT_CHAIN;
-       else if (iotype == NFACCT_COUNTER_FORWARD) //LCOV_EXCL_LINE
-               return STC_FRWD_CHAIN; //LCOV_EXCL_LINE
+       if (iotype == NFACCT_COUNTER_IN) {
+               if (intend == NFACCT_COUNTER ||
+                       intend == NFACCT_TETH_COUNTER) {
+                       if (app_state == STC_APP_STATE_FOREGROUND)
+                               return STC_IN_FG_CHAIN;
+                       else
+                               return STC_IN_BG_CHAIN;
+               } else if (intend == NFACCT_ALLOW ||
+                               intend == NFACCT_TETH_ALLOW) {
+                       return STC_IN_ACCEPT_CHAIN;
+               } else {
+                       if (classid == STC_BACKGROUND_APP_CLASSID)
+                               return STC_IN_BG_DROP_CHAIN;
+                       else
+                               return STC_IN_DROP_CHAIN;
+               }
+       } else if (iotype == NFACCT_COUNTER_OUT) {
+               if (intend == NFACCT_COUNTER ||
+                       intend == NFACCT_TETH_COUNTER) {
+                       if (app_state == STC_APP_STATE_FOREGROUND)
+                               return STC_OUT_FG_CHAIN;
+                       else
+                               return STC_OUT_BG_CHAIN;
+               } else if (intend == NFACCT_ALLOW ||
+                               intend == NFACCT_TETH_ALLOW) {
+                       return STC_OUT_ACCEPT_CHAIN;
+               } else {
+                       if (classid == STC_BACKGROUND_APP_CLASSID)
+                               return STC_OUT_BG_DROP_CHAIN;
+                       else
+                               return STC_OUT_DROP_CHAIN;
+               }
+       } else if (iotype == NFACCT_COUNTER_FORWARD)
+               return STC_FRWD_CHAIN;
 
        return "";
 }
@@ -464,9 +495,13 @@ static stc_error_e exec_iptables_cmd(nfacct_rule_s *rule)
                        rule->intend == NFACCT_TETH_BLOCK)
                iptables_rule.chain = g_strdup(STC_TETHER_CHAIN);
        else
-               iptables_rule.chain = g_strdup(get_iptables_chain(rule->iotype));
+               iptables_rule.chain = g_strdup(get_iptables_chain(rule->classid,
+                                                               rule->iotype, rule->app_state, rule->intend));
 
-       iptables_rule.classid = rule->classid;
+       if (rule->classid < STC_RESERVED_CLASSID_MAX)
+               iptables_rule.classid = STC_UNKNOWN_CLASSID;
+       else
+               iptables_rule.classid = rule->classid;
        iptables_rule.direction = (rule->iotype & NFACCT_COUNTER_IN) ?
                                        IPTABLES_DIRECTION_IN : IPTABLES_DIRECTION_OUT;
        iptype = (iptables_ip_type_e)rule->iptype;
@@ -476,22 +511,14 @@ static stc_error_e exec_iptables_cmd(nfacct_rule_s *rule)
        iptables_rule.d_iprange_type = rule->dst_iprange_type;
 
        /* specify source and destination ip address if any */
-       if (rule->src_ip1) {
-               if (!inet_aton(rule->src_ip1, &iptables_rule.s_ip1))
-                       STC_LOGE("Failed to inet aton [%s]", rule->src_ip1);
-       }
-       if (rule->src_ip2) {
-               if (!inet_aton(rule->src_ip2, &iptables_rule.s_ip2))
-                       STC_LOGE("Failed to inet aton [%s]", rule->src_ip2);
-       }
-       if (rule->dst_ip1) {
-               if (!inet_aton(rule->dst_ip1, &iptables_rule.d_ip1))
-                       STC_LOGE("Failed to inet aton [%s]", rule->dst_ip1);
-       }
-       if (rule->dst_ip2) {
-               if (!inet_aton(rule->dst_ip2, &iptables_rule.d_ip2))
-                       STC_LOGE("Failed to inet aton [%s]", rule->dst_ip2);
-       }
+       if (rule->src_ip1)
+               inet_aton(rule->src_ip1, &iptables_rule.s_ip1);
+       if (rule->src_ip2)
+               inet_aton(rule->src_ip2, &iptables_rule.s_ip2);
+       if (rule->dst_ip1)
+               inet_aton(rule->dst_ip1, &iptables_rule.d_ip1);
+       if (rule->dst_ip2)
+               inet_aton(rule->dst_ip2, &iptables_rule.d_ip2);
 
        if (rule->action == NFACCT_ACTION_DELETE) {
                /* delete interface rule */
@@ -654,7 +681,6 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule)
        char *jump_cmd = get_iptables_jump(rule->jump);
        char nfacct_buf[sizeof(NFACCT_NAME_MOD) +
                3*MAX_DEC_SIZE(int) + 4 + 1];
-       uint32_t classid = rule->classid;
        stc_error_e ret;
 
        if (rule->iotype & NFACCT_COUNTER_IN) {
@@ -682,9 +708,6 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule)
                ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0,
                                 STC_ERROR_FAIL, "Not enought buffer");
 
-               classid = rule->classid;
-               rule->classid = 0;
-
                ret = exec_iptables_cmd(rule);
                ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
                                 "Can't set conditional block for ingress"
@@ -719,8 +742,6 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule)
                //LCOV_EXCL_STOP
        }
 
-       rule->classid = classid;
-
        if (rule->iotype & NFACCT_COUNTER_OUT) {
                /* outcome part */
                rule->iotype = NFACCT_COUNTER_OUT;
@@ -746,9 +767,6 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule)
                ret_value_msg_if(ret > sizeof(nfacct_buf) || ret < 0,
                                 STC_ERROR_FAIL, "Not enough buffer");
 
-               classid = rule->classid;
-               rule->classid = 0;
-
                ret = exec_iptables_cmd(rule);
                ret_value_msg_if(ret != STC_ERROR_NONE, STC_ERROR_FAIL,
                                 "Can't set conditional block for "
@@ -781,6 +799,7 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule)
                }
                //LCOV_EXCL_STOP
        }
+
        return STC_ERROR_NONE;
 }
 
@@ -798,6 +817,7 @@ stc_error_e produce_net_rule(nfacct_rule_s *rule)
 
        if (rule->classid != STC_ALL_APP_CLASSID &&
            rule->classid != STC_TETHERING_APP_CLASSID &&
+           rule->classid != STC_BACKGROUND_APP_CLASSID &&
            rule->classid != STC_TOTAL_DATACALL_CLASSID &&
            rule->classid != STC_TOTAL_WIFI_CLASSID &&
            rule->classid != STC_TOTAL_BLUETOOTH_CLASSID &&
index 7520709..4506f0e 100644 (file)
@@ -105,6 +105,7 @@ struct nfacct_rule {
        nfacct_rule_direction iotype;
        nfacct_rule_intend intend;
        nfacct_rule_jump jump; /* in most cases jump is evalutation based on intend, but not always */
+       stc_app_state_e app_state;
        stc_rstn_state_e rstn_state;
        nfacct_rule_iptype iptype;
        nfacct_rule_iprange_type src_iprange_type;
similarity index 77%
rename from src/monitor/include/stc-default-connection.h
rename to src/monitor/include/stc-connection.h
index 222e77b..85d27de 100644 (file)
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef __STC_DEFAULT_CONNECTION_H__
-#define __STC_DEFAULT_CONNECTION_H__
+#ifndef __STC_CONNECTION_H__
+#define __STC_CONNECTION_H__
 
 #include <glib.h>
 #include "stc-error.h"
@@ -39,7 +39,7 @@ typedef struct {
 } tether_iface_s;
 
 /**
- * @brief default connection information will be fetched from net-config
+ * @brief connection information will be fetched from connman
  */
 typedef struct {
        /* to identify each connection uniquely */
@@ -63,12 +63,11 @@ typedef struct {
 
        /* tethering interface */
        tether_iface_s tether_iface;
-} default_connection_s;
+} stc_connection_s;
 
-stc_error_e stc_default_connection_monitor_init(stc_s *stc);
-stc_error_e stc_default_connection_monitor_deinit(stc_s *stc);
+stc_error_e stc_connection_monitor_init(stc_s *stc);
+stc_error_e stc_connection_monitor_deinit(stc_s *stc);
 
-gchar *stc_default_connection_get_ifname(void);
-default_connection_s *stc_get_default_connection(void);
+GSList *stc_get_connection_list(void);
 
-#endif /* __STC_DEFAULT_CONNECTION_H__ */
+#endif /* __STC_CONNECTION_H__ */
diff --git a/src/monitor/include/stc-monitor-app.h b/src/monitor/include/stc-monitor-app.h
new file mode 100644 (file)
index 0000000..3a312fb
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#ifndef __STC_MONITOR_APP_H__
+#define __STC_MONITOR_APP_H__
+
+#include <glib.h>
+
+#include "stc-error.h"
+#include "stc-manager.h"
+#include "stc-monitor-context.h"
+#include "stc-connection.h"
+
+#define MAC_ADDRESS_LEN 18
+#define SUBSCRIBERID_NONE "none_subid"
+
+typedef struct {
+       uint32_t classid;  /**< classid for a package */
+       gchar *pkg_id;  /**< package id */
+       gchar *app_id;  /**< application id */
+       stc_app_type_e type;  /**< type of application */
+       stc_app_state_e state;
+       stc_data_counter_s data_usage;
+       stc_data_counter_s counter;
+       GHashTable *processes;  /**< applications instances */
+       char mac[MAC_ADDRESS_LEN+1]; /**< application mac address */
+} stc_app_value_s;
+
+void stc_monitor_app_update_counter(stc_app_value_s *value,
+                                classid_bytes_context_s *context);
+
+void stc_monitor_app_update_iface_counter(classid_bytes_context_s *context);
+
+gboolean stc_monitor_app_flush_stats_to_db(gpointer user_data);
+
+stc_error_e stc_monitor_app_add(uint32_t classid,
+                               const char *app_id,
+                               const char *pkg_id,
+                               const stc_app_value_s value);
+
+void stc_monitor_app_add_by_iface(const char *app_id);
+
+void stc_monitor_app_add_monitor(gpointer key,
+                               gpointer value, gpointer data);
+
+void stc_monitor_app_add_by_connection(stc_connection_s *conn);
+
+void stc_monitor_app_add_accept(gpointer key,
+                               gpointer value, gpointer data);
+
+stc_error_e stc_monitor_app_remove(uint32_t classid, const char *app_id);
+
+void stc_monitor_app_remove_monitor(gpointer key,
+                               gpointer value, gpointer data);
+
+void stc_monitor_app_remove_by_connection(stc_connection_s *conn);
+
+void stc_monitor_app_remove_accept(gpointer key,
+                               gpointer value, gpointer data);
+
+gboolean stc_monitor_app_lookup(uint32_t classid);
+
+GHashTable *stc_monitor_apps_init(void);
+
+#endif /* __STC_MONITOR_APP_H__ */
diff --git a/src/monitor/include/stc-monitor-context.h b/src/monitor/include/stc-monitor-context.h
new file mode 100644 (file)
index 0000000..82d15f7
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#ifndef __STC_MONITOR_CONTEXT_H__
+#define __STC_MONITOR_CONTEXT_H__
+
+#include "stc-manager.h"
+
+typedef struct {
+       time_t now;
+       time_t month_start_ts;
+       time_t week_start_ts;
+       time_t day_start_ts;
+       int is_updated;
+} reset_time_limits_context_s;
+
+typedef struct {
+       struct nfacct_rule *counter;
+       int64_t bytes;
+       gboolean data_limit_exceeded;
+} classid_bytes_context_s;
+
+/**
+ * @brief structure to store system info
+ */
+typedef struct {
+       int contr_sock;  /**< socket used for getting kernel counters */
+       guint contr_timer_id;  /**< timer id for periodically getting kernel counters */
+       guint contr_gsource_id;
+       stc_data_counter_s du_curr;  /**< current data usage */
+       GHashTable *rstns;  /**< restriction rules */
+       gboolean rstns_updated;
+       GHashTable *apps;  /**< monitored applications */
+       gboolean apps_updated;
+       gboolean background_state;
+       time_t last_month_ts;
+       time_t last_week_ts;
+       time_t last_day_ts;
+       int month_start_date;
+} stc_system_s;
+
+#endif /* __STC_MONITOR_CONTEXT_H__ */
diff --git a/src/monitor/include/stc-monitor-ipt.h b/src/monitor/include/stc-monitor-ipt.h
new file mode 100644 (file)
index 0000000..ee38069
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#ifndef __STC_MONITOR_IPT_H__
+#define __STC_MONITOR_IPT_H__
+
+#include "helper-nfacct-rule.h"
+
+stc_error_e stc_monitor_ipt_add_in(struct nfacct_rule *counter);
+stc_error_e stc_monitor_ipt_add_out(struct nfacct_rule *counter);
+stc_error_e stc_monitor_ipt_del_in(struct nfacct_rule *counter);
+stc_error_e stc_monitor_ipt_del_out(struct nfacct_rule *counter);
+
+stc_error_e stc_monitor_ip6t_add_in(struct nfacct_rule *counter);
+stc_error_e stc_monitor_ip6t_add_out(struct nfacct_rule *counter);
+stc_error_e stc_monitor_ip6t_del_in(struct nfacct_rule *counter);
+stc_error_e stc_monitor_ip6t_del_out(struct nfacct_rule *counter);
+
+stc_error_e stc_monitor_tether_add_in(struct nfacct_rule *counter,
+               const gchar *ipaddr);
+stc_error_e stc_monitor_tether_add_out(struct nfacct_rule *counter,
+               const gchar *ipaddr);
+stc_error_e stc_monitor_tether_del_in(struct nfacct_rule *counter,
+               const gchar *ipaddr);
+stc_error_e stc_monitor_tether_del_out(struct nfacct_rule *counter,
+               const gchar *ipaddr);
+
+#endif /* __STC_MONITOR_IPT_H__ */
diff --git a/src/monitor/include/stc-monitor-proc.h b/src/monitor/include/stc-monitor-proc.h
new file mode 100644 (file)
index 0000000..602912b
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#ifndef __STC_MONITOR_PROC_H__
+#define __STC_MONITOR_PROC_H__
+
+#include <glib.h>
+
+#include "stc-error.h"
+#include "stc-manager.h"
+
+typedef struct {
+       pid_t pid;
+       stc_app_state_e ground;
+} stc_proc_value_s;
+
+typedef struct {
+       pid_t pid;
+       stc_app_value_s *app_value;
+       gboolean entry_removed;
+} remove_pid_context_s;
+
+stc_error_e stc_monitor_proc_update_ground(uint32_t classid,
+                                               const char *app_id,
+                                               const stc_proc_value_s value);
+
+stc_error_e stc_monitor_proc_add(uint32_t classid,
+                                               const char *app_id,
+                                               const stc_proc_value_s value);
+
+stc_error_e stc_monitor_proc_remove(uint32_t classid,
+                                               pid_t pid);
+
+stc_error_e stc_monitor_proc_move(uint32_t from,
+                                               uint32_t to);
+
+#endif /* __STC_MONITOR_PROC_H__ */
diff --git a/src/monitor/include/stc-monitor-rstn.h b/src/monitor/include/stc-monitor-rstn.h
new file mode 100644 (file)
index 0000000..ecf404d
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#ifndef __STC_MONITOR_RSTN_H__
+#define __STC_MONITOR_RSTN_H__
+
+#include <glib.h>
+
+#include "stc-error.h"
+#include "stc-manager.h"
+#include "stc-monitor-context.h"
+#include "stc-connection.h"
+#include "stc-restriction.h"
+#include "table-restrictions.h"
+
+#define GRANULARITY 10
+
+#define MAX_INT_LENGTH 128
+#define MAX_KEY_LENGTH 128
+
+typedef enum {
+       STC_RSTN_LIMIT_TYPE_DATA_WARN,
+       STC_RSTN_LIMIT_TYPE_DATA,
+       STC_RSTN_LIMIT_TYPE_MONTHLY,
+       STC_RSTN_LIMIT_TYPE_WEEKLY,
+       STC_RSTN_LIMIT_TYPE_DAILY,
+       STC_RSTN_LIMIT_TYPE_MAX
+} stc_rstn_limit_type_e;
+
+typedef enum {
+       STC_RSTN_STATE_INIT = -1,
+       STC_RSTN_STATE_UNSET,
+       STC_RSTN_STATE_SET,
+} stc_rstn_noti_state_e;
+
+typedef struct {
+       uint32_t classid;
+       char *app_id;
+       stc_iface_type_e iftype;
+       char *ifname;
+       char *subscriber_id;
+       stc_roaming_type_e roaming;
+       char *mac;
+
+       uint64_t restriction_id;
+       stc_rstn_state_e rstn_state;
+       stc_rstn_type_e rstn_type;
+
+       int64_t counter[STC_RSTN_LIMIT_TYPE_MAX];
+       int64_t limit[STC_RSTN_LIMIT_TYPE_MAX];
+       int32_t limit_exceeded;
+       int32_t limit_notified;
+
+       int month_start_date;
+       time_t month_start_ts;
+} stc_rstn_data_s;
+
+typedef struct {
+       GSList *rules;
+} stc_rstn_value_s;
+
+typedef struct {
+       time_t month_start_ts;
+       time_t week_start_ts;
+       time_t day_start_ts;
+       int64_t monthly_stat;
+       int64_t weekly_stat;
+       int64_t daily_stat;
+} stc_rstn_cumulative_data_s;
+
+void stc_monitor_rstn_reset_time_counters_if_required(void);
+
+void stc_monitor_rstn_update_counter(gpointer data,
+                                       gpointer user_data);
+
+void stc_monitor_rstn_update_iface_counter(classid_bytes_context_s *context);
+
+void stc_monitor_rstn_action_when_limit_exceeded(stc_rstn_limit_type_e limit_type,
+                                               stc_rstn_data_s *rstn_data,
+                                               classid_bytes_context_s *context);
+
+gboolean stc_monitor_rstn_flush_contr_to_db(gpointer user_data);
+
+stc_error_e stc_monitor_rstn_add(const table_restrictions_info *info);
+
+void stc_monitor_rstn_add_for_app(uint32_t classid);
+
+void stc_monitor_rstn_add_by_connection(stc_connection_s *conn);
+
+stc_error_e stc_monitor_rstn_remove(const table_restrictions_info *info);
+
+void stc_monitor_rstn_remove_for_app(uint32_t classid);
+
+void stc_monitor_rstn_remove_by_connection(stc_connection_s *conn);
+
+void stc_monitor_rstns_load(void);
+
+GHashTable *stc_monitor_rstns_init(void);
+
+#endif /* __STC_MONITOR_RSTN_H__ */
old mode 100755 (executable)
new mode 100644 (file)
index d138cbc..32404b0
 #include <glib.h>
 #include "stc-error.h"
 #include "stc-manager.h"
-#include "stc-restriction.h"
 #include "stc-manager-util.h"
-#include "table-restrictions.h"
+#include "stc-monitor-context.h"
+#include "stc-monitor-app.h"
+#include "stc-monitor-proc.h"
+#include "stc-monitor-rstn.h"
 #include "helper-nl.h"
 
 /* 1 seconds */
 #define CONTR_TIMER_INTERVAL 1
-#define MAC_ADDRESS_LEN 18
 
-/**
- * @brief enumeration for data limit types
- */
-typedef enum {
-       STC_RSTN_LIMIT_TYPE_DATA_WARN,
-       STC_RSTN_LIMIT_TYPE_DATA,
-       STC_RSTN_LIMIT_TYPE_MONTHLY,
-       STC_RSTN_LIMIT_TYPE_WEEKLY,
-       STC_RSTN_LIMIT_TYPE_DAILY,
-       STC_RSTN_LIMIT_TYPE_MAX
-} stc_rstn_limit_type_e;
-
-typedef enum {
-       STC_RSTN_STATE_INIT = -1,
-       STC_RSTN_STATE_UNSET,
-       STC_RSTN_STATE_SET,
-} stc_rstn_noti_state_e;
+#ifndef VCONFKEY_STC_BACKGROUND_STATE
+#define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
+#endif
 
-/**
- * @brief key for processes tree
- */
-typedef struct {
-       stc_app_state_e ground;  /**< application state foreground/background */
-} stc_process_value_s;
+#ifndef VCONFKEY_SETAPPL_DATA_RESTRICTION_INT
+#define VCONFKEY_SETAPPL_DATA_RESTRICTION_INT "db/setting/data_restriction"
+#endif
 
 /**
- * @brief value for processes tree
+ * @brief initializes stc monitor module
  */
-typedef struct {
-       pid_t pid;  /**< process id */
-} stc_process_key_s;
+stc_error_e stc_monitor_init(void);
 
 /**
- * @brief key for apps tree
+ * @brief deinitializes stc monitor module
  */
-typedef struct {
-       gchar *pkg_id;  /**< package id */
-       gchar *app_id;  /**< application id */
-} stc_app_key_s;
+stc_error_e stc_monitor_deinit(void);
 
-/**
- * @brief value for apps tree
- */
-typedef struct {
-       uint32_t classid;  /**< classid for a package */
-       stc_app_type_e type;  /**< type of application */
-       stc_data_counter_s data_usage;
-       stc_data_counter_s counter;
-       GTree *processes;  /**< applications instances */
-       char mac[MAC_ADDRESS_LEN+1]; /**< application mac address */
-} stc_app_value_s;
+GHashTable *stc_monitor_get_system_apps(void);
 
-/**
- * @brief key for rstn_rules tree
- */
-typedef struct {
-       gchar *app_id;
-       gchar *ifname;
-       gchar *mac;
-       gchar *subscriber_id;
-       stc_iface_type_e iftype;
-       stc_roaming_type_e roaming;
-} stc_rstn_key_s;
+GHashTable *stc_monitor_get_system_rstns(void);
 
-/**
- * @brief value for rstn_rules tree
- */
-typedef struct {
-       uint64_t restriction_id;
-       uint32_t classid;
-       stc_rstn_state_e rstn_state;
-       stc_rstn_type_e rstn_type;
+int stc_monitor_get_contr_sock(void);
 
-       int64_t counter[STC_RSTN_LIMIT_TYPE_MAX];
-       int64_t limit[STC_RSTN_LIMIT_TYPE_MAX];
-       int32_t limit_exceeded;
-       int32_t limit_notified;
+time_t stc_monitor_get_last_month_ts(void);
 
-       int month_start_date;
-       time_t month_start_ts;
-} stc_rstn_value_s;
+void stc_monitor_set_last_month_ts(time_t time);
 
-/**
- * @brief structure to store system info
- */
-typedef struct {
-       int contr_sock;  /**< socket used for getting kernel counters */
-       guint contr_timer_id;  /**< timer id for periodically getting kernel counters */
-       guint contr_gsource_id;
-       stc_data_counter_s du_curr;  /**< current data usage */
-       GTree *rstns;  /**< restriction rules */
-       gboolean rstns_tree_updated;
-       GTree *apps;  /**< monitored applications */
-       gboolean apps_tree_updated;
-       guint background_state;
-       time_t last_month_ts;
-       time_t last_week_ts;
-       time_t last_day_ts;
-       int month_start_date;
-} stc_system_s;
+time_t stc_monitor_get_last_week_ts(void);
 
-/**
- * @brief initializes stc monitor module
- */
-stc_error_e stc_monitor_init(void);
+void stc_monitor_set_last_week_ts(time_t time);
 
-/**
- * @brief deinitializes stc monitor module
- */
-stc_error_e stc_monitor_deinit(void);
+time_t stc_monitor_get_last_day_ts(void);
 
-/**
- * @brief creates an application entry
- */
-stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
-                                       const stc_app_value_s app_value);
+void stc_monitor_set_last_day_ts(time_t time);
 
-/**
- * @brief deletes an application entry
- */
-stc_error_e stc_monitor_application_remove(const stc_app_key_s app_key);
+void stc_monitor_set_rstns_updated(gboolean value);
 
-/**
- * @brief associates process to an application
- */
-stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
-                                   const stc_process_key_s proc_key,
-                                   const stc_process_value_s proc_value);
+gboolean stc_monitor_get_rstns_updated(void);
 
-/**
- * @brief removes associated process from respective application
- */
-stc_error_e stc_monitor_process_remove(pid_t pid);
+void stc_monitor_set_apps_updated(gboolean value);
 
-/**
- * @brief updates process's ground attribute
- */
-stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
-                                             const stc_process_key_s proc_key,
-                                             stc_app_state_e ground);
+gboolean stc_monitor_get_apps_updated(void);
 
-void stc_monitor_update_rstn_by_default_connection(void *default_connection);
+void stc_monitor_set_background_state(gboolean value);
 
-stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info);
+gboolean stc_monitor_get_background_state(void);
 
-stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info);
+void stc_monitor_update_by_connection(void *data);
 
-stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline);
+void stc_monitor_add_by_connection(void *data);
 
-int stc_monitor_get_counter_socket(void);
+void stc_monitor_remove_by_connection(void *data);
+
+stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline);
 
 #endif /* __STC_MONITOR_H__ */
similarity index 62%
rename from src/monitor/stc-default-connection.c
rename to src/monitor/stc-connection.c
index 244a8b9..fb16297 100644 (file)
@@ -20,7 +20,7 @@
 #include "stc-monitor.h"
 #include "stc-firewall.h"
 #include "stc-manager-gdbus.h"
-#include "stc-default-connection.h"
+#include "stc-connection.h"
 
 /* connman service dbus details */
 #define CONNMAN_SERVICE                          "net.connman"
 
 #define VCONF_TELEPHONY_DEFAULT_DATA_SERVICE     "db/telephony/dualsim/default_data_service"
 
-default_connection_s g_default_connection;
-guint g_default_connection_sub_id = 0;
+stc_connection_s *g_default_connection = NULL;
+GSList *g_connection_list = NULL;
+guint g_connection_sub_id = 0;
+
+static void __conn_list_free(gpointer value)
+{
+       stc_connection_s *conn = (stc_connection_s *)value;
+
+       FREE(conn->path);
+       FREE(conn->ifname);
+       FREE(conn->tether_iface.ifname);
+       FREE(conn);
+}
+
+static gint __conn_list_comp(gconstpointer a, gconstpointer b)
+{
+       stc_connection_s *conn = (stc_connection_s *)a;
+       char *path = (char *)b;
+
+       if (g_strcmp0(conn->path, path) == 0)
+               return 0;
+
+       return -1;
+}
+
+static void __update_monitor_by_conn(gpointer data, gpointer user_data)
+{
+       stc_connection_s *conn = (stc_connection_s *)data;
+
+       stc_monitor_update_by_connection(conn);
+}
 
 static int __telephony_get_current_sim(void)
 {
@@ -78,7 +107,7 @@ static int __telephony_get_current_sim(void)
        return current_sim;
 }
 
-static void __make_imsi_to_subscriber_id(char *imsi)
+static void __make_imsi_to_subscriber_id(stc_connection_s *conn, char *imsi)
 {
        int i = 0;
        SHA256_CTX ctx;
@@ -89,11 +118,11 @@ static void __make_imsi_to_subscriber_id(char *imsi)
        SHA256_Final(md, &ctx);
 
        for (i = 0; i < SHA256_DIGEST_LENGTH; ++i)
-               snprintf(g_default_connection.subscriber_id + (i * 2), 3, "%02x", md[i]);
+               snprintf(conn->subscriber_id + (i * 2), 3, "%02x", md[i]);
 }
 
 static void __telephony_get_modem_subscriber_id(GDBusConnection *connection,
-                                      const char *default_modem_name)
+                               stc_connection_s *conn, const char *default_modem_name)
 {
        GVariant *message = NULL;
        char tel_path[MAX_PATH_LENGTH];
@@ -130,14 +159,15 @@ static void __telephony_get_modem_subscriber_id(GDBusConnection *connection,
        }
 
        snprintf(imsi, IMSI_LENGTH, "%s%s", plmn, msin);
-       __make_imsi_to_subscriber_id(imsi);
+       __make_imsi_to_subscriber_id(conn, imsi);
 
 done:
        g_variant_unref(message);
        return;
 }
 
-static void __telephony_update_default_modem_subscriber_id(GDBusConnection *connection)
+static void __telephony_update_default_modem_subscriber_id(GDBusConnection *connection,
+                               stc_connection_s *conn)
 {
        GVariant *message = NULL;
        GVariantIter *iter = NULL;
@@ -173,7 +203,7 @@ static void __telephony_update_default_modem_subscriber_id(GDBusConnection *conn
                current_sim--; //LCOV_EXCL_LINE
        }
 
-       __telephony_get_modem_subscriber_id(connection, default_modem_name);
+       __telephony_get_modem_subscriber_id(connection, conn, default_modem_name);
 
        FREE(default_modem_name);
        g_variant_iter_free(iter);
@@ -181,38 +211,27 @@ static void __telephony_update_default_modem_subscriber_id(GDBusConnection *conn
        return;
 }
 
-static void __print_default_connection_info(void)
+static void __print_connection_info(stc_connection_s *conn)
 {
-       STC_LOGI("============= default connection info ============");
-       STC_LOGI("path    [%s]", g_default_connection.path);
-       STC_LOGI("type    [%d]", g_default_connection.type);
-       STC_LOGI("ifname  [%s]", g_default_connection.ifname);
-       STC_LOGI("roaming [%u]", g_default_connection.roaming ? TRUE : FALSE);
-       if (g_default_connection.type == STC_IFACE_DATACALL)
-               STC_LOGI("sub_id  [%s]", g_default_connection.subscriber_id);
+       STC_LOGI("============= connection info ============");
+       STC_LOGI("path    [%s]", conn->path);
+       STC_LOGI("type    [%d]", conn->type);
+       STC_LOGI("ifname  [%s]", conn->ifname);
+       STC_LOGI("roaming [%u]", conn->roaming ? TRUE : FALSE);
+       if (conn->type == STC_IFACE_DATACALL)
+               STC_LOGI("sub_id  [%s]", conn->subscriber_id);
        STC_LOGI("==================================================");
 }
 
 static void __print_tether_connection_info(void)
 {
        STC_LOGI("============= tethering connection info ============");
-       STC_LOGI("mode    [%u]", g_default_connection.tether_state ? TRUE : FALSE);
-       STC_LOGI("type    [%d]", g_default_connection.tether_iface.type);
-       STC_LOGI("ifname  [%s]", g_default_connection.tether_iface.ifname);
+       STC_LOGI("mode    [%u]", g_default_connection->tether_state ? TRUE : FALSE);
+       STC_LOGI("type    [%d]", g_default_connection->tether_iface.type);
+       STC_LOGI("ifname  [%s]", g_default_connection->tether_iface.ifname);
        STC_LOGI("====================================================");
 }
 
-static void __reset_default_connection_data(void)
-{
-       FREE(g_default_connection.path);
-       FREE(g_default_connection.ifname);
-       FREE(g_default_connection.tether_iface.ifname);
-       g_default_connection.type = STC_IFACE_UNKNOWN;
-       g_default_connection.roaming = FALSE;
-       g_default_connection.tether_iface.type = STC_IFACE_UNKNOWN;
-       g_default_connection.tether_state = FALSE;
-}
-
 static gboolean __is_cellular_internet_profile(const char *profile)
 {
        const char internet_suffix[] = "_1";
@@ -293,8 +312,8 @@ static gboolean __is_connected(GVariantIter *array)
        return is_connected;
 }
 
-static void __get_default_connection_info(GDBusConnection *connection,
-                                         const char *object_path)
+static void __get_connection_info(GDBusConnection *connection,
+                               stc_connection_s *conn, const char *object_path)
 {
        GVariant *message = NULL;
        GVariantIter *iter = NULL;
@@ -338,8 +357,7 @@ static void __get_default_connection_info(GDBusConnection *connection,
                                        const gchar *value =
                                                g_variant_get_string(variant1,
                                                                     NULL);
-                                       g_default_connection.ifname =
-                                               g_strdup(value);
+                                       conn->ifname = g_strdup(value);
                                }
                        }
 
@@ -351,7 +369,7 @@ static void __get_default_connection_info(GDBusConnection *connection,
                        if (g_variant_is_of_type(variant,
                                                 G_VARIANT_TYPE_BOOLEAN)) {
                                roaming = g_variant_get_boolean(variant);
-                               g_default_connection.roaming = roaming;
+                               conn->roaming = roaming;
                        }
                }
        }
@@ -366,12 +384,14 @@ done:
        return;
 }
 
-static stc_error_e __get_default_profile(GDBusConnection *connection)
+static stc_error_e __get_connected_profiles(GDBusConnection *connection)
 {
        GVariant *message = NULL;
        GVariantIter *iter = NULL;
        GVariantIter *next;
        gchar *object_path;
+       stc_connection_s *conn;
+       gboolean default_conn = TRUE;
 
        message = stc_manager_gdbus_call_sync(connection,
                                              CONNMAN_SERVICE,
@@ -383,6 +403,8 @@ static stc_error_e __get_default_profile(GDBusConnection *connection)
                return STC_ERROR_FAIL; //LCOV_EXCL_LINE
        }
 
+       g_slist_free_full(g_connection_list, __conn_list_free);
+
        g_variant_get(message, "(a(oa{sv}))", &iter);
        while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
                if (object_path == NULL)
@@ -393,105 +415,144 @@ static stc_error_e __get_default_profile(GDBusConnection *connection)
                        continue;
 
                if (__is_connected(next) == TRUE) {
-                       /* reset old default connection data */
-                       FREE(g_default_connection.path);
-                       FREE(g_default_connection.ifname);
-                       g_default_connection.type = STC_IFACE_UNKNOWN;
-                       g_default_connection.roaming = FALSE;
+                       conn = MALLOC0(stc_connection_s, 1);
+                       if (conn == NULL)
+                               continue;
+
+                       conn->path = g_strdup(object_path);
+                       conn->roaming = FALSE;
+
+                       if (__is_cellular_profile(conn->path)) {
+                               conn->type = STC_IFACE_DATACALL;
+                               __telephony_update_default_modem_subscriber_id(connection, conn);
+                       } else if (__is_wifi_profile(conn->path)) {
+                               conn->type = STC_IFACE_WIFI;
+                       } else if (__is_ethernet_profile(conn->path)) {
+                               conn->type = STC_IFACE_WIRED;
+                       } else if (__is_bluetooth_profile(conn->path)) {
+                               conn->type = STC_IFACE_BLUETOOTH;
+                       } else {
+                               conn->type = STC_IFACE_UNKNOWN;
+                       }
+
+                       __get_connection_info(connection, conn, conn->path);
+                       __print_connection_info(conn);
+
+                       if (default_conn == TRUE) {
+                               g_default_connection = conn;
+                               default_conn = FALSE;
+                       }
+
+                       g_connection_list = g_slist_append(g_connection_list, conn);
 
-                       g_default_connection.path = g_strdup(object_path);
                        g_free(object_path);
                        g_variant_iter_free(next);
-                       break;
+                       continue;
                }
        }
 
        g_variant_iter_free(iter);
        g_variant_unref(message);
 
-       if (__is_cellular_profile(g_default_connection.path)) {
-               g_default_connection.type = STC_IFACE_DATACALL; //LCOV_EXCL_LINE
-               __telephony_update_default_modem_subscriber_id(connection); //LCOV_EXCL_LINE
-       } else if (__is_wifi_profile(g_default_connection.path)) {
-               g_default_connection.type = STC_IFACE_WIFI; //LCOV_EXCL_LINE
-       } else if (__is_ethernet_profile(g_default_connection.path)) {
-               g_default_connection.type = STC_IFACE_WIRED; //LCOV_EXCL_LINE
-       } else if (__is_bluetooth_profile(g_default_connection.path)) {
-               g_default_connection.type = STC_IFACE_BLUETOOTH; //LCOV_EXCL_LINE
-       } else {
-               g_default_connection.type = STC_IFACE_UNKNOWN; //LCOV_EXCL_LINE
-       }
-
-       __get_default_connection_info(connection, g_default_connection.path);
-
-       __print_default_connection_info();
-
-       stc_monitor_update_rstn_by_default_connection(&g_default_connection);
+       g_slist_foreach(g_connection_list, __update_monitor_by_conn, NULL);
        stc_firewall_update();
 
        return STC_ERROR_NONE;
 }
 
-static void _service_signal_cb(GDBusConnection *conn,
-                              const gchar *name, const gchar *path,
-                              const gchar *interface, const gchar *sig,
-                              GVariant *param, gpointer user_data)
+static stc_error_e __get_default_connection(GDBusConnection *connection)
 {
-       gchar *sigvalue = NULL;
-       GVariant *variant = NULL;
-       stc_s *stc = (stc_s *)stc_get_manager();
-       ret_msg_if(stc == NULL, "failed to get stc data");
+       GVariant *message = NULL;
+       GVariantIter *iter = NULL;
+       GVariantIter *next;
+       gchar *object_path;
 
-       if (path == NULL || param == NULL)
-               goto done;
+       message = stc_manager_gdbus_call_sync(connection,
+                                             CONNMAN_SERVICE,
+                                             CONNMAN_MANAGER_PATH,
+                                             CONNMAN_MANAGER_INTERFACE,
+                                             "GetServices", NULL);
+       if (message == NULL) {
+               STC_LOGE("Failed to get profiles"); //LCOV_EXCL_LINE
+               return STC_ERROR_FAIL; //LCOV_EXCL_LINE
+       }
 
-       g_variant_get(param, "(sv)", &sigvalue, &variant);
-       if (sigvalue == NULL)
-               goto done;
+       g_variant_get(message, "(a(oa{sv}))", &iter);
+       while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
+               if (object_path == NULL)
+                       continue; //LCOV_EXCL_LINE
 
-       if (g_strcmp0(sig, CONNMAN_SIGNAL_PROPERTY_CHANGED) != 0)
-               goto done;
+               if (__is_cellular_profile(object_path) &&
+                   !__is_cellular_internet_profile(object_path))
+                       continue;
 
-       if (g_strcmp0(sigvalue, "State") == 0 &&
-           g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
-               const gchar *state = NULL;
+               if (__is_connected(next) == TRUE) {
+                       GSList *comp = g_slist_find_custom(g_connection_list,
+                                                               object_path, __conn_list_comp);
+                       if (comp && comp->data)
+                               g_default_connection = comp->data;
 
-               state = g_variant_get_string(variant, NULL);
-               if (g_strcmp0(state, "ready") == 0 ||
-                   g_strcmp0(state, "online") == 0) {
-                       if (g_strcmp0(g_default_connection.path, path)) {
-                               __reset_default_connection_data();
-                               __get_default_profile(stc->connection);
-                       }
-               } else {
-                       if (g_strcmp0(g_default_connection.path, path) == 0) {
-                               __reset_default_connection_data(); //LCOV_EXCL_LINE
-                               __get_default_profile(stc->connection); //LCOV_EXCL_LINE
-                       }
+                       break;
                }
-       } else if (g_strcmp0(sigvalue, "Roaming") == 0) {
-               //LCOV_EXCL_START
-               if (g_strcmp0(g_default_connection.path, path) == 0) {
-                       gboolean roaming = 0;
+       }
 
-                       if (g_variant_is_of_type(variant,
-                                                G_VARIANT_TYPE_BOOLEAN)) {
-                               roaming = g_variant_get_boolean(variant);
-                               g_default_connection.roaming = roaming;
-                       }
-               }
-               //LCOV_EXCL_STOP
+       g_variant_iter_free(iter);
+       g_variant_unref(message);
+
+       return STC_ERROR_NONE;
+}
+
+static void __append_connected_profile(GDBusConnection *connection,
+                               const char *path)
+{
+       stc_connection_s *conn;
+
+       conn = MALLOC0(stc_connection_s, 1);
+       if (conn == NULL)
+               return;
+
+       conn->path = g_strdup(path);
+       conn->roaming = FALSE;
+
+       if (__is_cellular_profile(conn->path)) {
+               conn->type = STC_IFACE_DATACALL;
+               __telephony_update_default_modem_subscriber_id(connection, conn);
+       } else if (__is_wifi_profile(conn->path)) {
+               conn->type = STC_IFACE_WIFI;
+       } else if (__is_ethernet_profile(conn->path)) {
+               conn->type = STC_IFACE_WIRED;
+       } else if (__is_bluetooth_profile(conn->path)) {
+               conn->type = STC_IFACE_BLUETOOTH;
        } else {
-               ;//Do nothing
+               conn->type = STC_IFACE_UNKNOWN;
        }
-done:
-       if (sigvalue)
-               g_free(sigvalue);
 
-       if (variant)
-               g_variant_unref(variant);
+       __get_connection_info(connection, conn, conn->path);
+       __print_connection_info(conn);
 
-       return;
+       g_connection_list = g_slist_append(g_connection_list, conn);
+
+       stc_monitor_add_by_connection(conn);
+       stc_firewall_update();
+
+       __get_default_connection(connection);
+}
+
+static void __remove_disconnected_profile(GDBusConnection *connection,
+                               stc_connection_s *conn)
+{
+       __print_connection_info(conn);
+
+       stc_monitor_remove_by_connection(conn);
+
+       g_connection_list = g_slist_remove(g_connection_list, conn);
+
+       __get_default_connection(connection);
+
+       FREE(conn->path);
+       FREE(conn->ifname);
+       FREE(conn->tether_iface.ifname);
+       FREE(conn);
 }
 
 static void __vconf_key_callback(keynode_t *node, void *user_data)
@@ -503,6 +564,9 @@ static void __vconf_key_callback(keynode_t *node, void *user_data)
                return;
        }
 
+       if (g_default_connection == NULL)
+               return;
+
        if (vconf_keynode_get_type(node) != VCONF_TYPE_INT) {
                STC_LOGE("Invalid vconf key type");
                return;
@@ -514,31 +578,31 @@ static void __vconf_key_callback(keynode_t *node, void *user_data)
        switch (vconf_key) {
        case VCONFKEY_MOBILE_HOTSPOT_MODE_USB:
                STC_LOGI("Hotspot mode USB type !");
-               g_default_connection.tether_state = TRUE;
-               g_default_connection.tether_iface.ifname = g_strdup(TETHERING_USB_IF);
-               g_default_connection.tether_iface.type = STC_IFACE_USB;
+               g_default_connection->tether_state = TRUE;
+               g_default_connection->tether_iface.ifname = g_strdup(TETHERING_USB_IF);
+               g_default_connection->tether_iface.type = STC_IFACE_USB;
                break;
        case VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI:
                STC_LOGI("Hotspot mode Wi-Fi type !");
-               g_default_connection.tether_state = TRUE;
-               g_default_connection.tether_iface.ifname = g_strdup(TETHERING_WIFI_IF);
-               g_default_connection.tether_iface.type = STC_IFACE_WIFI;
+               g_default_connection->tether_state = TRUE;
+               g_default_connection->tether_iface.ifname = g_strdup(TETHERING_WIFI_IF);
+               g_default_connection->tether_iface.type = STC_IFACE_WIFI;
                break;
        case VCONFKEY_MOBILE_HOTSPOT_MODE_BT:
                STC_LOGI("Hotspot mode Bluetooth type !");
-               g_default_connection.tether_state = TRUE;
-               g_default_connection.tether_iface.ifname = g_strdup(TETHERING_BT_IF);
-               g_default_connection.tether_iface.type = STC_IFACE_BLUETOOTH;
+               g_default_connection->tether_state = TRUE;
+               g_default_connection->tether_iface.ifname = g_strdup(TETHERING_BT_IF);
+               g_default_connection->tether_iface.type = STC_IFACE_BLUETOOTH;
                break;
        case VCONFKEY_MOBILE_HOTSPOT_MODE_P2P:
                STC_LOGI("Hotspot mode P2P type !");
-               g_default_connection.tether_state = TRUE;
-               g_default_connection.tether_iface.ifname = g_strdup(TETHERING_P2P_IF);
-               g_default_connection.tether_iface.type = STC_IFACE_P2P;
+               g_default_connection->tether_state = TRUE;
+               g_default_connection->tether_iface.ifname = g_strdup(TETHERING_P2P_IF);
+               g_default_connection->tether_iface.type = STC_IFACE_P2P;
                break;
        case VCONFKEY_MOBILE_HOTSPOT_MODE_NONE:
                STC_LOGI("Hotspot mode none");
-               g_default_connection.tether_state = FALSE;
+               g_default_connection->tether_state = FALSE;
                break;
        default:
                STC_LOGE("Unknown Hotspot mode type !");
@@ -546,32 +610,98 @@ static void __vconf_key_callback(keynode_t *node, void *user_data)
        }
 
        /* add monitoring for tethering if active found */
-       if (g_default_connection.tether_state == TRUE && g_default_connection.tether_iface.ifname) {
+       if (g_default_connection->tether_state == TRUE && g_default_connection->tether_iface.ifname) {
                __print_tether_connection_info();
-               stc_monitor_update_rstn_by_default_connection(&g_default_connection);
+               stc_monitor_update_by_connection(&g_default_connection);
                stc_firewall_update();
                STC_LOGI("Data monitoring started for tethering iface !");
                return;
        }
 
        /* remove monitoring for tethering if in-active found */
-       if (g_default_connection.tether_state == FALSE && g_default_connection.tether_iface.ifname) {
-               stc_monitor_update_rstn_by_default_connection(&g_default_connection);
-               g_free(g_default_connection.tether_iface.ifname);
-               g_default_connection.tether_iface.ifname = NULL;
-               g_default_connection.tether_iface.type = STC_IFACE_UNKNOWN;
+       if (g_default_connection->tether_state == FALSE && g_default_connection->tether_iface.ifname) {
+               stc_monitor_update_by_connection(&g_default_connection);
+               g_free(g_default_connection->tether_iface.ifname);
+               g_default_connection->tether_iface.ifname = NULL;
+               g_default_connection->tether_iface.type = STC_IFACE_UNKNOWN;
                STC_LOGI("Data monitoring stopped for tethering iface !");
                return;
        }
 }
 
-stc_error_e stc_default_connection_monitor_init(stc_s *stc)
+static void _service_signal_cb(GDBusConnection *connection,
+                              const gchar *name, const gchar *path,
+                              const gchar *interface, const gchar *sig,
+                              GVariant *param, gpointer user_data)
+{
+       gchar *sigvalue = NULL;
+       GVariant *variant = NULL;
+       stc_s *stc = (stc_s *)stc_get_manager();
+       ret_msg_if(stc == NULL, "failed to get stc data");
+
+       if (path == NULL || param == NULL)
+               goto done;
+
+       g_variant_get(param, "(sv)", &sigvalue, &variant);
+       if (sigvalue == NULL)
+               goto done;
+
+       if (g_strcmp0(sig, CONNMAN_SIGNAL_PROPERTY_CHANGED) != 0)
+               goto done;
+
+       if (g_strcmp0(sigvalue, "State") == 0 &&
+           g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
+               const gchar *state = NULL;
+
+               state = g_variant_get_string(variant, NULL);
+               if (g_strcmp0(state, "ready") == 0 ||
+                   g_strcmp0(state, "online") == 0) {
+                       GSList *comp = g_slist_find_custom(g_connection_list,
+                                                               path, __conn_list_comp);
+                       if (!comp)
+                               __append_connected_profile(stc->connection, path);
+               } else if (g_strcmp0(state, "idle") == 0 ||
+                       g_strcmp0(state, "disconnect") == 0) {
+                       GSList *comp = g_slist_find_custom(g_connection_list,
+                                                               path, __conn_list_comp);
+                       if (comp && comp->data)
+                               __remove_disconnected_profile(stc->connection, comp->data);
+               }
+       } else if (g_strcmp0(sigvalue, "Roaming") == 0) {
+               //LCOV_EXCL_START
+               GSList *comp = g_slist_find_custom(g_connection_list,
+                                                       path, __conn_list_comp);
+
+               if (comp) {
+                       stc_connection_s *conn = comp->data;
+                       gboolean roaming = 0;
+
+                       if (g_variant_is_of_type(variant, G_VARIANT_TYPE_BOOLEAN)) {
+                               roaming = g_variant_get_boolean(variant);
+                               conn->roaming = roaming;
+                       }
+               }
+               //LCOV_EXCL_STOP
+       } else {
+               ;//Do nothing
+       }
+done:
+       if (sigvalue)
+               g_free(sigvalue);
+
+       if (variant)
+               g_variant_unref(variant);
+
+       return;
+}
+
+stc_error_e stc_connection_monitor_init(stc_s *stc)
 {
        int ret;
        ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data");
 
-       __get_default_profile(stc->connection);
-       g_default_connection_sub_id =
+       __get_connected_profiles(stc->connection);
+       g_connection_sub_id =
                stc_manager_gdbus_subscribe_signal(stc->connection,
                                                   CONNMAN_SERVICE,
                                                   CONNMAN_SERVICE_INTERFACE,
@@ -589,23 +719,19 @@ stc_error_e stc_default_connection_monitor_init(stc_s *stc)
        return STC_ERROR_NONE;
 }
 
-stc_error_e stc_default_connection_monitor_deinit(stc_s *stc)
+stc_error_e stc_connection_monitor_deinit(stc_s *stc)
 {
        ret_value_msg_if(stc == NULL, STC_ERROR_INVALID_PARAMETER, "failed to get stc data");
 
        stc_manager_gdbus_unsubscribe_signal(stc->connection,
-                                            g_default_connection_sub_id);
-       FREE(g_default_connection.path);
-       FREE(g_default_connection.ifname);
-       return STC_ERROR_NONE;
-}
+                                            g_connection_sub_id);
 
-gchar *stc_default_connection_get_ifname(void)
-{
-       return g_strdup(g_default_connection.ifname);
+       g_slist_free_full(g_connection_list, __conn_list_free);
+
+       return STC_ERROR_NONE;
 }
 
-default_connection_s *stc_get_default_connection(void)
+GSList *stc_get_connection_list(void)
 {
-       return &g_default_connection;
+       return g_connection_list;
 }
diff --git a/src/monitor/stc-monitor-app.c b/src/monitor/stc-monitor-app.c
new file mode 100644 (file)
index 0000000..96d602a
--- /dev/null
@@ -0,0 +1,744 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#include "stc-db.h"
+#include "counter.h"
+#include "stc-manager.h"
+#include "stc-monitor.h"
+#include "stc-monitor-app.h"
+#include "stc-monitor-rstn.h"
+#include "stc-monitor-proc.h"
+#include "stc-monitor-ipt.h"
+#include "table-statistics.h"
+#include "helper-net-cls.h"
+#include "stc-manager-plugin-tether.h"
+
+static void __print_app(gpointer key, gpointer value,
+                                         gpointer data)
+{
+       stc_app_value_s *app_value = (stc_app_value_s *)value;
+
+       STC_LOGD("PkgID[%s] AppID[\033[0;32m%s\033[0;m] "
+               "type[%d] state[%d] classid[\033[1;36m%d\033[0;m] "
+               "counter[in(%lld) out(%lld)]",
+                app_value->pkg_id, app_value->app_id,
+                app_value->type, app_value->state,
+                app_value->classid,
+                app_value->data_usage.in_bytes,
+                app_value->data_usage.out_bytes);
+}
+
+static void __app_update_statistics(gpointer key,
+                                       gpointer value, gpointer data)
+{
+       stc_app_value_s *app_value = (stc_app_value_s *)value;
+       time_t *touch_time = (time_t *)data;
+       GSList *conn_list = stc_get_connection_list();
+
+       for (; conn_list != NULL; conn_list = conn_list->next) {
+               stc_connection_s *conn = conn_list->data;
+               stc_db_classid_iftype_key stat_key;
+               stc_db_app_stats stat;
+
+               memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
+               memset(&stat, 0 , sizeof(stc_db_app_stats));
+
+               if (conn == NULL)
+                       return;
+
+               /* Do not update statistics for Tethering
+                * if tethering is in-active found */
+               if (conn && conn->tether_state == FALSE &&
+                       !strcmp(app_value->app_id, STC_TOTAL_TETHERING))
+                       return;
+
+               /* Do not update statistics for Wi-Fi
+                * if tethering is active on wlan0 iface */
+               if (conn && conn->tether_state &&
+                       conn->tether_iface.type == STC_IFACE_WIFI &&
+                       !strcmp(app_value->app_id, STC_TOTAL_WIFI))
+                       return;
+
+               stat_key.classid = app_value->classid;
+
+               if (app_value->classid == STC_TETHERING_APP_CLASSID &&
+                       conn->tether_state == TRUE)
+                       stat_key.iftype = conn->tether_iface.type;
+               else if (g_str_has_suffix(app_value->app_id, STC_TETHERING_APP_SUFFIX))
+                       stat_key.iftype = conn->tether_iface.type;
+               else
+                       stat_key.iftype = conn->type;
+
+               if (STC_IFACE_DATACALL == stat_key.iftype)
+                       stat_key.subscriber_id = g_strdup(conn->subscriber_id);
+               else
+                       stat_key.subscriber_id = g_strdup(SUBSCRIBERID_NONE);
+
+               if (app_value->classid == STC_TETHERING_APP_CLASSID &&
+                       conn->tether_state == TRUE)
+                       g_strlcpy(stat_key.ifname, conn->tether_iface.ifname,
+                                 MAX_IFACE_LENGTH);
+               else if (g_str_has_suffix(app_value->app_id, STC_TETHERING_APP_SUFFIX))
+                       g_strlcpy(stat_key.ifname, conn->tether_iface.ifname,
+                                 MAX_IFACE_LENGTH);
+               else
+                       g_strlcpy(stat_key.ifname, conn->ifname,
+                                 MAX_IFACE_LENGTH);
+
+               stat.app_id = g_strdup(app_value->app_id);
+               stat.snd_count = app_value->counter.out_bytes;
+               stat.rcv_count = app_value->counter.in_bytes;
+               stat.is_roaming = conn->roaming;
+
+               if (strstr(stat.app_id, "_BACKGROUND")) {
+                       stat.ground = STC_APP_STATE_BACKGROUND;
+               } else {
+                       if (strstr(stat.app_id, "TOTAL_"))
+                               stat.ground = STC_APP_STATE_UNKNOWN;
+                       else
+                               stat.ground = STC_APP_STATE_FOREGROUND;
+               }
+
+               table_statistics_insert(&stat_key, &stat, *touch_time);
+
+               app_value->counter.out_bytes = 0;
+               app_value->counter.in_bytes = 0;
+
+               FREE(stat.app_id);
+               FREE(stat_key.subscriber_id);
+       }
+
+       return;
+}
+
+static gboolean __add_app_monitor_for_tethering(gpointer key,
+                                   gpointer value, gpointer data)
+{
+       stc_app_value_s *app_value = (stc_app_value_s *)value;
+       stc_connection_s *connection = (stc_connection_s *)data;
+       stc_s *stc = stc_get_manager();
+       struct nfacct_rule counter;
+       char *ipaddr = NULL;
+       int ret;
+
+       STC_LOGI("Add appid(%s) classid(%d)", app_value->app_id,
+                       app_value->classid);
+
+       if (stc == NULL || connection == NULL)
+               return FALSE;
+
+       if (!stc->carg) {
+               stc->carg = MALLOC0(counter_arg_s, 1);
+               if (stc->carg == NULL)
+                       return FALSE;
+
+               stc->carg->sock = stc_monitor_get_contr_sock();
+       }
+
+       memset(&counter, 0, sizeof(struct nfacct_rule));
+
+       counter.carg = stc->carg;
+       counter.classid = app_value->classid;
+       counter.intend = NFACCT_TETH_COUNTER;
+
+       if (connection->tether_state != TRUE ||
+                       connection->tether_iface.ifname == NULL)
+               return FALSE;
+
+       counter.iftype = connection->tether_iface.type;
+       g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
+
+       /* get the ip address of the station based on its mac address */
+       ret = stc_plugin_tether_get_station_ip(app_value->mac, &ipaddr);
+       if (ret != STC_ERROR_NONE)
+               return FALSE;
+
+       /* tethering iptables rule */
+       stc_monitor_tether_add_in(&counter, ipaddr);
+       stc_monitor_tether_add_out(&counter, ipaddr);
+
+       g_free(ipaddr);
+       return FALSE;
+}
+
+static gboolean __remove_app_monitor_for_tethering(gpointer key,
+                               gpointer value, gpointer data)
+{
+       stc_app_value_s *app_value = (stc_app_value_s *)value;
+       stc_connection_s *connection = (stc_connection_s *)data;
+       stc_s *stc = stc_get_manager();
+       struct nfacct_rule counter;
+       char *ipaddr = NULL;
+       int ret;
+
+       STC_LOGI("Remove appid(%s) classid(%d)", app_value->app_id,
+                       app_value->classid);
+
+       if (stc == NULL || connection == NULL)
+               return FALSE;
+
+       if (!stc->carg) {
+               stc->carg = MALLOC0(counter_arg_s, 1);
+               if (stc->carg == NULL)
+                       return FALSE;
+
+               stc->carg->sock = stc_monitor_get_contr_sock();
+       }
+
+       memset(&counter, 0, sizeof(struct nfacct_rule));
+
+       counter.carg = stc->carg;
+       counter.classid = app_value->classid;
+       counter.intend = NFACCT_TETH_COUNTER;
+
+       if (connection->tether_state != TRUE ||
+                       connection->tether_iface.ifname == NULL)
+               return FALSE;
+
+       counter.iftype = connection->tether_iface.type;
+       g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
+
+       /* get the ip address of the station based on its mac address */
+       ret = stc_plugin_tether_get_station_ip(app_value->mac, &ipaddr);
+       if (ret != STC_ERROR_NONE)
+               return FALSE;
+
+       stc_monitor_tether_del_in(&counter, ipaddr);
+       stc_monitor_tether_del_out(&counter, ipaddr);
+
+       g_free(ipaddr);
+       return FALSE;
+}
+
+static void __app_value_destroy(gpointer data)
+{
+       stc_app_value_s *app_value = (stc_app_value_s *)data;
+
+       FREE(app_value->pkg_id);
+       FREE(app_value->app_id);
+       g_hash_table_destroy(app_value->processes);
+       app_value->processes = NULL;
+
+       FREE(app_value);
+}
+
+static void __check_rstn_limit_exceeded(gpointer data,
+                               gpointer user_data)
+{
+       stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
+       int32_t *limit_exceeded = (int32_t *)user_data;
+
+       if (rstn_data->limit_exceeded != 0)
+               *limit_exceeded = rstn_data->limit_exceeded;
+}
+
+static void __app_update_counter(classid_bytes_context_s *context,
+                               uint32_t classid)
+{
+       stc_app_value_s *lookup_app;
+       stc_rstn_value_s *lookup_rstn;
+       GHashTable *apps = stc_monitor_get_system_apps();
+       GHashTable *rstns = stc_monitor_get_system_rstns();
+
+       if (!apps)
+               return;
+
+       lookup_app = g_hash_table_lookup(apps, GUINT_TO_POINTER(classid));
+       if (lookup_app)
+               stc_monitor_app_update_counter(lookup_app, context);
+
+       if (!rstns)
+               return;
+
+       lookup_rstn = g_hash_table_lookup(rstns, GUINT_TO_POINTER(classid));
+       if (lookup_rstn) {
+               int32_t limit_exceeded = 0;
+               g_slist_foreach(lookup_rstn->rules,
+                       __check_rstn_limit_exceeded, &limit_exceeded);
+
+               if (limit_exceeded != 0)
+                       return;
+       }
+}
+
+void stc_monitor_app_update_counter(stc_app_value_s *value,
+                                classid_bytes_context_s *context)
+{
+       switch (context->counter->iotype) {
+       case NFACCT_COUNTER_IN:
+               value->data_usage.in_bytes += context->bytes;
+               value->counter.in_bytes = context->bytes;
+               stc_monitor_set_apps_updated(TRUE);
+
+               break;
+       case NFACCT_COUNTER_OUT:
+               value->data_usage.out_bytes += context->bytes;
+               value->counter.out_bytes = context->bytes;
+               stc_monitor_set_apps_updated(TRUE);
+
+               break;
+       default:
+               STC_LOGE("Unknown iotype");
+       }
+}
+
+void stc_monitor_app_update_iface_counter(classid_bytes_context_s *context)
+{
+       switch (context->counter->iftype) {
+       case STC_IFACE_DATACALL:
+               __app_update_counter(context, STC_TOTAL_DATACALL_CLASSID);
+               break;
+       case STC_IFACE_WIFI:
+               __app_update_counter(context, STC_TOTAL_WIFI_CLASSID);
+               __app_update_counter(context, STC_TETHERING_APP_CLASSID);
+               break;
+       case STC_IFACE_BLUETOOTH:
+               __app_update_counter(context, STC_TOTAL_BLUETOOTH_CLASSID);
+               __app_update_counter(context, STC_TETHERING_APP_CLASSID);
+               break;
+       case STC_IFACE_USB:
+               __app_update_counter(context, STC_TETHERING_APP_CLASSID);
+               break;
+       case STC_IFACE_P2P:
+               __app_update_counter(context, STC_TETHERING_APP_CLASSID);
+               break;
+       default:
+               break;
+       }
+}
+
+gboolean stc_monitor_app_flush_stats_to_db(gpointer user_data)
+{
+       time_t current_time = 0;
+       stc_s *stc = stc_get_manager();
+       GHashTable *apps = stc_monitor_get_system_apps();
+       gboolean apps_updated = stc_monitor_get_apps_updated();
+
+       if (stc && stc->carg)
+               current_time = stc->carg->last_run_time;
+
+       if (apps_updated == FALSE)
+               return G_SOURCE_REMOVE;
+
+       stc_monitor_set_apps_updated(FALSE);
+
+       if (apps)
+               g_hash_table_foreach(apps,
+                                       __app_update_statistics,
+                                       &current_time);
+
+       STC_LOGI("Flushed app stats to database");
+       return G_SOURCE_REMOVE;
+}
+
+API stc_error_e stc_monitor_app_add(uint32_t classid,
+                               const char *app_id,
+                               const char *pkg_id,
+                               const stc_app_value_s value)
+{
+       stc_app_value_s *app_value;
+       stc_app_value_s *lookup_value;
+
+       GHashTable *apps = stc_monitor_get_system_apps();
+       GSList *conn_list = stc_get_connection_list();
+
+       if (!apps)
+               return STC_ERROR_NO_DATA;
+
+       if (classid == STC_UNKNOWN_CLASSID)
+               classid = get_classid_by_app_id(app_id, TRUE);
+
+       lookup_value = g_hash_table_lookup(apps, GUINT_TO_POINTER(classid));
+       if (lookup_value) {
+               if (STC_DEBUG_LOG)
+                       STC_LOGE("Already exists [\033[1;36m%d\033[0;m:"
+                                               "\033[0;32m%s\033[0;m]", classid, app_id);
+               return STC_ERROR_NONE;
+       }
+
+       app_value = MALLOC0(stc_app_value_s, 1);
+       if (!app_value) {
+               if (STC_DEBUG_LOG)
+                       STC_LOGE("Value allocation failed");
+               return STC_ERROR_OUT_OF_MEMORY;
+       }
+
+       /* create cgroup and update classid */
+       app_value->classid = classid;
+
+       app_value->app_id = g_strdup(app_id);
+       app_value->pkg_id = g_strdup(pkg_id);
+
+       app_value->type = value.type;
+       app_value->state = value.state;
+       app_value->data_usage.in_bytes = value.data_usage.in_bytes;
+       app_value->data_usage.out_bytes = value.data_usage.out_bytes;
+       g_strlcpy(app_value->mac, value.mac, MAC_ADDRESS_LEN);
+
+       app_value->processes = g_hash_table_new_full(g_direct_hash,
+                                                               g_direct_equal, NULL, NULL);
+
+       /* update classid for tethering station based on its mac address */
+       if (g_str_has_suffix(app_id, STC_TETHERING_APP_SUFFIX) &&
+                               classid != STC_TETHERING_APP_CLASSID)
+               stc_plugin_tether_set_station_classid(app_value->mac, classid);
+
+       g_hash_table_insert(apps, GUINT_TO_POINTER(classid), app_value);
+
+       for (; conn_list != NULL; conn_list = conn_list->next) {
+               stc_connection_s *conn = conn_list->data;
+               /* add nfacct rule for this classid */
+               stc_monitor_app_add_monitor(GUINT_TO_POINTER(classid),
+                                       app_value, conn);
+
+               if (app_value->state == STC_APP_STATE_FOREGROUND)
+                       stc_monitor_app_add_accept(GUINT_TO_POINTER(classid),
+                                       app_value, conn);
+       }
+
+       stc_monitor_rstn_add_for_app(classid);
+
+       if (STC_DEBUG_LOG) {
+               __print_app(GUINT_TO_POINTER(classid), app_value, NULL);
+               STC_LOGD("\033[1;32mApplication added\033[0;m "
+                       "[\033[1;36m%d\033[0;m]", classid);
+       }
+
+       return STC_ERROR_NONE;
+}
+
+void stc_monitor_app_add_by_iface(const char *ifname)
+{
+       stc_app_value_s app_value;
+
+       if (ifname == NULL)
+               return;
+
+       memset(&app_value, 0, sizeof(stc_app_value_s));
+
+       app_value.type = STC_APP_TYPE_NONE;
+       app_value.processes = NULL;
+       app_value.counter.in_bytes = 0;
+       app_value.counter.out_bytes = 0;
+
+       stc_monitor_app_add(STC_UNKNOWN_CLASSID, ifname, ifname, app_value);
+}
+
+void stc_monitor_app_add_monitor(gpointer key,
+                               gpointer value, gpointer data)
+{
+       stc_app_value_s *app_value = (stc_app_value_s *)value;
+       stc_connection_s *connection = (stc_connection_s *)data;
+       stc_s *stc = stc_get_manager();
+
+       if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
+           app_value->classid == STC_TOTAL_WIFI_CLASSID ||
+           app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
+               return;
+
+       if (stc && connection && connection->ifname) {
+               struct nfacct_rule counter;
+
+               if (!stc->carg) {
+                       stc->carg = MALLOC0(counter_arg_s, 1);
+                       if (stc->carg == NULL)
+                               return;
+
+                       stc->carg->sock = stc_monitor_get_contr_sock();
+               }
+
+               memset(&counter, 0, sizeof(struct nfacct_rule));
+
+               counter.carg = stc->carg;
+               counter.classid = app_value->classid;
+               counter.app_state = app_value->state;
+               counter.intend = NFACCT_COUNTER;
+
+               if (connection->tether_state == TRUE &&
+                       connection->tether_iface.ifname != NULL &&
+                       app_value->classid == STC_TETHERING_APP_CLASSID) {
+                       counter.iftype = connection->tether_iface.type;
+                       g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
+               } else {
+                       counter.iftype = connection->type;
+                       g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
+               }
+
+               if (g_str_has_suffix(app_value->app_id, STC_TETHERING_APP_SUFFIX) &&
+                               app_value->classid != STC_TETHERING_APP_CLASSID) {
+                       __add_app_monitor_for_tethering(key, value, data);
+               } else if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
+                       stc_monitor_ipt_add_in(&counter);
+                       stc_monitor_ipt_add_out(&counter);
+               } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
+                       stc_monitor_ip6t_add_in(&counter);
+                       stc_monitor_ip6t_add_out(&counter);
+               } else {
+                       stc_monitor_ipt_add_in(&counter);
+                       stc_monitor_ipt_add_out(&counter);
+                       stc_monitor_ip6t_add_in(&counter);
+                       stc_monitor_ip6t_add_out(&counter);
+               }
+       }
+}
+
+void stc_monitor_app_add_by_connection(stc_connection_s *conn)
+{
+       GHashTable *apps = stc_monitor_get_system_apps();
+
+       if (!apps)
+               return;
+
+       g_hash_table_foreach(apps, stc_monitor_app_add_monitor, conn);
+       g_hash_table_foreach(apps, stc_monitor_app_add_accept, conn);
+}
+
+void stc_monitor_app_add_accept(gpointer key,
+                               gpointer value, gpointer data)
+{
+       stc_app_value_s *app_value = (stc_app_value_s *)value;
+       stc_connection_s *connection = (stc_connection_s *)data;
+       stc_s *stc = stc_get_manager();
+
+       if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
+           app_value->classid == STC_TOTAL_WIFI_CLASSID ||
+           app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
+               return;
+
+       if (app_value->state != STC_APP_STATE_FOREGROUND)
+               return;
+
+       if (stc && connection && connection->ifname) {
+               struct nfacct_rule counter;
+
+               if (!stc->carg) {
+                       stc->carg = MALLOC0(counter_arg_s, 1);
+                       if (stc->carg == NULL)
+                               return;
+
+                       stc->carg->sock = stc_monitor_get_contr_sock();
+               }
+
+               memset(&counter, 0, sizeof(struct nfacct_rule));
+
+               counter.carg = stc->carg;
+               counter.classid = app_value->classid;
+               counter.app_state = app_value->state;
+               counter.intend = NFACCT_ALLOW;
+
+               counter.iftype = connection->type;
+               g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
+
+               if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
+                       stc_monitor_ipt_add_in(&counter);
+                       stc_monitor_ipt_add_out(&counter);
+               } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
+                       stc_monitor_ip6t_add_in(&counter);
+                       stc_monitor_ip6t_add_out(&counter);
+               } else {
+                       stc_monitor_ipt_add_in(&counter);
+                       stc_monitor_ipt_add_out(&counter);
+                       stc_monitor_ip6t_add_in(&counter);
+                       stc_monitor_ip6t_add_out(&counter);
+               }
+       }
+}
+
+API stc_error_e stc_monitor_app_remove(uint32_t classid, const char *app_id)
+{
+       stc_app_value_s *app_lookup;
+       GHashTable *apps = stc_monitor_get_system_apps();
+       GSList *conn_list = stc_get_connection_list();
+
+       if (!apps)
+               return STC_ERROR_NO_DATA;
+
+       classid = get_classid_by_app_id(app_id, FALSE);
+
+       app_lookup = g_hash_table_lookup(apps, GUINT_TO_POINTER(classid));
+       if (!app_lookup) {
+               if (STC_DEBUG_LOG)
+                       STC_LOGD("Application not found [\033[1;36m%d\033[0;m]", classid);
+               return STC_ERROR_FAIL;
+       }
+
+       for (; conn_list != NULL; conn_list = conn_list->next) {
+               stc_connection_s *conn = conn_list->data;
+               /* remove nfacct rule for this classid */
+               stc_monitor_app_remove_monitor(GUINT_TO_POINTER(classid),
+                                       app_lookup, conn);
+
+               if (app_lookup->state == STC_APP_STATE_FOREGROUND)
+                       stc_monitor_app_remove_accept(GUINT_TO_POINTER(classid),
+                                               app_lookup, conn);
+       }
+
+       /* remove ristrictions if any */
+       stc_monitor_rstn_remove_for_app(classid);
+
+       if (STC_DEBUG_LOG)
+               __print_app(GUINT_TO_POINTER(classid), app_lookup, NULL);
+
+       /* remove app_key from the stc-manager */
+       g_hash_table_remove(apps, GUINT_TO_POINTER(classid));
+
+       return STC_ERROR_NONE;
+}
+
+void stc_monitor_app_remove_monitor(gpointer key,
+                               gpointer value, gpointer data)
+{
+       stc_app_value_s *app_value = (stc_app_value_s *)value;
+       stc_connection_s *connection = (stc_connection_s *)data;
+       stc_s *stc = stc_get_manager();
+
+       if (stc && connection && connection->ifname) {
+               struct nfacct_rule counter;
+
+               if (!stc->carg) {
+                       stc->carg = MALLOC0(counter_arg_s, 1);
+                       if (stc->carg == NULL)
+                               return;
+
+                       stc->carg->sock = stc_monitor_get_contr_sock();
+               }
+
+               memset(&counter, 0, sizeof(struct nfacct_rule));
+
+               counter.carg = stc->carg;
+               counter.classid = app_value->classid;
+               counter.app_state = app_value->state;
+               counter.intend = NFACCT_COUNTER;
+
+               if (g_str_has_suffix(app_value->app_id, STC_TETHERING_APP_SUFFIX) &&
+                               app_value->classid != STC_TETHERING_APP_CLASSID) {
+                       __remove_app_monitor_for_tethering(key, value, data);
+                       return;
+               } else if (connection->tether_state == FALSE &&
+                       connection->tether_iface.ifname != NULL &&
+                       app_value->classid == STC_TETHERING_APP_CLASSID) {
+                       counter.iftype = connection->tether_iface.type;
+                       g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
+               } else {
+                       counter.iftype = connection->type;
+                       g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
+               }
+
+               if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
+                       stc_monitor_ipt_del_in(&counter);
+                       stc_monitor_ipt_del_out(&counter);
+               } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
+                       stc_monitor_ip6t_del_in(&counter);
+                       stc_monitor_ip6t_del_out(&counter);
+               } else {
+                       stc_monitor_ipt_del_in(&counter);
+                       stc_monitor_ipt_del_out(&counter);
+                       stc_monitor_ip6t_del_in(&counter);
+                       stc_monitor_ip6t_del_out(&counter);
+               }
+       }
+
+       return;
+}
+
+void stc_monitor_app_remove_by_connection(stc_connection_s *conn)
+{
+       GHashTable *apps = stc_monitor_get_system_apps();
+
+       if (!apps)
+               return;
+
+       g_hash_table_foreach(apps, stc_monitor_app_remove_monitor, conn);
+       g_hash_table_foreach(apps, stc_monitor_app_remove_accept, conn);
+}
+
+void stc_monitor_app_remove_accept(gpointer key,
+                               gpointer value, gpointer data)
+{
+       stc_app_value_s *app_value = (stc_app_value_s *)value;
+       stc_connection_s *connection = (stc_connection_s *)data;
+       stc_s *stc = stc_get_manager();
+
+       if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
+           app_value->classid == STC_TOTAL_WIFI_CLASSID ||
+           app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
+               return;
+
+       if (app_value->state != STC_APP_STATE_FOREGROUND)
+               return;
+
+       if (stc && connection && connection->ifname) {
+               struct nfacct_rule counter;
+
+               if (!stc->carg) {
+                       stc->carg = MALLOC0(counter_arg_s, 1);
+                       if (stc->carg == NULL)
+                               return;
+
+                       stc->carg->sock = stc_monitor_get_contr_sock();
+               }
+
+               memset(&counter, 0, sizeof(struct nfacct_rule));
+
+               counter.carg = stc->carg;
+               counter.classid = app_value->classid;
+               counter.app_state = app_value->state;
+               counter.intend = NFACCT_ALLOW;
+
+               counter.iftype = connection->type;
+               g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
+
+               if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
+                       stc_monitor_ipt_del_in(&counter);
+                       stc_monitor_ipt_del_out(&counter);
+               } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
+                       stc_monitor_ip6t_del_in(&counter);
+                       stc_monitor_ip6t_del_out(&counter);
+               } else {
+                       stc_monitor_ipt_del_in(&counter);
+                       stc_monitor_ipt_del_out(&counter);
+                       stc_monitor_ip6t_del_in(&counter);
+                       stc_monitor_ip6t_del_out(&counter);
+               }
+       }
+
+       return;
+}
+
+API gboolean stc_monitor_app_lookup(uint32_t classid)
+{
+       stc_app_value_s *lookup_value;
+
+       GHashTable *apps = stc_monitor_get_system_apps();
+
+       if (!apps)
+               return FALSE;
+
+       if (classid == STC_UNKNOWN_CLASSID)
+               return FALSE;
+
+       lookup_value = g_hash_table_lookup(apps, GUINT_TO_POINTER(classid));
+       if (lookup_value)
+               return TRUE;
+
+       return FALSE;
+}
+
+GHashTable *stc_monitor_apps_init(void)
+{
+       return g_hash_table_new_full(g_direct_hash, g_direct_equal,
+                                       NULL, __app_value_destroy);
+}
diff --git a/src/monitor/stc-monitor-ipt.c b/src/monitor/stc-monitor-ipt.c
new file mode 100644 (file)
index 0000000..016ae04
--- /dev/null
@@ -0,0 +1,250 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#include "stc-monitor-ipt.h"
+#include "stc-monitor-context.h"
+
+static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
+{
+       if (counter->intend == NFACCT_WARN)
+               return NFACCT_JUMP_ACCEPT;
+       else if (counter->intend == NFACCT_BLOCK)
+               return NFACCT_JUMP_REJECT;
+       else if (counter->intend == NFACCT_ALLOW)
+               return NFACCT_JUMP_ACCEPT;
+       else if (counter->intend == NFACCT_TETH_BLOCK)
+               return NFACCT_JUMP_REJECT;
+       else if (counter->intend == NFACCT_TETH_ALLOW)
+               return NFACCT_JUMP_ACCEPT;
+
+       return NFACCT_JUMP_UNKNOWN;
+}
+
+stc_error_e stc_monitor_ipt_add_in(struct nfacct_rule *counter)
+{
+       if (counter == NULL)
+               return STC_ERROR_INVALID_PARAMETER;
+
+       counter->action = NFACCT_ACTION_INSERT;
+       counter->iotype = NFACCT_COUNTER_IN;
+       counter->jump = __get_jump_by_intend(counter);
+       counter->iptype = NFACCT_TYPE_IPV4;
+       counter->send_limit = 0;
+       counter->rcv_limit = 0;
+
+       return produce_net_rule(counter);
+}
+
+stc_error_e stc_monitor_ipt_add_out(struct nfacct_rule *counter)
+{
+       if (counter == NULL)
+               return STC_ERROR_INVALID_PARAMETER;
+
+       counter->action = NFACCT_ACTION_INSERT;
+       counter->iotype = NFACCT_COUNTER_OUT;
+       counter->jump = __get_jump_by_intend(counter);
+       counter->iptype = NFACCT_TYPE_IPV4;
+       counter->send_limit = 0;
+       counter->rcv_limit = 0;
+
+       return produce_net_rule(counter);
+}
+
+stc_error_e stc_monitor_ipt_del_in(struct nfacct_rule *counter)
+{
+       if (counter == NULL)
+               return STC_ERROR_INVALID_PARAMETER;
+
+       counter->action = NFACCT_ACTION_DELETE;
+       counter->iotype = NFACCT_COUNTER_IN;
+       counter->jump = __get_jump_by_intend(counter);
+       counter->iptype = NFACCT_TYPE_IPV4;
+       counter->send_limit = 0;
+       counter->rcv_limit = 0;
+
+       return produce_net_rule(counter);
+}
+
+stc_error_e stc_monitor_ipt_del_out(struct nfacct_rule *counter)
+{
+       if (counter == NULL)
+               return STC_ERROR_INVALID_PARAMETER;
+
+       counter->action = NFACCT_ACTION_DELETE;
+       counter->iotype = NFACCT_COUNTER_OUT;
+       counter->jump = __get_jump_by_intend(counter);
+       counter->iptype = NFACCT_TYPE_IPV4;
+       counter->send_limit = 0;
+       counter->rcv_limit = 0;
+
+       return produce_net_rule(counter);
+}
+
+stc_error_e stc_monitor_ip6t_add_in(struct nfacct_rule *counter)
+{
+       if (counter == NULL)
+               return STC_ERROR_INVALID_PARAMETER;
+
+       counter->action = NFACCT_ACTION_INSERT;
+       counter->iotype = NFACCT_COUNTER_IN;
+       counter->jump = __get_jump_by_intend(counter);
+       counter->iptype = NFACCT_TYPE_IPV6;
+       counter->send_limit = 0;
+       counter->rcv_limit = 0;
+
+       return produce_net_rule(counter);
+}
+
+stc_error_e stc_monitor_ip6t_add_out(struct nfacct_rule *counter)
+{
+       if (counter == NULL)
+               return STC_ERROR_INVALID_PARAMETER;
+
+       counter->action = NFACCT_ACTION_INSERT;
+       counter->iotype = NFACCT_COUNTER_OUT;
+       counter->jump = __get_jump_by_intend(counter);
+       counter->iptype = NFACCT_TYPE_IPV6;
+       counter->send_limit = 0;
+       counter->rcv_limit = 0;
+
+       return produce_net_rule(counter);
+}
+
+stc_error_e stc_monitor_ip6t_del_in(struct nfacct_rule *counter)
+{
+       if (counter == NULL)
+               return STC_ERROR_INVALID_PARAMETER;
+
+       counter->action = NFACCT_ACTION_DELETE;
+       counter->iotype = NFACCT_COUNTER_IN;
+       counter->jump = __get_jump_by_intend(counter);
+       counter->iptype = NFACCT_TYPE_IPV6;
+       counter->send_limit = 0;
+       counter->rcv_limit = 0;
+
+       return produce_net_rule(counter);
+}
+
+stc_error_e stc_monitor_ip6t_del_out(struct nfacct_rule *counter)
+{
+       if (counter == NULL)
+               return STC_ERROR_INVALID_PARAMETER;
+
+       counter->action = NFACCT_ACTION_DELETE;
+       counter->iotype = NFACCT_COUNTER_OUT;
+       counter->jump = __get_jump_by_intend(counter);
+       counter->iptype = NFACCT_TYPE_IPV6;
+       counter->send_limit = 0;
+       counter->rcv_limit = 0;
+
+       return produce_net_rule(counter);
+}
+
+stc_error_e stc_monitor_tether_add_in(struct nfacct_rule *counter,
+               const gchar *ipaddr)
+{
+       int ret;
+
+       if (counter == NULL || ipaddr == NULL)
+               return STC_ERROR_INVALID_PARAMETER;
+
+       counter->action = NFACCT_ACTION_INSERT;
+       counter->iotype = NFACCT_COUNTER_IN;
+       counter->jump = __get_jump_by_intend(counter);
+       counter->iptype = NFACCT_TYPE_IPV4;
+       counter->send_limit = 0;
+       counter->rcv_limit = 0;
+       counter->src_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
+       counter->src_ip1 = g_strdup(ipaddr);
+
+       ret = produce_net_rule(counter);
+
+       FREE(counter->src_ip1);
+       counter->src_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
+       return ret;
+}
+
+stc_error_e stc_monitor_tether_add_out(struct nfacct_rule *counter,
+               const gchar *ipaddr)
+{
+       int ret;
+
+       if (counter == NULL || ipaddr == NULL)
+               return STC_ERROR_INVALID_PARAMETER;
+
+       counter->action = NFACCT_ACTION_INSERT;
+       counter->iotype = NFACCT_COUNTER_OUT;
+       counter->jump = __get_jump_by_intend(counter);
+       counter->iptype = NFACCT_TYPE_IPV4;
+       counter->send_limit = 0;
+       counter->rcv_limit = 0;
+       counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
+       counter->dst_ip1 = g_strdup(ipaddr);
+
+       ret = produce_net_rule(counter);
+
+       FREE(counter->dst_ip1);
+       counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
+       return ret;
+}
+
+stc_error_e stc_monitor_tether_del_in(struct nfacct_rule *counter,
+               const gchar *ipaddr)
+{
+       int ret;
+
+       if (counter == NULL || ipaddr == NULL)
+               return STC_ERROR_INVALID_PARAMETER;
+
+       counter->action = NFACCT_ACTION_DELETE;
+       counter->iotype = NFACCT_COUNTER_IN;
+       counter->jump = __get_jump_by_intend(counter);
+       counter->iptype = NFACCT_TYPE_IPV4;
+       counter->send_limit = 0;
+       counter->rcv_limit = 0;
+       counter->src_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
+       counter->src_ip1 = g_strdup(ipaddr);
+
+       ret = produce_net_rule(counter);
+
+       FREE(counter->src_ip1);
+       counter->src_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
+       return ret;
+}
+
+stc_error_e stc_monitor_tether_del_out(struct nfacct_rule *counter,
+               const gchar *ipaddr)
+{
+       int ret;
+
+       if (counter == NULL || ipaddr == NULL)
+               return STC_ERROR_INVALID_PARAMETER;
+
+       counter->action = NFACCT_ACTION_DELETE;
+       counter->iotype = NFACCT_COUNTER_OUT;
+       counter->jump = __get_jump_by_intend(counter);
+       counter->iptype = NFACCT_TYPE_IPV4;
+       counter->send_limit = 0;
+       counter->rcv_limit = 0;
+       counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
+       counter->dst_ip1 = g_strdup(ipaddr);
+
+       ret = produce_net_rule(counter);
+
+       FREE(counter->dst_ip1);
+       counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
+       return ret;
+}
diff --git a/src/monitor/stc-monitor-proc.c b/src/monitor/stc-monitor-proc.c
new file mode 100644 (file)
index 0000000..e472421
--- /dev/null
@@ -0,0 +1,312 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#include "stc-monitor.h"
+#include "stc-monitor-proc.h"
+#include "stc-connection.h"
+#include "helper-net-cls.h"
+
+static void __print_proc(gpointer key, gpointer value,
+                               gpointer data)
+{
+       stc_proc_value_s *proc_value = (stc_proc_value_s *)value;
+
+       STC_LOGD("pid[\033[1;33m%d\033[0;m] ground[%d]",
+                proc_value->pid, proc_value->ground);
+}
+
+static void __print_proc_all(GHashTable *procs)
+{
+       g_hash_table_foreach(procs, __print_proc, NULL);
+}
+
+static void __proc_remove_pid(gpointer key, gpointer value,
+                               gpointer data)
+{
+       stc_app_value_s *app_value = (stc_app_value_s *)value;
+       remove_pid_context_s *context = (remove_pid_context_s *)data;
+
+       if (!g_hash_table_remove(app_value->processes, GUINT_TO_POINTER(context->pid)))
+               return;
+
+       if (STC_DEBUG_LOG) {
+               __print_proc_all(app_value->processes);
+               STC_LOGD("\033[1;31mProcess removed\033[0;m "
+                       "[\033[1;33m%d\033[0;m]", context->pid);
+       }
+
+       context->entry_removed = TRUE;
+       context->app_value = app_value;
+}
+
+static void __proc_move_pid(gpointer key, gpointer value,
+                               gpointer data)
+{
+       stc_proc_value_s *from_value = (stc_proc_value_s *)value;
+       stc_app_value_s *to_app_value = (stc_app_value_s *)data;
+       stc_proc_value_s *to_value;
+
+       to_value = MALLOC0(stc_proc_value_s, 1);
+       if (!to_value)
+               return;
+
+       to_value->pid = from_value->pid;
+       to_value->ground = to_app_value->state;
+
+       g_hash_table_insert(to_app_value->processes,
+                       GUINT_TO_POINTER(to_value->pid),
+                       to_value);
+
+       /* add pid to application cgroup */
+       place_pids_to_net_cgroup(to_value->pid, to_app_value->app_id);
+}
+
+API stc_error_e stc_monitor_proc_update_ground(uint32_t classid,
+                                               const char *app_id,
+                                               const stc_proc_value_s value)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+       stc_app_value_s *app_lookup;
+       stc_proc_value_s *proc_lookup;
+       GHashTable *apps = stc_monitor_get_system_apps();
+
+       if (!apps)
+               return STC_ERROR_NO_DATA;
+
+       if (classid == STC_UNKNOWN_CLASSID)
+               classid = get_classid_by_app_id(app_id, TRUE);
+
+       app_lookup = g_hash_table_lookup(apps, GUINT_TO_POINTER(classid));
+       if (!app_lookup) {
+               if (STC_DEBUG_LOG)
+                       STC_LOGD("Application not found [\033[1;36m%d\033[0;m]", classid);
+               return STC_ERROR_NO_DATA;
+       }
+
+       proc_lookup = g_hash_table_lookup(app_lookup->processes,
+                                       GUINT_TO_POINTER(value.pid));
+       if (!proc_lookup) {
+               if (STC_DEBUG_LOG)
+                       STC_LOGD("Process not found [\033[1;33m%d\033[0;m]", value.pid);
+               return STC_ERROR_NO_DATA;
+       }
+
+       if (proc_lookup->ground != value.ground)
+               proc_lookup->ground = value.ground;
+
+       place_pids_to_net_cgroup(value.pid, app_id);
+
+       return ret;
+}
+
+API stc_error_e stc_monitor_proc_add(uint32_t classid,
+                                       const char *app_id,
+                                       const stc_proc_value_s value)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+       stc_app_value_s *app_lookup;
+       stc_proc_value_s *proc_lookup;
+       stc_proc_value_s *proc_value;
+       GHashTable *apps = stc_monitor_get_system_apps();
+
+       if (!apps)
+               return STC_ERROR_NO_DATA;
+
+       if (classid == STC_UNKNOWN_CLASSID)
+               classid = get_classid_by_app_id(app_id, TRUE);
+
+       app_lookup = g_hash_table_lookup(apps, GUINT_TO_POINTER(classid));
+       if (!app_lookup) {
+               if (STC_DEBUG_LOG)
+                       STC_LOGD("Application not found [\033[1;36m%d\033[0;m]", classid);
+               return STC_ERROR_NO_DATA;
+       }
+
+       proc_lookup = g_hash_table_lookup(app_lookup->processes,
+                                       GUINT_TO_POINTER(value.pid));
+       if (proc_lookup) {
+               if (STC_DEBUG_LOG)
+                       STC_LOGE("Already exists [\033[1;36m%d\033[0;m]", value.pid);
+               return STC_ERROR_NONE;
+       }
+
+       proc_value = MALLOC0(stc_proc_value_s, 1);
+       if (!proc_value) {
+               if (STC_DEBUG_LOG)
+                       STC_LOGE("Value allocation failed");
+               return STC_ERROR_OUT_OF_MEMORY;
+       }
+
+       proc_value->pid = value.pid;
+       proc_value->ground = value.ground;
+
+       g_hash_table_insert(app_lookup->processes,
+                       GUINT_TO_POINTER(proc_value->pid),
+                       proc_value);
+
+       /* add pid to application cgroup */
+       place_pids_to_net_cgroup(proc_value->pid, app_id);
+
+       if (STC_DEBUG_LOG) {
+               __print_proc_all(app_lookup->processes);
+               STC_LOGD("\033[1;32mProcess added\033[0;m "
+                       "[\033[1;33m%d\033[0;m]", proc_value->pid);
+       }
+
+       return ret;
+}
+
+API stc_error_e stc_monitor_proc_remove(uint32_t classid,
+                                               pid_t pid)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+       stc_app_value_s *app_lookup = NULL;
+       guint pid_count = 0;
+       GHashTable *apps = stc_monitor_get_system_apps();
+       GSList *conn_list = stc_get_connection_list();
+       remove_pid_context_s context = {
+               .pid = pid,
+               .app_value = NULL,
+               .entry_removed = FALSE,
+       };
+
+       if (!apps)
+               return STC_ERROR_NO_DATA;
+
+       if (classid == STC_UNKNOWN_CLASSID) {
+               g_hash_table_foreach(apps, __proc_remove_pid, &context);
+
+               if (context.entry_removed)
+                       app_lookup = context.app_value;
+
+               if (!app_lookup) {
+                       if (STC_DEBUG_LOG)
+                               STC_LOGD("Process not found [\033[1;33m%d\033[0;m]", pid);
+                       return STC_ERROR_NO_DATA;
+               }
+       } else {
+               app_lookup = g_hash_table_lookup(apps, GUINT_TO_POINTER(classid));
+               if (!app_lookup) {
+                       if (STC_DEBUG_LOG)
+                               STC_LOGD("Application not found [\033[1;36m%d\033[0;m]",
+                                                       classid);
+                       return STC_ERROR_NO_DATA;
+               }
+
+               if (g_hash_table_remove(app_lookup->processes, GUINT_TO_POINTER(pid))) {
+                       if (STC_DEBUG_LOG) {
+                               __print_proc_all(app_lookup->processes);
+                               STC_LOGD("\033[1;31mProcess removed\033[0;m "
+                                       "[\033[1;33m%d\033[0;m]", pid);
+                       }
+               } else {
+                       STC_LOGD("Process not found [\033[1;33m%d\033[0;m]", pid);
+               }
+       }
+
+       pid_count = g_hash_table_size(app_lookup->processes);
+
+       if (!pid_count) {
+               for (; conn_list != NULL; conn_list = conn_list->next) {
+                       stc_connection_s *conn = conn_list->data;
+                       /* remove nfacct rule for this classid */
+                       stc_monitor_app_remove_monitor(GUINT_TO_POINTER(classid),
+                                               app_lookup, conn);
+
+                       if (app_lookup->state == STC_APP_STATE_FOREGROUND)
+                               stc_monitor_app_remove_accept(GUINT_TO_POINTER(classid),
+                                               app_lookup, conn);
+               }
+
+               stc_monitor_rstn_remove_for_app(classid);
+
+               g_hash_table_remove(apps, GUINT_TO_POINTER(classid));
+
+               if (STC_DEBUG_LOG)
+                       STC_LOGD("\033[1;31mApplication removed\033[0;m "
+                               "[\033[1;36m%d\033[0;m]", classid);
+       }
+
+       return ret;
+}
+
+API stc_error_e stc_monitor_proc_move(uint32_t from,
+                                               uint32_t to)
+{
+       stc_error_e ret = STC_ERROR_NONE;
+       stc_app_value_s *from_lookup = NULL;
+       stc_app_value_s *to_lookup = NULL;
+       GSList *conn_list = stc_get_connection_list();
+       GHashTable *apps = stc_monitor_get_system_apps();
+
+       if (!apps)
+               return STC_ERROR_NO_DATA;
+
+       if (from != STC_UNKNOWN_CLASSID &&
+               to != STC_UNKNOWN_CLASSID) {
+               from_lookup = g_hash_table_lookup(apps, GUINT_TO_POINTER(from));
+               if (!from_lookup) {
+                       if (STC_DEBUG_LOG)
+                               STC_LOGD("Application not found [\033[1;36m%d\033[0;m]", from);
+                       return STC_ERROR_NO_DATA;
+               }
+
+               to_lookup = g_hash_table_lookup(apps, GUINT_TO_POINTER(to));
+               if (!to_lookup) {
+                       if (STC_DEBUG_LOG)
+                               STC_LOGD("Application not found [\033[1;36m%d\033[0;m]", to);
+                       return STC_ERROR_NO_DATA;
+               }
+
+               g_hash_table_foreach(from_lookup->processes,
+                               __proc_move_pid, to_lookup);
+
+               if (STC_DEBUG_LOG) {
+                       __print_proc_all(to_lookup->processes);
+                       STC_LOGD("\033[1;34mProcesses moved\033[0;m "
+                               "[\033[1;36m%d\033[0;m] -> [\033[1;36m%d\033[0;m]",
+                               from, to);
+               }
+
+               g_hash_table_remove_all(from_lookup->processes);
+
+               if (STC_DEBUG_LOG) {
+                       __print_proc_all(from_lookup->processes);
+                       STC_LOGD("\033[1;31mProcesses removed\033[0;m "
+                               "[\033[1;36m%d\033[0;m]", from);
+               }
+
+               for (; conn_list != NULL; conn_list = conn_list->next) {
+                       stc_connection_s *conn = conn_list->data;
+                       stc_monitor_app_remove_monitor(GUINT_TO_POINTER(from),
+                                               from_lookup, conn);
+
+                       if (from_lookup->state == STC_APP_STATE_FOREGROUND)
+                               stc_monitor_app_remove_accept(GUINT_TO_POINTER(from),
+                                               from_lookup, conn);
+               }
+
+               stc_monitor_rstn_remove_for_app(from);
+
+               g_hash_table_remove(apps, GUINT_TO_POINTER(from));
+
+               if (STC_DEBUG_LOG)
+                       STC_LOGD("\033[1;31mApplication removed\033[0;m "
+                               "[\033[1;36m%d\033[0;m]", from);
+       }
+
+       return ret;
+}
diff --git a/src/monitor/stc-monitor-rstn.c b/src/monitor/stc-monitor-rstn.c
new file mode 100644 (file)
index 0000000..f28de3c
--- /dev/null
@@ -0,0 +1,1465 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * 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.
+ */
+
+#include <vconf.h>
+#include <vconf-keys.h>
+
+#include "counter.h"
+#include "stc-monitor.h"
+#include "stc-monitor-rstn.h"
+#include "stc-monitor-ipt.h"
+#include "stc-time.h"
+#include "table-counters.h"
+#include "table-restrictions.h"
+#include "table-statistics.h"
+#include "helper-net-cls.h"
+#include "stc-manager-plugin-appstatus.h"
+#include "stc-manager-plugin-tether.h"
+
+static void __print_rstn(stc_rstn_data_s *rstn_data)
+{
+       STC_LOGI("RstnID[%llu] AppID[%s] classid[%u] "
+               "iftype[%d] ifname[%s] rstn_state[%d] "
+               "rstn_type[%d] roaming[%d] subscriber_id[%s]",
+                rstn_data->restriction_id,
+                rstn_data->app_id, rstn_data->classid,
+                rstn_data->iftype, rstn_data->ifname,
+                rstn_data->rstn_state, rstn_data->rstn_type,
+                rstn_data->roaming, rstn_data->subscriber_id);
+       STC_LOGI("month_start_date[%d] limit[%lld] "
+               "warn_limit[%lld] monthly_limit[%lld] "
+               "weekly_limit[%lld] daily_limit[%lld] ",
+               rstn_data->month_start_date,
+               rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA],
+               rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN],
+               rstn_data->limit[STC_RSTN_LIMIT_TYPE_MONTHLY],
+               rstn_data->limit[STC_RSTN_LIMIT_TYPE_WEEKLY],
+               rstn_data->limit[STC_RSTN_LIMIT_TYPE_DAILY]);
+       STC_LOGI("data_counter[%lld] warn_counter[%lld] "
+               "monthly_counter[%lld] weekly_counter[%lld] "
+               "daily_counter[%lld]",
+               rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA],
+               rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
+               rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
+               rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
+               rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY]);
+}
+
+static int __vconf_get_int(const char *key, int *value)
+{
+       int ret = 0;
+
+       ret = vconf_get_int(key, value);
+       if (ret != VCONF_OK) {
+               STC_LOGE("Failed to get vconfkey [%s] value", key);
+               return -1;
+       }
+
+       return 0;
+}
+
+static int __vconf_set_int(const char *key, int value)
+{
+       int ret = 0;
+
+       ret = vconf_set_int(key, value);
+       if (ret != VCONF_OK) {
+               STC_LOGE("Failed to set vconfkey [%s] value", key);
+               return -1;
+       }
+
+       return 0;
+}
+
+static stc_cb_ret_e __statistics_info_cb(const table_statistics_info *info,
+                                       void *user_data)
+{
+       stc_rstn_cumulative_data_s *stat = (stc_rstn_cumulative_data_s *)user_data;
+       int64_t counters = 0;
+
+       counters = info->cnt.in_bytes + info->cnt.out_bytes;
+
+       stat->monthly_stat += counters;
+       if (stat->week_start_ts <= info->interval->from)
+               stat->weekly_stat += counters;
+       if (stat->day_start_ts <= info->interval->from)
+               stat->daily_stat += counters;
+
+       return STC_CONTINUE;
+}
+
+static void __rstn_add_tether_rule(int64_t classid, gchar *mac,
+               nfacct_rule_intend intend, stc_iface_type_e iftype)
+{
+       GSList *conn_list = stc_get_connection_list();
+       struct nfacct_rule counter;
+       stc_s *stc = stc_get_manager();
+       char *ipaddr = NULL;
+       int ret;
+
+       if (!stc || !mac)
+               return;
+
+       for (; conn_list != NULL; conn_list = conn_list->next) {
+               stc_connection_s *conn = conn_list->data;
+
+               if (!stc->carg) {
+                       stc->carg = MALLOC0(counter_arg_s, 1);
+                       if (stc->carg == NULL)
+                               return;
+
+                       stc->carg->sock = stc_monitor_get_contr_sock();
+               }
+
+               memset(&counter, 0, sizeof(struct nfacct_rule));
+
+               counter.carg = stc->carg;
+               counter.classid = classid;
+               counter.intend = intend;
+
+               if (conn->tether_state != TRUE ||
+                       conn->tether_iface.ifname == NULL)
+                       return;
+
+               counter.iftype = conn->tether_iface.type;
+               g_strlcpy(counter.ifname, conn->tether_iface.ifname, MAX_IFACE_LENGTH);
+
+               /* get connected station ip based on its mac */
+               ret = stc_plugin_tether_get_station_ip(mac, &ipaddr);
+               if (ret != STC_ERROR_NONE)
+                       return;
+
+               /* tethering iptables rule */
+               stc_monitor_tether_add_in(&counter, ipaddr);
+               stc_monitor_tether_add_out(&counter, ipaddr);
+               g_free(ipaddr);
+       }
+}
+
+static void __rstn_del_tether_rule(int64_t classid, gchar *mac,
+               nfacct_rule_intend intend, stc_iface_type_e iftype)
+{
+       GSList *conn_list = stc_get_connection_list();
+       struct nfacct_rule counter;
+       stc_s *stc = stc_get_manager();
+       char *ipaddr = NULL;
+       int ret;
+
+       if (!stc || !mac)
+               return;
+
+       for (; conn_list != NULL; conn_list = conn_list->next) {
+               stc_connection_s *conn = conn_list->data;
+
+               if (!stc->carg) {
+                       stc->carg = MALLOC0(counter_arg_s, 1);
+                       if (stc->carg == NULL)
+                               return;
+
+                       stc->carg->sock = stc_monitor_get_contr_sock();
+               }
+
+               memset(&counter, 0, sizeof(struct nfacct_rule));
+
+               counter.carg = stc->carg;
+               counter.classid = classid;
+               counter.intend = intend;
+
+               if (conn->tether_state != TRUE ||
+                       conn->tether_iface.ifname == NULL)
+                       return;
+
+               counter.iftype = conn->tether_iface.type;
+               g_strlcpy(counter.ifname, conn->tether_iface.ifname, MAX_IFACE_LENGTH);
+
+               /* get connected station ip based on its mac */
+               ret = stc_plugin_tether_get_station_ip(mac, &ipaddr);
+               if (ret != STC_ERROR_NONE) {
+                       STC_LOGE("Error: no IP found for station mac(%s)", mac);
+                       return;
+               }
+
+               /* tethering iptables rule */
+               stc_monitor_tether_del_in(&counter, ipaddr);
+               stc_monitor_tether_del_out(&counter, ipaddr);
+               g_free(ipaddr);
+       }
+}
+
+static void __rstn_add_ipt_rule(int64_t classid, nfacct_rule_intend intend,
+                               stc_iface_type_e iftype)
+{
+       GSList *conn_list = stc_get_connection_list();
+       struct nfacct_rule counter;
+       stc_s *stc = stc_get_manager();
+
+       if (!stc)
+               return;
+
+       for (; conn_list != NULL; conn_list = conn_list->next) {
+               stc_connection_s *conn = conn_list->data;
+
+               if (conn == NULL)
+                       return;
+
+               if (!stc->carg) {
+                       stc->carg = MALLOC0(counter_arg_s, 1);
+                       if (stc->carg == NULL)
+                               return;
+
+                       stc->carg->sock = stc_monitor_get_contr_sock();
+               }
+
+               memset(&counter, 0, sizeof(struct nfacct_rule));
+
+               counter.carg = stc->carg;
+               counter.classid = classid;
+               counter.intend = intend;
+
+               if (conn && conn->tether_iface.ifname != NULL &&
+                       classid == STC_TETHERING_APP_CLASSID) {
+                       counter.iftype = conn->tether_iface.type;
+                       g_strlcpy(counter.ifname, conn->tether_iface.ifname, MAX_IFACE_LENGTH);
+               } else {
+                       counter.iftype = iftype;
+                       g_strlcpy(counter.ifname, conn->ifname, MAX_IFACE_LENGTH);
+               }
+
+               /* iptables rule */
+               stc_monitor_ipt_add_in(&counter);
+               stc_monitor_ipt_add_out(&counter);
+
+               /* ip6tables rule */
+               stc_monitor_ip6t_add_in(&counter);
+               stc_monitor_ip6t_add_out(&counter);
+       }
+}
+
+static void __rstn_del_ipt_rule(int64_t classid, nfacct_rule_intend intend,
+                               stc_iface_type_e iftype)
+{
+       GSList *conn_list = stc_get_connection_list();
+       struct nfacct_rule counter;
+       stc_s *stc = stc_get_manager();
+
+       if (!stc)
+               return;
+
+       for (; conn_list != NULL; conn_list = conn_list->next) {
+               stc_connection_s *conn = conn_list->data;
+
+               if (conn == NULL)
+                       return;
+
+               if (!stc->carg) {
+                       stc->carg = MALLOC0(counter_arg_s, 1);
+                       if (stc->carg == NULL)
+                               return;
+
+                       stc->carg->sock = stc_monitor_get_contr_sock();
+               }
+
+               memset(&counter, 0, sizeof(struct nfacct_rule));
+
+               counter.carg = stc->carg;
+               counter.classid = classid;
+               counter.intend = intend;
+
+               if (conn && conn->tether_iface.ifname != NULL &&
+                       classid == STC_TETHERING_APP_CLASSID) {
+                       counter.iftype = conn->tether_iface.type;
+                       g_strlcpy(counter.ifname, conn->tether_iface.ifname, MAX_IFACE_LENGTH);
+               } else {
+                       counter.iftype = iftype;
+                       g_strlcpy(counter.ifname, conn->ifname, MAX_IFACE_LENGTH);
+               }
+
+               /* iptables rule */
+               stc_monitor_ipt_del_in(&counter);
+               stc_monitor_ipt_del_out(&counter);
+
+               /* ip6tables rule */
+               stc_monitor_ip6t_del_in(&counter);
+               stc_monitor_ip6t_del_out(&counter);
+       }
+}
+
+static void __rstn_set_noti_state(int value)
+{
+       int state = STC_RSTN_STATE_INIT;
+
+       if (__vconf_get_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, &state))
+               return;
+
+       if (state == value) {
+               STC_LOGI("No need to change a restriction status: %d", state);
+               return;
+       }
+
+       __vconf_set_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, value);
+}
+
+static void __rstn_tethering_process(enum traffic_restriction_type rstn_type,
+                               char *app_id, stc_rstn_data_s *rstn_data, void *data)
+{
+       stc_connection_s *old_connection = (stc_connection_s *)data;
+       stc_connection_s *connection = NULL;
+       char *mac_str = NULL;
+
+       if (old_connection != NULL) {
+               connection = old_connection;
+               if (connection->tether_state == FALSE)
+                       return;
+
+               /* rstn not applicable for this interface */
+               if (rstn_data->ifname != NULL &&
+                       g_strcmp0("", rstn_data->ifname) != 0 &&
+                       g_strcmp0(connection->tether_iface.ifname, rstn_data->ifname) != 0)
+                       return;
+       } else {
+               GSList *conn_list = stc_get_connection_list();
+               for (; conn_list != NULL; conn_list = conn_list->next) {
+                       stc_connection_s *conn = conn_list->data;
+                       if (conn->tether_state == FALSE)
+                               continue;
+
+                       if (rstn_data->ifname != NULL &&
+                               g_strcmp0(rstn_data->ifname, "") != 0 &&
+                               g_strcmp0(conn->tether_iface.ifname, rstn_data->ifname) == 0)
+                               connection = conn;
+               }
+
+               if (connection == NULL)
+                       return;
+       }
+
+       /* in case appid not a tethering app */
+       if (!g_str_has_suffix(app_id, STC_TETHERING_APP_SUFFIX))
+               return;
+
+       /* Ignore TOTAL_TETHERING,
+        * Process only station appids */
+       if (rstn_data->classid == STC_TETHERING_APP_CLASSID)
+               return;
+
+       /* get the station mac based on classid */
+       stc_plugin_tether_get_station_by_classid(rstn_data->classid, &mac_str);
+       if (!mac_str) {
+               STC_LOGE("Station not found for classid(%d)", rstn_data->classid);
+               return;
+       }
+
+       switch (rstn_type) {
+       case RST_SET:
+       {
+               int i;
+               table_counters_info info;
+               int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
+
+                       memset(&info, 0, sizeof(table_counters_info));
+                       rstn_data->limit_exceeded = 0;
+
+                       if ((rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
+                                               rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
+                                       (rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
+                                        rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
+                                       (rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
+                                        rstn_data->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
+                                       (rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
+                                        rstn_data->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
+                                       (rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
+                                        rstn_data->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
+                               table_counters_get(rstn_data->restriction_id, &info);
+
+                               time_t current_time = 0;
+                               stc_rstn_cumulative_data_s stat;
+                               table_statistics_select_rule rule;
+                               time_t last_week_ts = stc_monitor_get_last_week_ts();
+                               time_t last_day_ts = stc_monitor_get_last_day_ts();
+
+                               memset(&stat, 0, sizeof(stc_rstn_cumulative_data_s));
+                               stat.month_start_ts = rstn_data->month_start_ts;
+                               stat.week_start_ts = last_week_ts;
+                               stat.day_start_ts = last_day_ts;
+
+                               memset(&rule, 0, sizeof(table_statistics_select_rule));
+                               rule.from = rstn_data->month_start_ts;
+                               time(&current_time);
+                               rule.to = current_time;
+                               rule.iftype = rstn_data->iftype;
+                               rule.granularity = GRANULARITY;
+
+                               table_statistics_per_app(app_id, &rule, __statistics_info_cb, &stat);
+
+                               rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
+                               rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
+                               rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
+                               rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
+                               rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
+                       }
+
+                       for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
+                               if (rstn_data->limit[i] >= 0) {
+                                       effective_limit[i] = rstn_data->limit[i] - rstn_data->counter[i];
+
+                                       if (effective_limit[i] < 0)
+                                               rstn_data->limit_exceeded |= (1 << i);
+                               }
+                       }
+
+                       STC_LOGD("Rstn_id[%llu] datausage[%lld]bytes",
+                                       rstn_data->restriction_id, info.data_counter);
+
+                       if (rstn_data->limit_exceeded != 0 &&
+                                       rstn_data->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
+                               __rstn_add_tether_rule(rstn_data->classid, mac_str,
+                                               NFACCT_TETH_BLOCK, rstn_data->iftype);
+                       }
+
+                       rstn_data->rstn_state = STC_RSTN_STATE_ACTIVATED;
+       }
+       break;
+       case RST_EXCLUDE:
+       {
+               __rstn_add_tether_rule(rstn_data->classid, mac_str,
+                                       NFACCT_TETH_ALLOW, rstn_data->iftype);
+
+                       rstn_data->rstn_state = STC_RSTN_STATE_ACTIVATED;
+                       rstn_data->limit_exceeded = 0;
+                       rstn_data->limit_notified = 0;
+       }
+       break;
+       case RST_UNSET:
+       {
+                       int i;
+                       __rstn_del_tether_rule(rstn_data->classid, mac_str,
+                                       NFACCT_TETH_BLOCK, rstn_data->iftype);
+
+                       rstn_data->rstn_state = STC_RSTN_STATE_DEACTIVATED;
+                       rstn_data->limit_exceeded = 0;
+                       rstn_data->limit_notified = 0;
+
+                       for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
+                               if (rstn_data->limit[i] >= 0)
+                                       rstn_data->counter[i] = 0;
+       }
+       break;
+       default:
+               break;
+       }
+       FREE(mac_str);
+}
+
+static void __rstn_process(enum traffic_restriction_type rstn_type,
+                                 char *app_id, stc_rstn_data_s *rstn_data, void *data)
+{
+       stc_connection_s *old_connection = (stc_connection_s *)data;
+       stc_connection_s *connection = NULL;
+
+       if (old_connection != NULL) {
+               connection = old_connection;
+               if (connection->ifname == NULL)
+                       return;
+
+               /* rstn not applicable for this interface */
+               if (rstn_data->ifname != NULL &&
+                       g_strcmp0(rstn_data->ifname, "") != 0 &&
+                   g_strcmp0(connection->ifname, rstn_data->ifname) != 0 &&
+                   g_strcmp0(connection->tether_iface.ifname, rstn_data->ifname) != 0)
+                   return;
+       } else {
+               GSList *conn_list = stc_get_connection_list();
+               for (; conn_list != NULL; conn_list = conn_list->next) {
+                       stc_connection_s *conn = conn_list->data;
+                       if (conn == NULL || conn->ifname == NULL)
+                               continue;
+
+                       if (rstn_data->ifname != NULL &&
+                               g_strcmp0(rstn_data->ifname, "") != 0 &&
+                               g_strcmp0(conn->ifname, rstn_data->ifname) == 0 &&
+                               g_strcmp0(conn->tether_iface.ifname, rstn_data->ifname) == 0)
+                               connection = conn;
+               }
+
+               if (connection == NULL)
+                       return;
+       }
+
+       /* classid is invalid */
+       if (rstn_data->classid <= STC_UNKNOWN_CLASSID)
+               return;
+
+       /* Do not proceed for tethering station appid if found here,
+        * for tethering station apps __rstn_tethering_process() call
+        * will handle it */
+       if (g_str_has_suffix(app_id, STC_TETHERING_APP_SUFFIX) &&
+                       rstn_data->classid != STC_TETHERING_APP_CLASSID)
+               return;
+
+       switch (rstn_type) {
+       case RST_SET:
+       {
+               int i;
+               table_counters_info info;
+               int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
+
+               memset(&info, 0, sizeof(table_counters_info));
+               rstn_data->limit_exceeded = 0;
+
+               if ((rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
+                       rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
+                       (rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
+                       rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
+                       (rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
+                       rstn_data->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
+                       (rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
+                       rstn_data->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
+                       (rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
+                       rstn_data->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
+                       table_counters_get(rstn_data->restriction_id, &info);
+
+                       time_t current_time = 0;
+                       stc_rstn_cumulative_data_s stat;
+                       table_statistics_select_rule rule;
+                       time_t last_week_ts = stc_monitor_get_last_week_ts();
+                       time_t last_day_ts = stc_monitor_get_last_day_ts();
+
+                       memset(&stat, 0, sizeof(stc_rstn_cumulative_data_s));
+                       stat.month_start_ts = rstn_data->month_start_ts;
+                       stat.week_start_ts = last_week_ts;
+                       stat.day_start_ts = last_day_ts;
+
+                       memset(&rule, 0, sizeof(table_statistics_select_rule));
+                       rule.from = rstn_data->month_start_ts;
+                       time(&current_time);
+                       rule.to = current_time;
+                       rule.iftype = rstn_data->iftype;
+                       rule.granularity = GRANULARITY;
+
+                       table_statistics_per_app(rstn_data->app_id, &rule, __statistics_info_cb, &stat);
+
+                       rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
+                       rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
+                       rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
+                       rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
+                       rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
+
+                       if (STC_DEBUG_LOG)
+                               STC_LOGD("Rstn counter data[%lld] warn[%lld] "
+                               "monthly[%lld] weekly[%lld] daily[%lld]",
+                               rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA],
+                               rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
+                               rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
+                               rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
+                               rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY]);
+               }
+
+               for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
+                       if (rstn_data->limit[i] >= 0) {
+                               effective_limit[i] = rstn_data->limit[i] - rstn_data->counter[i];
+
+                               if (effective_limit[i] < 0)
+                                       rstn_data->limit_exceeded |= (1 << i);
+                       }
+               }
+
+               STC_LOGD("Rstn_id[%llu] limit[%lld] datausage[%lld]",
+                                       rstn_data->restriction_id,
+                                       rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA],
+                                       info.data_counter);
+
+               if (rstn_data->limit_exceeded != 0 &&
+                       rstn_data->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
+                       __rstn_add_ipt_rule(rstn_data->classid, NFACCT_BLOCK, rstn_data->iftype);
+               }
+
+               if (rstn_data->classid == STC_BACKGROUND_APP_CLASSID)
+                       __rstn_add_ipt_rule(rstn_data->classid, NFACCT_BLOCK, rstn_data->iftype);
+
+               rstn_data->rstn_state = STC_RSTN_STATE_ACTIVATED;
+
+               if (STC_DEBUG_LOG) {
+                       STC_LOGD("Restriction activated "
+                               "[\033[1;36m%d\033[0;m:\033[1;35m%d\033[0;m]",
+                               rstn_data->classid, rstn_data->restriction_id);
+               }
+       }
+       break;
+       case RST_EXCLUDE:
+               __rstn_add_ipt_rule(rstn_data->classid, NFACCT_ALLOW,
+                                   rstn_data->iftype);
+
+               rstn_data->rstn_state = STC_RSTN_STATE_ACTIVATED;
+               rstn_data->limit_exceeded = 0;
+               rstn_data->limit_notified = 0;
+
+               if (STC_DEBUG_LOG) {
+                       STC_LOGD("Restriction activated "
+                               "[\033[1;36m%d\033[0;m:\033[1;35m%d\033[0;m]",
+                               rstn_data->classid, rstn_data->restriction_id);
+               }
+               break;
+       case RST_UNSET:
+       {
+               int i;
+
+               if (rstn_data->classid == STC_TETHERING_APP_CLASSID)
+                       __rstn_del_ipt_rule(rstn_data->classid, NFACCT_BLOCK,
+                                           rstn_data->iftype);
+               else
+                       __rstn_del_ipt_rule(rstn_data->classid, rstn_data->rstn_type,
+                                           rstn_data->iftype);
+
+               rstn_data->rstn_state = STC_RSTN_STATE_DEACTIVATED;
+               rstn_data->limit_exceeded = 0;
+               rstn_data->limit_notified = 0;
+
+               for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
+                       if (rstn_data->limit[i] >= 0)
+                               rstn_data->counter[i] = 0;
+
+               __rstn_set_noti_state(STC_RSTN_STATE_UNSET);
+
+               if (STC_DEBUG_LOG) {
+                       STC_LOGD("Restriction deactivated "
+                               "[\033[1;36m%d\033[0;m:\033[1;35m%d\033[0;m]",
+                               rstn_data->classid, rstn_data->restriction_id);
+               }
+       }
+       break;
+       default:
+               break;
+       }
+}
+
+static void __rstn_add(gpointer data, gpointer user_data)
+{
+       stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
+
+       /* rstn rule is activated */
+       if (rstn_data->rstn_state == STC_RSTN_STATE_ACTIVATED) {
+               if (STC_DEBUG_LOG) {
+                       STC_LOGD("Restriction already activated "
+                               "[\033[1;36m%d\033[0;m:\033[1;35m%d\033[0;m]",
+                               rstn_data->classid, rstn_data->restriction_id);
+               }
+               return;
+       }
+
+       if (rstn_data->rstn_type == STC_RSTN_TYPE_ACCEPT) {
+               __rstn_process(RST_EXCLUDE,
+                       rstn_data->app_id, rstn_data, user_data);
+               __rstn_tethering_process(RST_EXCLUDE,
+                       rstn_data->app_id, rstn_data, user_data);
+       } else {
+               __rstn_process(RST_SET,
+                       rstn_data->app_id, rstn_data, user_data);
+               __rstn_tethering_process(RST_SET,
+                       rstn_data->app_id, rstn_data, user_data);
+       }
+
+       if (STC_DEBUG_LOG) {
+               __print_rstn(rstn_data);
+               STC_LOGD("\033[1;32mRestriction added\033[0;m "
+                       "[\033[1;36m%d\033[0;m:\033[1;35m%d\033[0;m]",
+                       rstn_data->classid, rstn_data->restriction_id);
+       }
+}
+
+static void __rstn_add_by_connection(gpointer key,
+                                       gpointer value, gpointer data)
+{
+       stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
+
+       g_slist_foreach(rstn_value->rules, __rstn_add, data);
+}
+
+static void __rstn_remove(gpointer data, gpointer user_data)
+{
+       stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
+
+       if (rstn_data->rstn_state == STC_RSTN_STATE_DEACTIVATED) {
+               STC_LOGD("\033[1;31mRestriction already deactivated\033[0;m "
+                       "[\033[1;36m%d\033[0;m:\033[1;35m%d\033[0;m]",
+                       rstn_data->classid, rstn_data->restriction_id);
+               return;
+       }
+
+       __rstn_process(RST_UNSET,
+               rstn_data->app_id, rstn_data, user_data);
+       __rstn_tethering_process(RST_UNSET,
+               rstn_data->app_id, rstn_data, user_data);
+
+       if (STC_DEBUG_LOG) {
+               __print_rstn(rstn_data);
+               STC_LOGD("\033[1;31mRestriction removed\033[0;m "
+                       "[\033[1;36m%d\033[0;m:\033[1;35m%d\033[0;m]",
+                       rstn_data->classid, rstn_data->restriction_id);
+       }
+}
+
+static void __rstn_remove_by_connection(gpointer key,
+                                       gpointer value, gpointer data)
+{
+       stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
+
+       g_slist_foreach(rstn_value->rules, __rstn_remove, data);
+}
+
+static void __rstn_update_counter_data(gpointer data,
+                                               gpointer user_data)
+{
+       stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
+
+       table_counters_info info = {
+               .restriction_id = rstn_data->restriction_id,
+               .data_counter = rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA],
+               .warn_counter = rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
+               .monthly_counter = rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
+               .weekly_counter = rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
+               .daily_counter = rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY]
+       };
+
+       table_counters_update_counters(&info);
+}
+
+static void __rstn_update_counter_value(gpointer key,
+                                               gpointer value, gpointer data)
+{
+       stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
+
+       g_slist_foreach(rstn_value->rules, __rstn_update_counter_data, NULL);
+}
+
+static void __rstn_data_destroy(gpointer data)
+{
+       stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
+
+       FREE(rstn_data->app_id);
+       FREE(rstn_data->ifname);
+       FREE(rstn_data->subscriber_id);
+       FREE(rstn_data->mac);
+
+       FREE(rstn_data);
+}
+
+static gint __rstn_data_comp(gconstpointer a, gconstpointer b)
+{
+       stc_rstn_data_s *da = (stc_rstn_data_s *)a;
+       stc_rstn_data_s *db = (stc_rstn_data_s *)b;
+
+       if ((da->iftype == db->iftype) &&
+               (g_strcmp0(da->ifname, db->ifname) == 0) &&
+               (g_strcmp0(da->subscriber_id, db->subscriber_id) == 0) &&
+               (da->roaming == db->roaming))
+               return 0;
+
+       return -1;
+}
+
+static stc_error_e __rstn_data_remove(stc_rstn_data_s *data)
+{
+       stc_rstn_value_s *lookup_value;
+       GSList *lookup_list;
+       stc_rstn_data_s *lookup_data;
+       GHashTable *rstns = stc_monitor_get_system_rstns();
+
+       if (!rstns)
+               return STC_ERROR_NO_DATA;
+
+       lookup_value = g_hash_table_lookup(rstns, GUINT_TO_POINTER(data->classid));
+       if (!lookup_value) {
+               if (STC_DEBUG_LOG)
+                       STC_LOGE("Restriction not found [\033[1;36m%d\033[0;m]",
+                                               data->classid);
+               return STC_ERROR_NO_DATA;
+       }
+
+       lookup_list = g_slist_find_custom(lookup_value->rules,
+                                       data, __rstn_data_comp);
+       if (!lookup_list) {
+               if (STC_DEBUG_LOG)
+                       STC_LOGE("Restriction not found [%d:%s:%s:%d]",
+                               data->iftype, data->ifname,
+                               data->subscriber_id, data->roaming);
+               return STC_ERROR_NO_DATA;
+       }
+
+       lookup_data = lookup_list->data;
+
+       /* remove counter also */
+       table_counters_delete(lookup_data->restriction_id);
+       __rstn_remove(lookup_data, NULL);
+
+       lookup_value->rules = g_slist_remove(lookup_value->rules,
+                                                       lookup_data);
+       __rstn_data_destroy(lookup_data);
+
+       if (!lookup_value->rules)
+               g_hash_table_remove(rstns, GUINT_TO_POINTER(data->classid));
+
+       return STC_ERROR_NONE;
+}
+
+static stc_error_e __rstn_data_add(stc_rstn_data_s *data)
+{
+       int i;
+       stc_rstn_value_s *lookup_value;
+       stc_rstn_value_s *rstn_value;
+       stc_rstn_data_s *rstn_data;
+       GHashTable *rstns = stc_monitor_get_system_rstns();
+
+       if (!rstns)
+               return STC_ERROR_NO_DATA;
+
+       rstn_data = MALLOC0(stc_rstn_data_s, 1);
+       if (!rstn_data) {
+               if (STC_DEBUG_LOG)
+                       STC_LOGE("Rstn_data allocation failed");
+               return STC_ERROR_OUT_OF_MEMORY;
+       }
+
+       lookup_value = g_hash_table_lookup(rstns, GUINT_TO_POINTER(data->classid));
+       if (!lookup_value) {
+               rstn_value = MALLOC0(stc_rstn_value_s, 1);
+               if (!rstn_value) {
+                       if (STC_DEBUG_LOG)
+                               STC_LOGE("Rstn_value allocation failed");
+                       FREE(rstn_data);
+                       return STC_ERROR_OUT_OF_MEMORY;
+               }
+
+               g_hash_table_insert(rstns, GUINT_TO_POINTER(data->classid),
+                       rstn_value);
+       } else {
+               rstn_value = lookup_value;
+       }
+
+       rstn_data->classid = data->classid;
+       rstn_data->app_id = g_strdup(data->app_id);
+       rstn_data->iftype = data->iftype;
+       rstn_data->ifname = g_strdup(data->ifname);
+       rstn_data->subscriber_id = g_strdup(data->subscriber_id);
+       rstn_data->roaming = data->roaming;
+       rstn_data->mac = g_strdup(data->mac);
+
+       rstn_data->restriction_id = data->restriction_id;
+       rstn_data->rstn_state = data->rstn_state;
+       rstn_data->rstn_type = data->rstn_type;
+
+       for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
+               rstn_data->limit[i] = data->limit[i];
+               rstn_data->counter[i] = 0;
+       }
+
+       rstn_data->limit_exceeded = 0;
+       rstn_data->limit_notified = 0;
+       rstn_data->month_start_date = data->month_start_date;
+       rstn_data->month_start_ts = data->month_start_ts;
+
+       __rstn_data_remove(rstn_data);
+       rstn_value->rules = g_slist_append(rstn_value->rules, rstn_data);
+
+       __rstn_add(rstn_data, NULL);
+
+       return STC_ERROR_NONE;
+}
+
+static void __rstn_value_destroy(gpointer data)
+{
+       stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)data;
+
+       g_slist_free_full(rstn_value->rules, __rstn_data_destroy);
+
+       FREE(rstn_value);
+}
+
+static stc_cb_ret_e __rstn_insert_cb(const table_restrictions_info *info,
+                                           void *user_data)
+{
+       stc_cb_ret_e ret = STC_CONTINUE;
+       stc_rstn_data_s data;
+
+       memset(&data, 0, sizeof(stc_rstn_data_s));
+
+       if (info->app_id) {
+               data.classid = get_classid_by_app_id(info->app_id, TRUE);
+               data.app_id = info->app_id;
+       } else
+               data.classid = STC_UNKNOWN_CLASSID;
+
+       data.iftype = info->iftype;
+       data.ifname = info->ifname;
+       data.subscriber_id = info->subscriber_id;
+       data.roaming = info->roaming;
+
+       data.rstn_type = info->rstn_type;
+       data.rstn_state = STC_RSTN_STATE_UNKNOWN;
+       data.restriction_id = info->restriction_id;
+
+       data.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
+       data.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
+       data.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
+       data.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
+       data.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
+
+       if (__rstn_data_add(&data) != STC_ERROR_NONE)
+               ret = STC_CANCEL;
+
+       return ret;
+}
+
+static void __rstn_update_counter(classid_bytes_context_s *context,
+                               uint32_t classid)
+{
+       stc_rstn_value_s *lookup;
+       GHashTable *rstns = stc_monitor_get_system_rstns();
+
+       if (!rstns)
+               return;
+
+       lookup = g_hash_table_lookup(rstns, GUINT_TO_POINTER(classid));
+       if (lookup) {
+               context->counter->classid = classid;
+               g_slist_foreach(lookup->rules,
+                       stc_monitor_rstn_update_counter,
+                       context);
+       }
+}
+
+static void __rstn_action_when_limit_exceeded_tethering(stc_rstn_data_s *rstn_data,
+                                       classid_bytes_context_s *context)
+{
+       char *mac_str = NULL;
+       struct nfacct_rule *counter = context->counter;
+
+       /* get the station mac based on classid */
+       stc_plugin_tether_get_station_by_classid(counter->classid, &mac_str);
+       if (!mac_str) {
+               STC_LOGE("Station not found for classid(%d)", counter->classid);
+               return;
+       }
+
+       STC_LOGI("Station mac[%s] classid[%u] iftype[%u] iotype[%d] "
+                       "intend[%d] ifname[%s] bytes[%lld]", mac_str,
+                       counter->classid, counter->iftype, counter->iotype,
+                       counter->intend, counter->ifname, context->bytes);
+
+       /* Block tethering station immediately */
+       counter->intend = NFACCT_TETH_BLOCK;
+       __rstn_del_tether_rule(counter->classid, mac_str,
+                       NFACCT_TETH_BLOCK, rstn_data->iftype);
+
+       __rstn_add_tether_rule(counter->classid, mac_str,
+                       NFACCT_TETH_BLOCK, rstn_data->iftype);
+       counter->intend = NFACCT_TETH_COUNTER;
+
+       g_free(mac_str);
+}
+
+static void __reset_time_counter_foreach_rstn_data(gpointer data,
+                                                 gpointer user_data)
+{
+       stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
+       reset_time_limits_context_s *context =
+               (reset_time_limits_context_s *)user_data;
+       int i;
+       time_t now_month_start_ts;
+
+       if (rstn_data->month_start_date == 0) {
+               table_counters_info info;
+               memset(&info, 0, sizeof(table_counters_info));
+               table_counters_get_timestamps(rstn_data->restriction_id, &info);
+
+               if (info.month_start_date == 0)
+                       rstn_data->month_start_date = 1;
+               else
+                       rstn_data->month_start_date = info.month_start_date;
+               rstn_data->month_start_ts = info.month_start_ts;
+       }
+
+       now_month_start_ts =
+               stc_time_get_month_start(context->now,
+                                        rstn_data->month_start_date);
+
+       if (rstn_data->month_start_ts != now_month_start_ts) {
+               rstn_data->month_start_ts = now_month_start_ts;
+               context->month_start_ts = now_month_start_ts;
+               context->is_updated |= (1 << STC_RSTN_LIMIT_TYPE_MONTHLY);
+       }
+
+       if (context->is_updated) {
+               table_counters_info info;
+               memset(&info, 0, sizeof(table_counters_info));
+
+               info.restriction_id = rstn_data->restriction_id;
+               info.month_start_date = rstn_data->month_start_date;
+               info.month_start_ts = rstn_data->month_start_ts;
+               info.week_start_ts = context->week_start_ts;
+               info.day_start_ts = context->day_start_ts;
+
+               table_counters_update_timestamps(&info);
+       }
+
+       for (i = STC_RSTN_LIMIT_TYPE_MONTHLY; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
+
+               if ((context->is_updated) & (1 << i)) {
+                       /* reset limit */
+                       rstn_data->counter[i] = 0;
+
+                       if (rstn_data->limit_exceeded & (1 << i)) {
+                               /* remove iptables rule */
+                               GSList *conn_list = stc_get_connection_list();
+                               struct nfacct_rule counter;
+                               stc_s *stc = stc_get_manager();
+                               if (stc == NULL) {
+                                       STC_LOGE("Can't get stc data");
+                                       goto try_next_callback;
+                               }
+
+                               if (!stc->carg) {
+                                       stc->carg = MALLOC0(counter_arg_s, 1);
+                                       if (stc->carg == NULL) {
+                                               goto try_next_callback;
+                                       }
+
+                                       stc->carg->sock = stc_monitor_get_contr_sock();
+                               }
+
+                               for (; conn_list != NULL; conn_list = conn_list->next) {
+                                       stc_connection_s *conn = conn_list->data;
+
+                                       memset(&counter, 0, sizeof(struct nfacct_rule));
+
+                                       counter.carg = stc->carg;
+                                       counter.classid = rstn_data->classid;
+                                       counter.intend = NFACCT_BLOCK;
+                                       counter.iftype = rstn_data->iftype;
+                                       g_strlcpy(counter.ifname, conn->ifname, MAX_IFACE_LENGTH);
+
+                                       /* iptables rule */
+                                       stc_monitor_ipt_del_in(&counter);
+                                       stc_monitor_ipt_del_out(&counter);
+
+                                       /* ip6tables rule */
+                                       stc_monitor_ip6t_del_in(&counter);
+                                       stc_monitor_ip6t_del_out(&counter);
+
+                                       rstn_data->rstn_state = STC_RSTN_STATE_DEACTIVATED;
+                                       rstn_data->limit_exceeded &= ~(1 << i);
+                                       rstn_data->limit_notified &= ~(1 << i);
+                               }
+                       }
+               }
+       }
+
+try_next_callback:
+       return;
+}
+
+static void __reset_time_counter_foreach_rstn_value(gpointer key,
+                                                 gpointer value,
+                                                 gpointer data)
+{
+       stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
+       g_slist_foreach(rstn_value->rules,
+               __reset_time_counter_foreach_rstn_data, data);
+}
+
+void stc_monitor_rstn_reset_time_counters_if_required(void)
+{
+       reset_time_limits_context_s context;
+       GHashTable *rstns = stc_monitor_get_system_rstns();
+       time_t last_week_ts = stc_monitor_get_last_week_ts();
+       time_t last_day_ts = stc_monitor_get_last_day_ts();
+
+       context.now = time(NULL);
+       context.week_start_ts = stc_time_get_week_start(context.now);
+       context.day_start_ts = stc_time_get_day_start(context.now);
+       context.is_updated = 0;
+
+       if (last_week_ts != context.week_start_ts) {
+               stc_monitor_set_last_week_ts(context.week_start_ts);
+               context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_WEEKLY);
+       }
+
+       if (last_day_ts != context.day_start_ts) {
+               stc_monitor_set_last_day_ts(context.day_start_ts);
+               context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_DAILY);
+       }
+
+       if (rstns) {
+               g_hash_table_foreach(rstns,
+                              __reset_time_counter_foreach_rstn_value,
+                              &context);
+
+               if (context.is_updated)
+                       STC_LOGD("Counter reset completed month_start[%ld] "
+                               "week_start[%ld] day_start[%ld]",
+                               context.month_start_ts, last_week_ts, last_day_ts);
+       }
+}
+
+void stc_monitor_rstn_update_counter(gpointer data,
+                                       gpointer user_data)
+{
+       int i;
+       stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
+       classid_bytes_context_s *context = (classid_bytes_context_s *)user_data;
+       GSList *conn_list = stc_get_connection_list();
+
+       for (; conn_list != NULL; conn_list = conn_list->next) {
+               stc_connection_s *conn = conn_list->data;
+
+               if (rstn_data->iftype != context->counter->iftype)
+                       return;
+
+               if (rstn_data->ifname != NULL &&
+                       g_strcmp0(rstn_data->ifname, "") &&
+                       g_strcmp0(rstn_data->ifname, context->counter->ifname) != 0)
+                       return;
+
+               if (rstn_data->subscriber_id != NULL &&
+                       g_strcmp0(rstn_data->subscriber_id, "") &&
+                       g_strcmp0(rstn_data->subscriber_id, conn->subscriber_id) != 0)
+                       return;
+
+               if (rstn_data->roaming != conn->roaming)
+                       return;
+
+               if (rstn_data->limit_exceeded != 0) {
+                       context->data_limit_exceeded = TRUE;
+                       return;
+               }
+
+               switch (context->counter->iotype) {
+               case NFACCT_COUNTER_IN:
+               case NFACCT_COUNTER_OUT:
+                       if ((rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
+                               rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
+                               (rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
+                               rstn_data->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
+                               (rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
+                               rstn_data->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
+                               (rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
+                               rstn_data->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
+                               (rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
+                               rstn_data->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
+                               table_counters_info info;
+                               memset(&info, 0, sizeof(table_counters_info));
+                               table_counters_get(rstn_data->restriction_id, &info);
+
+                               rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
+                               rstn_data->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
+                               rstn_data->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter;
+                               rstn_data->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter;
+                               rstn_data->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter;
+                       }
+
+                       for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
+                               if (rstn_data->limit[i] >= 0 &&
+                                       !(rstn_data->limit_notified & (1 << i))) {
+                                       rstn_data->counter[i] += context->bytes;
+                                       if (rstn_data->limit[i] <= rstn_data->counter[i])
+                                               stc_monitor_rstn_action_when_limit_exceeded(i,
+                                                                                       rstn_data,
+                                                                                       context);
+                               }
+                       }
+
+                       stc_monitor_set_rstns_updated(TRUE);
+                       __print_rstn(rstn_data);
+                       break;
+               default:
+                       STC_LOGE("Unknown iotype");
+               }
+       }
+}
+
+void stc_monitor_rstn_update_iface_counter(classid_bytes_context_s *context)
+{
+       switch (context->counter->iftype) {
+       case STC_IFACE_DATACALL:
+               __rstn_update_counter(context, STC_TOTAL_DATACALL_CLASSID);
+               break;
+       case STC_IFACE_WIFI:
+               __rstn_update_counter(context, STC_TOTAL_WIFI_CLASSID);
+               __rstn_update_counter(context, STC_TETHERING_APP_CLASSID);
+               break;
+       case STC_IFACE_BLUETOOTH:
+               __rstn_update_counter(context, STC_TOTAL_BLUETOOTH_CLASSID);
+               __rstn_update_counter(context, STC_TETHERING_APP_CLASSID);
+               break;
+       case STC_IFACE_USB:
+               __rstn_update_counter(context, STC_TETHERING_APP_CLASSID);
+               break;
+       case STC_IFACE_P2P:
+               __rstn_update_counter(context, STC_TETHERING_APP_CLASSID);
+               break;
+       default:
+               break;
+       }
+}
+
+void stc_monitor_rstn_action_when_limit_exceeded(stc_rstn_limit_type_e limit_type,
+                                               stc_rstn_data_s *rstn_data,
+                                               classid_bytes_context_s *context)
+{
+       gboolean rv;
+       char iftype[MAX_INT_LENGTH] = { 0, };
+       char byte[MAX_INT_LENGTH] = { 0, };
+       const char *signal_name = NULL;
+       const char *net_popup_content = NULL;
+       const char *net_popup_type = NULL;
+       stc_s *stc = (stc_s *)stc_get_manager();
+
+       if (stc == NULL) {
+               STC_LOGE("Failed to get stc data");
+               return;
+       }
+
+       switch (limit_type) {
+       case STC_RSTN_LIMIT_TYPE_DATA_WARN:
+       {
+               signal_name = "WarnThresholdCrossed";
+               net_popup_content = "warn threshold crossed";
+               net_popup_type = "warning_noti";
+       }
+       break;
+       case STC_RSTN_LIMIT_TYPE_DATA:
+       case STC_RSTN_LIMIT_TYPE_MONTHLY:
+       case STC_RSTN_LIMIT_TYPE_WEEKLY:
+       case STC_RSTN_LIMIT_TYPE_DAILY:
+       {
+               signal_name = "RestrictionThresholdCrossed";
+               net_popup_content = "restriction threshold crossed";
+               net_popup_type = "restriction_noti";
+
+               /* Apply restriction for tethering apps if app_id is of tethering client
+                * otherwise do the normal iptables rule */
+               if (context->counter->intend == NFACCT_TETH_COUNTER) {
+
+                       if (g_str_has_suffix(rstn_data->app_id, STC_TETHERING_APP_SUFFIX) &&
+                                       rstn_data->classid != STC_TETHERING_APP_CLASSID) {
+                               __rstn_action_when_limit_exceeded_tethering(rstn_data,
+                                               context);
+                       }
+
+               } else {
+                       /* block immediately */
+                       context->counter->intend = NFACCT_BLOCK;
+                       stc_monitor_ipt_del_in(context->counter);
+                       stc_monitor_ipt_del_out(context->counter);
+                       stc_monitor_ipt_add_in(context->counter);
+                       stc_monitor_ipt_add_out(context->counter);
+
+                       stc_monitor_ip6t_del_in(context->counter);
+                       stc_monitor_ip6t_del_out(context->counter);
+                       stc_monitor_ip6t_add_in(context->counter);
+                       stc_monitor_ip6t_add_out(context->counter);
+                       context->counter->intend = NFACCT_COUNTER;
+               }
+
+               rstn_data->limit_exceeded |= (1 << limit_type);
+               __rstn_set_noti_state(STC_RSTN_STATE_SET);
+       }
+       break;
+       default:
+               break;
+       }
+
+       if (signal_name == NULL) {
+               STC_LOGE("Invalid parameter: limit_type");
+               return;
+       }
+
+       /* emit signal */
+       rv = stc_manager_dbus_emit_signal(stc->connection,
+                                                 STC_DBUS_SERVICE_RESTRICTION_PATH,
+                                                 STC_DBUS_INTERFACE_RESTRICTION,
+                                                 signal_name,
+                                                 g_variant_new("(si)",
+                                                 rstn_data->app_id,
+                                                 rstn_data->iftype));
+
+       if (rv == TRUE)
+               rstn_data->limit_notified |= (1 << limit_type);
+
+       snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_data->iftype);
+       snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_data->limit[limit_type]);
+       stc_plugin_appstatus_send_message(net_popup_content,
+                       net_popup_type, rstn_data->app_id, iftype, byte);
+
+       if (STC_DEBUG_LOG)
+               STC_LOGD("Limit exceeded [\033[0;31m%s\033[0;m:%d]",
+                                       net_popup_content, limit_type);
+}
+
+gboolean stc_monitor_rstn_flush_contr_to_db(gpointer user_data)
+{
+       time_t current_time = 0;
+       stc_s *stc = stc_get_manager();
+       GHashTable *rstns = stc_monitor_get_system_rstns();
+       gboolean rstns_updated = stc_monitor_get_rstns_updated();
+
+       if (stc && stc->carg)
+               current_time = stc->carg->last_run_time;
+
+       if (rstns_updated == FALSE)
+               return G_SOURCE_REMOVE;
+
+       stc_monitor_set_rstns_updated(FALSE);
+
+       if (rstns)
+               g_hash_table_foreach(rstns,
+                              __rstn_update_counter_value,
+                              &current_time);
+
+       STC_LOGI("Flushed rstns counters to database");
+       return G_SOURCE_REMOVE;
+}
+
+stc_error_e stc_monitor_rstn_add(const table_restrictions_info *info)
+{
+       stc_rstn_data_s data;
+
+       memset(&data, 0, sizeof(stc_rstn_data_s));
+
+       if (info->app_id) {
+               data.classid = get_classid_by_app_id(info->app_id, TRUE);
+               data.app_id = info->app_id;
+       } else
+               data.classid = STC_UNKNOWN_CLASSID;
+
+       if (data.classid == STC_BACKGROUND_APP_CLASSID) {
+               stc_monitor_set_background_state(TRUE);
+               __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, TRUE);
+       }
+
+       data.iftype = info->iftype;
+       data.ifname = info->ifname;
+       data.subscriber_id = info->subscriber_id;
+       data.roaming = info->roaming;
+       data.mac = info->mac;
+
+       data.rstn_type = info->rstn_type;
+       data.rstn_state = STC_RSTN_STATE_UNKNOWN;
+       data.restriction_id = info->restriction_id;
+
+       data.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
+       data.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
+       data.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
+       data.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
+       data.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
+       data.month_start_date = info->month_start_date;
+       data.month_start_ts = stc_time_get_month_start(time(NULL),
+                                                                               info->month_start_date);
+
+       return __rstn_data_add(&data);
+}
+
+void stc_monitor_rstn_add_for_app(uint32_t classid)
+{
+       stc_rstn_value_s *lookup_value;
+       GHashTable *rstns = stc_monitor_get_system_rstns();
+
+       if (!rstns)
+               return;
+
+       lookup_value = g_hash_table_lookup(rstns, GUINT_TO_POINTER(classid));
+       if (!lookup_value) {
+               if (STC_DEBUG_LOG)
+                       STC_LOGD("Restriction not found [\033[1;36m%d\033[0;m]",
+                                               classid);
+               return;
+       }
+
+       g_slist_foreach(lookup_value->rules, __rstn_add, NULL);
+}
+
+void stc_monitor_rstn_add_by_connection(stc_connection_s *conn)
+{
+       GHashTable *rstns = stc_monitor_get_system_rstns();
+
+       if (!rstns)
+               return;
+
+       g_hash_table_foreach(rstns, __rstn_add_by_connection, conn);
+}
+
+stc_error_e stc_monitor_rstn_remove(const table_restrictions_info *info)
+{
+       stc_rstn_data_s data;
+
+       memset(&data, 0, sizeof(stc_rstn_data_s));
+
+       data.classid = get_classid_by_app_id(info->app_id, TRUE);
+       data.app_id = info->app_id;
+
+       data.iftype = info->iftype;
+       data.ifname = info->ifname;
+       data.subscriber_id = info->subscriber_id;
+       data.roaming = info->roaming;
+
+       if (g_strcmp0(info->app_id, STC_TOTAL_BACKGROUND) == 0) {
+               stc_monitor_set_background_state(FALSE);
+               __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, FALSE);
+       }
+
+       return __rstn_data_remove(&data);
+}
+
+void stc_monitor_rstn_remove_for_app(uint32_t classid)
+{
+       stc_rstn_value_s *lookup_value;
+       GHashTable *rstns = stc_monitor_get_system_rstns();
+
+       if (!rstns)
+               return;
+
+       lookup_value = g_hash_table_lookup(rstns, GUINT_TO_POINTER(classid));
+       if (!lookup_value) {
+               if (STC_DEBUG_LOG)
+                       STC_LOGD("Restriction not found [\033[1;36m%d\033[0;m]",
+                                               classid);
+               return;
+       }
+
+       g_slist_foreach(lookup_value->rules, __rstn_remove, NULL);
+}
+
+void stc_monitor_rstn_remove_by_connection(stc_connection_s *conn)
+{
+       GHashTable *rstns = stc_monitor_get_system_rstns();
+
+       if (!rstns)
+               return;
+
+       g_hash_table_foreach(rstns, __rstn_remove_by_connection, conn);
+}
+
+void stc_monitor_rstns_load(void)
+{
+       table_restrictions_foreach(__rstn_insert_cb, NULL);
+
+       /* __rstn_tree_printall(); */
+}
+
+GHashTable *stc_monitor_rstns_init(void)
+{
+       return g_hash_table_new_full(g_direct_hash, g_direct_equal,
+                                       NULL, __rstn_value_destroy);
+}
old mode 100755 (executable)
new mode 100644 (file)
index 4f1129c..0bfc5a5
@@ -18,7 +18,7 @@
 #include <vconf.h>
 #include <vconf-keys.h>
 
-#include "stc-default-connection.h"
+#include "stc-connection.h"
 #include "helper-nl.h"
 #include "helper-nfacct-rule.h"
 #include "helper-net-cls.h"
 #include "table-statistics.h"
 #include "table-counters.h"
 #include "stc-monitor.h"
-#include "stc-time.h"
-#include "stc-manager-plugin-appstatus.h"
 #include "stc-manager-plugin-exception.h"
 #include "stc-manager-plugin-tether.h"
 
-#define GRANULARITY 10
-#define MAX_INT_LENGTH 128
-
-#ifndef VCONFKEY_STC_BACKGROUND_STATE
-#define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
-#endif
-
-#ifndef VCONFKEY_SETAPPL_DATA_RESTRICTION_INT
-#define VCONFKEY_SETAPPL_DATA_RESTRICTION_INT "db/setting/data_restriction"
-#endif
-
-typedef struct {
-       time_t now;
-       time_t month_start_ts;
-       time_t week_start_ts;
-       time_t day_start_ts;
-       int is_updated;
-} reset_time_limits_context_s;
-
-typedef struct {
-       stc_app_key_s *app_key;
-       stc_process_key_s *proc_key;
-       gboolean entry_removed;
-} remove_pid_context_s;
-
-typedef struct {
-       struct nfacct_rule *counter;
-       int64_t bytes;
-       gboolean data_limit_exceeded;
-} classid_bytes_context_s;
-
 static stc_system_s *g_system = NULL;
 
-//LCOV_EXCL_START
 static int __vconf_get_int(const char *key, int *value)
 {
-       int ret = 0;
-
-       ret = vconf_get_int(key, value);
-       if (ret != VCONF_OK) {
-               STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
-               return -1; //LCOV_EXCL_LINE
-       }
-
-       return 0;
-}
-
-static int __vconf_set_int(const char *key, int value)
-{
-       int ret = 0;
-
-       ret = vconf_set_int(key, value);
-       if (ret != VCONF_OK) {
-               STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
-               return -1; //LCOV_EXCL_LINE
-       }
-
-       return 0;
-}
-//LCOV_EXCL_STOP
-
-static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
-{
-       if (counter->intend == NFACCT_WARN)
-               return NFACCT_JUMP_ACCEPT;
-       else if (counter->intend == NFACCT_BLOCK)
-               return NFACCT_JUMP_REJECT;
-       else if (counter->intend == NFACCT_ALLOW)
-               return NFACCT_JUMP_ACCEPT;
-       else if (counter->intend == NFACCT_TETH_BLOCK)
-               return NFACCT_JUMP_REJECT;
-       else if (counter->intend == NFACCT_TETH_ALLOW)
-               return NFACCT_JUMP_ACCEPT;
-
-       return NFACCT_JUMP_UNKNOWN;
-}
-
-static stc_error_e __add_iptables_tether_in(struct nfacct_rule *counter,
-               const gchar *ipaddr)
-{
-       int ret;
-
-       if (counter == NULL || ipaddr == NULL)
-               return STC_ERROR_INVALID_PARAMETER;
-
-       counter->action = NFACCT_ACTION_INSERT;
-       counter->iotype = NFACCT_COUNTER_IN;
-       counter->jump = __get_jump_by_intend(counter);
-       counter->iptype = NFACCT_TYPE_IPV4;
-       counter->send_limit = 0;
-       counter->rcv_limit = 0;
-       counter->src_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
-       counter->src_ip1 = g_strdup(ipaddr);
-
-       ret = produce_net_rule(counter);
-
-       FREE(counter->src_ip1);
-       counter->src_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
-       return ret;
-}
-
-static stc_error_e __add_iptables_tether_out(struct nfacct_rule *counter,
-               const gchar *ipaddr)
-{
-       int ret;
-
-       if (counter == NULL || ipaddr == NULL)
-               return STC_ERROR_INVALID_PARAMETER;
-
-       counter->action = NFACCT_ACTION_INSERT;
-       counter->iotype = NFACCT_COUNTER_OUT;
-       counter->jump = __get_jump_by_intend(counter);
-       counter->iptype = NFACCT_TYPE_IPV4;
-       counter->send_limit = 0;
-       counter->rcv_limit = 0;
-       counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
-       counter->dst_ip1 = g_strdup(ipaddr);
-
-       ret = produce_net_rule(counter);
-
-       FREE(counter->dst_ip1);
-       counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
-       return ret;
-}
-
-static stc_error_e __del_iptables_tether_in(struct nfacct_rule *counter,
-               const gchar *ipaddr)
-{
-       int ret;
-
-       if (counter == NULL || ipaddr == NULL)
-               return STC_ERROR_INVALID_PARAMETER;
-
-       counter->action = NFACCT_ACTION_DELETE;
-       counter->iotype = NFACCT_COUNTER_IN;
-       counter->jump = __get_jump_by_intend(counter);
-       counter->iptype = NFACCT_TYPE_IPV4;
-       counter->send_limit = 0;
-       counter->rcv_limit = 0;
-       counter->src_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
-       counter->src_ip1 = g_strdup(ipaddr);
-
-       ret = produce_net_rule(counter);
-
-       FREE(counter->src_ip1);
-       counter->src_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
-       return ret;
-}
-
-static stc_error_e __del_iptables_tether_out(struct nfacct_rule *counter,
-               const gchar *ipaddr)
-{
-       int ret;
-
-       if (counter == NULL || ipaddr == NULL)
-               return STC_ERROR_INVALID_PARAMETER;
-
-       counter->action = NFACCT_ACTION_DELETE;
-       counter->iotype = NFACCT_COUNTER_OUT;
-       counter->jump = __get_jump_by_intend(counter);
-       counter->iptype = NFACCT_TYPE_IPV4;
-       counter->send_limit = 0;
-       counter->rcv_limit = 0;
-       counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_SINGLE;
-       counter->dst_ip1 = g_strdup(ipaddr);
-
-       ret = produce_net_rule(counter);
-
-       FREE(counter->dst_ip1);
-       counter->dst_iprange_type = NFACCT_IPRANGE_TYPE_NONE;
-       return ret;
-}
-
-static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
-{
-       if (counter == NULL)
-               return STC_ERROR_INVALID_PARAMETER;
-
-       counter->action = NFACCT_ACTION_INSERT;
-       counter->iotype = NFACCT_COUNTER_IN;
-       counter->jump = __get_jump_by_intend(counter);
-       counter->iptype = NFACCT_TYPE_IPV4;
-       counter->send_limit = 0;
-       counter->rcv_limit = 0;
-
-       return produce_net_rule(counter);
-}
-
-static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
-{
-       if (counter == NULL)
-               return STC_ERROR_INVALID_PARAMETER;
-
-       counter->action = NFACCT_ACTION_INSERT;
-       counter->iotype = NFACCT_COUNTER_OUT;
-       counter->jump = __get_jump_by_intend(counter);
-       counter->iptype = NFACCT_TYPE_IPV4;
-       counter->send_limit = 0;
-       counter->rcv_limit = 0;
-
-       return produce_net_rule(counter);
-}
-
-static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
-{
-       if (counter == NULL)
-               return STC_ERROR_INVALID_PARAMETER;
-
-       counter->action = NFACCT_ACTION_DELETE;
-       counter->iotype = NFACCT_COUNTER_IN;
-       counter->jump = __get_jump_by_intend(counter);
-       counter->iptype = NFACCT_TYPE_IPV4;
-       counter->send_limit = 0;
-       counter->rcv_limit = 0;
-
-       return produce_net_rule(counter);
-}
-
-static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
-{
-       if (counter == NULL)
-               return STC_ERROR_INVALID_PARAMETER;
-
-       counter->action = NFACCT_ACTION_DELETE;
-       counter->iotype = NFACCT_COUNTER_OUT;
-       counter->jump = __get_jump_by_intend(counter);
-       counter->iptype = NFACCT_TYPE_IPV4;
-       counter->send_limit = 0;
-       counter->rcv_limit = 0;
-
-       return produce_net_rule(counter);
-}
-
-static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
-{
-       if (counter == NULL)
-               return STC_ERROR_INVALID_PARAMETER;
-
-       counter->action = NFACCT_ACTION_INSERT;
-       counter->iotype = NFACCT_COUNTER_IN;
-       counter->jump = __get_jump_by_intend(counter);
-       counter->iptype = NFACCT_TYPE_IPV6;
-       counter->send_limit = 0;
-       counter->rcv_limit = 0;
-
-       return produce_net_rule(counter);
-}
-
-static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
-{
-       if (counter == NULL)
-               return STC_ERROR_INVALID_PARAMETER;
-
-       counter->action = NFACCT_ACTION_INSERT;
-       counter->iotype = NFACCT_COUNTER_OUT;
-       counter->jump = __get_jump_by_intend(counter);
-       counter->iptype = NFACCT_TYPE_IPV6;
-       counter->send_limit = 0;
-       counter->rcv_limit = 0;
-
-       return produce_net_rule(counter);
-}
-
-static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
-{
-       if (counter == NULL)
-               return STC_ERROR_INVALID_PARAMETER;
-
-       counter->action = NFACCT_ACTION_DELETE;
-       counter->iotype = NFACCT_COUNTER_IN;
-       counter->jump = __get_jump_by_intend(counter);
-       counter->iptype = NFACCT_TYPE_IPV6;
-       counter->send_limit = 0;
-       counter->rcv_limit = 0;
-
-       return produce_net_rule(counter);
-}
-
-static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
-{
-       if (counter == NULL)
-               return STC_ERROR_INVALID_PARAMETER;
-
-       counter->action = NFACCT_ACTION_DELETE;
-       counter->iotype = NFACCT_COUNTER_OUT;
-       counter->jump = __get_jump_by_intend(counter);
-       counter->iptype = NFACCT_TYPE_IPV6;
-       counter->send_limit = 0;
-       counter->rcv_limit = 0;
-
-       return produce_net_rule(counter);
-}
-
-static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
-                                       gpointer UNUSED user_data)
-{
-       stc_process_key_s *key_a = (stc_process_key_s *)a;
-       stc_process_key_s *key_b = (stc_process_key_s *)b;
-
-       return key_a->pid - key_b->pid;
-}
-
-static void __processes_tree_value_free(gpointer data)
-{
-       stc_process_value_s *value = (stc_process_value_s *)data;
-
-       FREE(value);
-}
-
-static void __processes_tree_key_free(gpointer data)
-{
-       stc_process_key_s *key = (stc_process_key_s *)data;
-
-       FREE(key);
-}
-
-static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
-                                  gpointer UNUSED user_data)
-{
-       stc_app_key_s *key_a = (stc_app_key_s *)a;
-       stc_app_key_s *key_b = (stc_app_key_s *)b;
-       gint ret;
-
-       ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
-       if (ret)
-               return ret;
-
-       return g_strcmp0(key_a->app_id, key_b->app_id);
-}
-
-static void __apps_tree_value_free(gpointer data)
-{
-       stc_app_value_s *value = (stc_app_value_s *)data;
-
-       g_tree_destroy(value->processes);
-       value->processes = NULL;
-
-       FREE(value);
-}
-
-static void __apps_tree_key_free(gpointer data)
-{
-       stc_app_key_s *key = (stc_app_key_s *)data;
-
-       g_free(key->pkg_id);
-       g_free(key->app_id);
-       FREE(key);
-}
-
-static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
-                                   gpointer UNUSED user_data)
-{
-       stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
-       stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
-       int ret;
-
-       ret = g_strcmp0(key_a->app_id, key_b->app_id);
-       if (ret != 0)
-               return ret;
-
-       ret = g_strcmp0(key_a->ifname, key_b->ifname);
-       if (ret != 0)
-               return ret;
-
-       ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
-       if (ret != 0)
-               return ret;
-
-       ret = key_a->iftype - key_b->iftype;
-       if (ret != 0)
-               return ret;
-
-       ret = key_a->roaming - key_b->roaming;
-       if (ret != 0)
-               return ret;
-
-       return 0;
-}
-
-static void __rstns_tree_value_free(gpointer data)
-{
-       stc_rstn_value_s *value = (stc_rstn_value_s *)data;
-
-       FREE(value);
-}
-
-static void __rstns_tree_key_free(gpointer data)
-{
-       stc_rstn_key_s *key = (stc_rstn_key_s *)data;
-
-       FREE(key->app_id);
-       FREE(key->ifname);
-       FREE(key->subscriber_id);
-       FREE(key);
-}
-
-/*
-//LCOV_EXCL_START
-static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
-                                              gpointer data)
-{
-       stc_process_key_s *proc_key = (stc_process_key_s *)key;
-       stc_process_value_s *proc_value = (stc_process_value_s *)value;
-
-       STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
-                proc_key->pid, proc_value->ground);
-       return FALSE;
-}
-
-static void __processes_tree_printall(GTree *processes)
-{
-       g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
-}
-
-static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
-                                         gpointer data)
-{
-       stc_app_key_s *app_key = (stc_app_key_s *)key;
-       stc_app_value_s *app_value = (stc_app_value_s *)value;
-
-       STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
-                "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
-                " counter [ in (%lld), out (%lld)]",
-                app_key->pkg_id, app_key->app_id,
-                app_value->type, app_value->classid,
-                app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
-
-       __processes_tree_printall(app_value->processes);
-       return FALSE;
-}
-
-static void __apps_tree_printall(void)
-{
-       g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
-}
-//LCOV_EXCL_STOP
-*/
-
-static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
-                                              gpointer data)
-{
-       remove_pid_context_s *context = (remove_pid_context_s *)data;
-       stc_app_value_s *app_value = (stc_app_value_s *)value;
-
-       if (!g_tree_remove(app_value->processes, context->proc_key))
-               return FALSE;
-
-       context->entry_removed = TRUE;
-       context->app_key = (stc_app_key_s *)key;
-
-       return TRUE;
-}
-
-static stc_app_value_s * __application_lookup(GTree *apps,
-                                             const stc_app_key_s *key)
-{
-       stc_app_value_s *lookup;
-
-       ret_value_msg_if(apps == NULL, NULL, "apps is null!");
-
-       lookup = g_tree_lookup(apps, key);
-
-       return lookup;
-}
-
-static stc_process_value_s * __process_lookup(GTree *processes,
-                                             const stc_process_key_s *key)
-{
-       stc_process_value_s *lookup;
-
-       ret_value_msg_if(processes == NULL, NULL, "processes is null!");
-
-       lookup = g_tree_lookup(processes, key);
-
-       return lookup;
-}
-
-//LCOV_EXCL_START
-static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
-                                            gpointer data)
-{
-       guint *pid_count = (guint *)data;
-       (*pid_count)++;
-       return TRUE;
-}
-//LCOV_EXCL_STOP
-
-static gboolean __add_application_monitor_for_tethering(gpointer key,
-                                   gpointer value, gpointer data)
-{
-       stc_app_value_s *app_value = (stc_app_value_s *)value;
-       stc_app_key_s *app_key = (stc_app_key_s *)key;
-       default_connection_s *connection = (default_connection_s *)data;
-       stc_s *stc = stc_get_manager();
-       struct nfacct_rule counter;
-       char *ipaddr = NULL;
-       int ret;
-
-       STC_LOGI("add appid(%s) classid(%d)", app_key->app_id,
-                       app_value->classid);
-
-       if (stc == NULL || connection == NULL)
-               return FALSE;
-
-       if (!stc->carg) {
-               stc->carg = MALLOC0(counter_arg_s, 1);
-               if (stc->carg == NULL)
-                       return FALSE;
-
-               stc->carg->sock = stc_monitor_get_counter_socket();
-       }
-
-       memset(&counter, 0, sizeof(struct nfacct_rule));
-
-       counter.carg = stc->carg;
-       counter.classid = app_value->classid;
-       counter.intend = NFACCT_TETH_COUNTER;
-
-       if (connection->tether_state != TRUE ||
-                       connection->tether_iface.ifname == NULL)
-               return FALSE;
-
-       counter.iftype = connection->tether_iface.type;
-       g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
-
-       /* get the ip address of the station based on its mac address */
-       ret = stc_plugin_tether_get_station_ip(app_value->mac, &ipaddr);
-       if (ret != STC_ERROR_NONE)
-               return FALSE;
-
-       /* tethering iptables rule */
-       __add_iptables_tether_in(&counter, ipaddr);
-       __add_iptables_tether_out(&counter, ipaddr);
-
-       g_free(ipaddr);
-       return FALSE;
-}
-
-static gboolean __remove_application_monitor_for_tethering(gpointer key, gpointer value,
-                                   gpointer data)
-{
-       stc_app_value_s *app_value = (stc_app_value_s *)value;
-       stc_app_key_s *app_key = (stc_app_key_s *)key;
-       default_connection_s *connection = (default_connection_s *)data;
-       stc_s *stc = stc_get_manager();
-       struct nfacct_rule counter;
-       char *ipaddr = NULL;
-       int ret;
-
-       STC_LOGI("remove appid(%s) classid(%d)", app_key->app_id,
-                       app_value->classid);
-
-       if (stc == NULL || connection == NULL)
-               return FALSE;
-
-       if (!stc->carg) {
-               stc->carg = MALLOC0(counter_arg_s, 1);
-               if (stc->carg == NULL)
-                       return FALSE;
-
-               stc->carg->sock = stc_monitor_get_counter_socket();
-       }
-
-       memset(&counter, 0, sizeof(struct nfacct_rule));
-
-       counter.carg = stc->carg;
-       counter.classid = app_value->classid;
-       counter.intend = NFACCT_TETH_COUNTER;
-
-       if (connection->tether_state != TRUE ||
-                       connection->tether_iface.ifname == NULL)
-               return FALSE;
-
-       counter.iftype = connection->tether_iface.type;
-       g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
-
-       /* get the ip address of the station based on its mac address */
-       ret = stc_plugin_tether_get_station_ip(app_value->mac, &ipaddr);
-       if (ret != STC_ERROR_NONE)
-               return FALSE;
-
-       __del_iptables_tether_in(&counter, ipaddr);
-       __del_iptables_tether_out(&counter, ipaddr);
-
-       g_free(ipaddr);
-       return FALSE;
-}
-
-static gboolean __add_application_monitor(gpointer key, gpointer value,
-                                         gpointer data)
-{
-       stc_app_value_s *app_value = (stc_app_value_s *)value;
-       stc_app_key_s *app_key = (stc_app_key_s *)key;
-       default_connection_s *connection = (default_connection_s *)data;
-       stc_s *stc = stc_get_manager();
-
-       if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
-           app_value->classid == STC_TOTAL_WIFI_CLASSID ||
-           app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
-               return FALSE;
-
-       if (stc && connection && connection->ifname) {
-               struct nfacct_rule counter;
-
-               if (!stc->carg) {
-                       stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
-                       if (stc->carg == NULL) //LCOV_EXCL_LINE
-                               return FALSE; //LCOV_EXCL_LINE
-
-                       stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
-               }
-
-               memset(&counter, 0, sizeof(struct nfacct_rule));
-
-               counter.carg = stc->carg;
-               counter.classid = app_value->classid;
-               counter.intend = NFACCT_COUNTER;
-
-               if (connection->tether_state == TRUE &&
-                       connection->tether_iface.ifname != NULL &&
-                       app_value->classid == STC_TETHERING_APP_CLASSID) {
-                       counter.iftype = connection->tether_iface.type;
-                       g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
-               } else {
-                       counter.iftype = connection->type;
-                       g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
-               }
-
-               if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX) &&
-                               app_value->classid != STC_TETHERING_APP_CLASSID) {
-                       __add_application_monitor_for_tethering(key, value, data);
-               } else if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
-                       __add_iptables_in(&counter);
-                       __add_iptables_out(&counter);
-               } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
-                       __add_ip6tables_in(&counter);
-                       __add_ip6tables_out(&counter);
-               } else {
-                       __add_iptables_in(&counter);
-                       __add_iptables_out(&counter);
-                       __add_ip6tables_in(&counter);
-                       __add_ip6tables_out(&counter);
-               }
-       }
-
-       return FALSE;
-}
-
-static gboolean __remove_application_monitor(gpointer key, gpointer value,
-                                            gpointer data)
-{
-       stc_app_value_s *app_value = (stc_app_value_s *)value;
-       stc_app_key_s *app_key = (stc_app_key_s *)key;
-       default_connection_s *connection = (default_connection_s *)data;
-       stc_s *stc = stc_get_manager();
-
-       if (stc && connection && connection->ifname) {
-               struct nfacct_rule counter;
-
-               if (!stc->carg) {
-                       stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
-                       if (stc->carg == NULL) //LCOV_EXCL_LINE
-                               return FALSE; //LCOV_EXCL_LINE
-
-                       stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
-               }
-
-               memset(&counter, 0, sizeof(struct nfacct_rule));
-
-               counter.carg = stc->carg;
-               counter.classid = app_value->classid;
-               counter.intend = NFACCT_COUNTER;
-
-               if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX) &&
-                               app_value->classid != STC_TETHERING_APP_CLASSID) {
-                       __remove_application_monitor_for_tethering(key, value, data);
-                       return FALSE;
-               } else if (connection->tether_state == FALSE &&
-                       connection->tether_iface.ifname != NULL &&
-                       app_value->classid == STC_TETHERING_APP_CLASSID) {
-                       counter.iftype = connection->tether_iface.type;
-                       g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
-               } else {
-                       counter.iftype = connection->type;
-                       g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
-               }
-
-               __del_iptables_in(&counter);
-               __del_iptables_out(&counter);
-               __del_ip6tables_in(&counter);
-               __del_ip6tables_out(&counter);
-       }
-
-       return FALSE;
-}
-
-static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
-{
-       STC_LOGI("rstn info => rstn_id [%llu], "
-                "app_id [%s], classid [%u], ifname [%s], "
-                "iftype [%d], rstn_state [%d], rstn_type [%d], "
-                "month_start_date [%d], limit [ (%lld) bytes], "
-                "warn_limit [ (%lld) bytes], "
-                "monthly_limit [ (%lld) bytes], "
-                "weekly_limit [ (%lld) bytes], "
-                "daily_limit [ (%lld) bytes], "
-                "data_counter [ (%lld) bytes], "
-                "warn_counter [ (%lld) bytes], "
-                "monthly_counter [ (%lld) bytes], "
-                "weekly_counter [ (%lld) bytes], "
-                "daily_counter [ (%lld) bytes], "
-                "roaming [%d], subscriber_id [%s]",
-                rstn_value->restriction_id,
-                rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
-                rstn_key->iftype, rstn_value->rstn_state, rstn_value->rstn_type,
-                rstn_value->month_start_date,
-                rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA],
-                rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN],
-                rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY],
-                rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY],
-                rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY],
-                rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
-                rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
-                rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
-                rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
-                rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY],
-                rstn_key->roaming, rstn_key->subscriber_id);
-}
-
-static void __add_iptables_rule(int64_t classid, nfacct_rule_intend intend,
-                               stc_iface_type_e iftype)
-{
-       char *default_ifname = stc_default_connection_get_ifname();
-       default_connection_s *connection = stc_get_default_connection();
-       struct nfacct_rule counter;
-       stc_s *stc = stc_get_manager();
-       if (!stc) {
-               g_free(default_ifname); //LCOV_EXCL_LINE
-               return; //LCOV_EXCL_LINE
-       }
-
-       if (!stc->carg) {
-               stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
-               if (stc->carg == NULL) { //LCOV_EXCL_LINE
-                       g_free(default_ifname); //LCOV_EXCL_LINE
-                       return; //LCOV_EXCL_LINE
-               }
-
-               stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
-       }
-
-       memset(&counter, 0, sizeof(struct nfacct_rule));
-
-       counter.carg = stc->carg;
-       counter.classid = classid;
-       counter.intend = intend;
-
-       if (connection && connection->tether_iface.ifname != NULL &&
-               classid == STC_TETHERING_APP_CLASSID) {
-               counter.iftype = connection->tether_iface.type;
-               g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
-       } else {
-               counter.iftype = iftype;
-               g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
-       }
-
-       g_free(default_ifname);
-
-       /* iptables rule */
-       __add_iptables_in(&counter);
-       __add_iptables_out(&counter);
-
-       /* ip6tables rule */
-       __add_ip6tables_in(&counter);
-       __add_ip6tables_out(&counter);
-}
-
-static void __add_tethering_iptables_rule(int64_t classid, gchar *mac,
-               nfacct_rule_intend intend, stc_iface_type_e iftype)
-{
-       default_connection_s *connection = stc_get_default_connection();
-       struct nfacct_rule counter;
-       stc_s *stc = stc_get_manager();
-       char *ipaddr = NULL;
-       int ret;
-
-       if (!stc || !mac)
-               return;
-
-       if (!stc->carg) {
-               stc->carg = MALLOC0(counter_arg_s, 1);
-               if (stc->carg == NULL)
-                       return;
-
-               stc->carg->sock = stc_monitor_get_counter_socket();
-       }
-
-       memset(&counter, 0, sizeof(struct nfacct_rule));
-
-       counter.carg = stc->carg;
-       counter.classid = classid;
-       counter.intend = intend;
-
-       if (connection->tether_state != TRUE ||
-               connection->tether_iface.ifname == NULL)
-       return;
-
-       counter.iftype = connection->tether_iface.type;
-       g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
-
-       /* get connected station ip based on its mac */
-       ret = stc_plugin_tether_get_station_ip(mac, &ipaddr);
-       if (ret != STC_ERROR_NONE)
-               return;
-
-       /* tethering iptables rule */
-       __add_iptables_tether_in(&counter, ipaddr);
-       __add_iptables_tether_out(&counter, ipaddr);
-       g_free(ipaddr);
-}
-
-static void __del_tethering_iptables_rule(int64_t classid, gchar *mac,
-               nfacct_rule_intend intend, stc_iface_type_e iftype)
-{
-       default_connection_s *connection = stc_get_default_connection();
-       struct nfacct_rule counter;
-       stc_s *stc = stc_get_manager();
-       char *ipaddr = NULL;
-       int ret;
-
-       if (!stc || !mac)
-               return;
-
-       if (!stc->carg) {
-               stc->carg = MALLOC0(counter_arg_s, 1);
-               if (stc->carg == NULL)
-                       return;
-
-               stc->carg->sock = stc_monitor_get_counter_socket();
-       }
-
-       memset(&counter, 0, sizeof(struct nfacct_rule));
-
-       counter.carg = stc->carg;
-       counter.classid = classid;
-       counter.intend = intend;
-
-       if (connection->tether_state != TRUE ||
-               connection->tether_iface.ifname == NULL)
-       return;
-
-       counter.iftype = connection->tether_iface.type;
-       g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
-
-       /* get connected station ip based on its mac */
-       ret = stc_plugin_tether_get_station_ip(mac, &ipaddr);
-       if (ret != STC_ERROR_NONE) {
-               STC_LOGE("Error: no IP found for station mac(%s)", mac);
-               return;
-       }
-
-       /* tethering iptables rule */
-       __del_iptables_tether_in(&counter, ipaddr);
-       __del_iptables_tether_out(&counter, ipaddr);
-       g_free(ipaddr);
-}
-
-static void __del_iptables_rule(int64_t classid, nfacct_rule_intend intend,
-                               stc_iface_type_e iftype)
-{
-       char *default_ifname = stc_default_connection_get_ifname();
-       default_connection_s *connection = stc_get_default_connection();
-       struct nfacct_rule counter;
-       stc_s *stc = stc_get_manager();
-       if (!stc) {
-               g_free(default_ifname); //LCOV_EXCL_LINE
-               return; //LCOV_EXCL_LINE
-       }
-
-       if (!stc->carg) {
-               stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
-               if (stc->carg == NULL) { //LCOV_EXCL_LINE
-                       g_free(default_ifname); //LCOV_EXCL_LINE
-                       return; //LCOV_EXCL_LINE
-               }
-
-               stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
-       }
-
-       memset(&counter, 0, sizeof(struct nfacct_rule));
-
-       counter.carg = stc->carg;
-       counter.classid = classid;
-       counter.intend = intend;
-
-       if (connection && connection->tether_iface.ifname != NULL &&
-               classid == STC_TETHERING_APP_CLASSID) {
-               counter.iftype = connection->tether_iface.type;
-               g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
-       } else {
-               counter.iftype = iftype;
-               g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
-       }
-
-       g_free(default_ifname);
-
-       /* iptables rule */
-       __del_iptables_in(&counter);
-       __del_iptables_out(&counter);
-
-       /* ip6tables rule */
-       __del_ip6tables_in(&counter);
-       __del_ip6tables_out(&counter);
-}
-
-static void __set_rstn_noti_state(int value)
-{
-       int state = STC_RSTN_STATE_INIT;
-
-       if (__vconf_get_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, &state))
-               return;
-
-       if (state == value) {
-               STC_LOGI("No need to change a restriction status: %d", state);
-               return;
-       }
-
-       vconf_set_int(VCONFKEY_SETAPPL_DATA_RESTRICTION_INT, value);
-       return;
-}
-
-typedef struct {
-       time_t month_start_ts;
-       time_t week_start_ts;
-       time_t day_start_ts;
-       int64_t monthly_stat;
-       int64_t weekly_stat;
-       int64_t daily_stat;
-} cumulative_data_s;
-
-static stc_cb_ret_e __statistics_info_cb(const table_statistics_info *info,
-                                       void *user_data)
-{
-       cumulative_data_s *stat = (cumulative_data_s *)user_data;
-       int64_t counters = 0;
-
-       counters = info->cnt.in_bytes + info->cnt.out_bytes;
-
-       stat->monthly_stat += counters;
-       if (stat->week_start_ts <= info->interval->from)
-               stat->weekly_stat += counters;
-       if (stat->day_start_ts <= info->interval->from)
-               stat->daily_stat += counters;
-
-       return STC_CONTINUE;
-}
-
-static void __process_tethering_restriction(enum traffic_restriction_type rstn_type,
-               stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value, void *data)
-{
-       default_connection_s *old_connection = (default_connection_s *)data;
-       default_connection_s *connection = NULL;
-       char *mac_str = NULL;
-
-       if (old_connection != NULL)
-               connection = old_connection;
-       else
-               connection = stc_get_default_connection();
-
-       /* in case tethering is not active */
-       if (connection->tether_state == FALSE)
-               return;
-
-       /* rstn not applicable for this interface */
-       if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
-                       (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
-               return;
-
-       /* in case appid not a tethering app */
-       if (!g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX))
-               return;
-
-       /* Ignore TOTAL_TETHERING,
-        * Process only station appids */
-       if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
-               return;
-
-       /* get the station mac based on classid */
-       stc_plugin_tether_get_station_by_classid(rstn_value->classid, &mac_str);
-       if (!mac_str) {
-               STC_LOGE("station not found for classid(%d)", rstn_value->classid);
-               return;
-       }
-
-       switch (rstn_type) {
-       case RST_SET:
-       {
-               int i;
-               table_counters_info info;
-               int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
-
-                       memset(&info, 0, sizeof(table_counters_info));
-                       rstn_value->limit_exceeded = 0;
-
-                       if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
-                                               rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
-                                       (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
-                                        rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
-                                       (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
-                                        rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
-                                       (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
-                                        rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
-                                       (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
-                                        rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
-                               table_counters_get(rstn_value->restriction_id, &info);
-
-                               time_t current_time = 0;
-                               cumulative_data_s stat;
-                               table_statistics_select_rule rule;
-
-                               memset(&stat, 0, sizeof(cumulative_data_s));
-                               stat.month_start_ts = rstn_value->month_start_ts;
-                               stat.week_start_ts = g_system->last_week_ts;
-                               stat.day_start_ts = g_system->last_day_ts;
-
-                               memset(&rule, 0, sizeof(table_statistics_select_rule));
-                               rule.from = rstn_value->month_start_ts;
-                               time(&current_time);
-                               rule.to = current_time;
-                               rule.iftype = rstn_key->iftype;
-                               rule.granularity = GRANULARITY;
-
-                               table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat);
-
-                               rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
-                               rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
-                               rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
-                               rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
-                               rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
-                       }
-
-                       for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
-                               if (rstn_value->limit[i] >= 0) {
-                                       effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i];
-
-                                       if (effective_limit[i] < 0)
-                                               rstn_value->limit_exceeded |= (1 << i);
-                               }
-                       }
-
-                       STC_LOGD("rstn_id [%llu], datausage [%llu] bytes",
-                                       rstn_value->restriction_id, info.data_counter);
-
-                       if (rstn_value->limit_exceeded != 0 &&
-                                       rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
-                               __add_tethering_iptables_rule(rstn_value->classid, mac_str,
-                                               NFACCT_TETH_BLOCK, rstn_key->iftype);
-                       }
-
-                       rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
-       }
-       break;
-       case RST_EXCLUDE:
-       {
-               __add_tethering_iptables_rule(rstn_value->classid, mac_str,
-                                       NFACCT_TETH_ALLOW, rstn_key->iftype);
-
-                       rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
-                       rstn_value->limit_exceeded = 0;
-                       rstn_value->limit_notified = 0;
-       }
-       break;
-       case RST_UNSET:
-       {
-                       int i;
-                       __del_tethering_iptables_rule(rstn_value->classid, mac_str,
-                                       NFACCT_TETH_BLOCK, rstn_key->iftype);
-
-                       rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
-                       rstn_value->limit_exceeded = 0;
-                       rstn_value->limit_notified = 0;
-
-                       for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
-                               if (rstn_value->limit[i] >= 0)
-                                       rstn_value->counter[i] = 0;
-       }
-       break;
-       default:
-                       ;//Do Nothing
-       }
-       FREE(mac_str);
-}
-
-static void __process_restriction(enum traffic_restriction_type rstn_type,
-                                 stc_rstn_key_s *rstn_key,
-                                 stc_rstn_value_s *rstn_value, void *data)
-{
-       default_connection_s *old_connection = (default_connection_s *)data;
-       default_connection_s *connection = NULL;
-
-       if (old_connection != NULL)
-               connection = old_connection;
-       else
-               connection = stc_get_default_connection();
-
-       /* no default ifname */
-       if (connection->ifname == NULL)
-               return;
-
-       /* rstn not applicable for this interface */
-       if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
-           (g_strcmp0(connection->ifname, rstn_key->ifname) != 0) &&
-               (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
-               return;
-
-       /* classid is invalid */
-       if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
-               return;
-
-       /* Do not proceed for tethering station appid if found here,
-        * for tethering station apps __process_tethering_restriction() call
-        * will handle it */
-       if (g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX) &&
-                       rstn_value->classid != STC_TETHERING_APP_CLASSID)
-               return;
-
-       switch (rstn_type) {
-       case RST_SET:
-       {
-               int i;
-               table_counters_info info;
-               int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
-
-               memset(&info, 0, sizeof(table_counters_info));
-               rstn_value->limit_exceeded = 0;
-
-               if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
-                       rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
-                       (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
-                       rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
-                       (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
-                       rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
-                       (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
-                       rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
-                       (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
-                       rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
-                       table_counters_get(rstn_value->restriction_id, &info);
-
-                       time_t current_time = 0;
-                       cumulative_data_s stat;
-                       table_statistics_select_rule rule;
-
-                       memset(&stat, 0, sizeof(cumulative_data_s));
-                       stat.month_start_ts = rstn_value->month_start_ts;
-                       stat.week_start_ts = g_system->last_week_ts;
-                       stat.day_start_ts = g_system->last_day_ts;
-
-                       memset(&rule, 0, sizeof(table_statistics_select_rule));
-                       rule.from = rstn_value->month_start_ts;
-                       time(&current_time);
-                       rule.to = current_time;
-                       rule.iftype = rstn_key->iftype;
-                       rule.granularity = GRANULARITY;
-
-                       table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat);
-
-                       rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
-                       rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
-                       rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
-                       rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
-                       rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
-               }
-
-               for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
-                       if (rstn_value->limit[i] >= 0) {
-                               effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i];
-
-                               if (effective_limit[i] < 0)
-                                       rstn_value->limit_exceeded |= (1 << i);
-                       }
-               }
-
-               STC_LOGD("rstn_id [%llu], datausage [%llu] bytes",
-                       rstn_value->restriction_id, info.data_counter);
-
-               if (rstn_value->limit_exceeded != 0 &&
-                       rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
-                       __add_iptables_rule(rstn_value->classid, NFACCT_BLOCK, rstn_key->iftype);
-               }
-
-               rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
-       }
-       break;
-       case RST_EXCLUDE:
-               __add_iptables_rule(rstn_value->classid, NFACCT_ALLOW,
-                                   rstn_key->iftype);
-
-               rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
-               rstn_value->limit_exceeded = 0;
-               rstn_value->limit_notified = 0;
-               break;
-       case RST_UNSET:
-       {
-               int i;
-
-               if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
-                       __del_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
-                                           rstn_key->iftype);
-               else
-                       __del_iptables_rule(rstn_value->classid, rstn_value->rstn_type,
-                                           rstn_key->iftype);
-
-               rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
-               rstn_value->limit_exceeded = 0;
-               rstn_value->limit_notified = 0;
-
-               for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
-                       if (rstn_value->limit[i] >= 0)
-                               rstn_value->counter[i] = 0;
-
-               __set_rstn_noti_state(STC_RSTN_STATE_UNSET);
-       }
-       break;
-       default:
-               ;//Do Nothing
-       }
-}
-
-//LCOV_EXCL_START
-static gboolean __remove_rstns_foreach_application(gpointer key,
-                                                  gpointer value,
-                                                  gpointer data)
-{
-       stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
-       stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
-       gchar *app_id = (gchar *)data;
-
-       /* rstn rule is not for applications */
-       if (rstn_key->app_id == NULL)
-               goto out;
-
-       /* rstn rule is not for this application */
-       if (g_strcmp0(rstn_key->app_id, app_id) != 0)
-               goto out;
-
-       /* rstn rule is already removed */
-       if (rstn_value->rstn_state == STC_RSTN_STATE_DEACTIVATED)
-               goto out;
-
-       /* remove restriction from system */
-       __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
-
-       /* remove tethering restriction from system*/
-       __process_tethering_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
-
-       __print_rstn(rstn_key, rstn_value);
-out:
-       return FALSE;
-}
-//LCOV_EXCL_STOP
-
-static void __remove_rstns_for_application(gchar *app_id)
-{
-       g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
-                      app_id);
-}
-
-static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
-{
-       stc_error_e ret = STC_ERROR_NONE;
-       guint pid_count = 0;
-       stc_app_value_s *lookup;
-
-       ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
-
-       lookup = __application_lookup(g_system->apps, app_key);
-       if (!lookup) {
-               STC_LOGE("app_key not found"); //LCOV_EXCL_LINE
-               return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
-       }
-
-       g_tree_foreach(lookup->processes, __processes_tree_check_empty,
-                      &pid_count);
-
-       if (!pid_count) {
-               /* remove nfacct rule for this classid */
-               __remove_application_monitor((gpointer) app_key, lookup,
-                                            stc_get_default_connection());
-               __remove_rstns_for_application(app_key->app_id);
-       }
-
-       if (!g_tree_remove(g_system->apps, app_key)) {
-               ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
-               STC_LOGE("key not found"); //LCOV_EXCL_LINE
-       }
-
-       return ret;
-}
-
-static stc_error_e __close_contr_sock(stc_system_s *system)
-{
-       ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
-
-       /* close netlink socket for updating kernel counters */
-       if (system->contr_sock != -1) {
-               close(system->contr_sock);
-               system->contr_sock = -1;
-       }
-
-       if (system->contr_gsource_id != 0) {
-               g_source_remove(system->contr_gsource_id);
-               system->contr_gsource_id = 0;
-       }
-
-       return STC_ERROR_NONE;
-}
-
-static gboolean __process_contr_reply(GIOChannel *source,
-                                     GIOCondition condition,
-                                     gpointer user_data);
-
-//LCOV_EXCL_START
-static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
-{
-       GIOChannel *gio = NULL;
-       ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
-
-       /* close netlink socket for updating kernel counters */
-       if (system->contr_sock != -1) {
-               close(system->contr_sock);
-               system->contr_sock = -1;
-       }
-
-       if (system->contr_gsource_id != 0) {
-               g_source_remove(system->contr_gsource_id);
-               system->contr_gsource_id = 0;
-       }
-
-       /* create netlink socket for updating kernel counters */
-       system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
-       if (system->contr_sock < 0) {
-               STC_LOGE("failed to open socket");
-               FREE(system);
-               return STC_ERROR_FAIL;
-       }
-
-       gio = g_io_channel_unix_new(system->contr_sock);
-       system->contr_gsource_id =
-               g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
-                              (GIOFunc) __process_contr_reply,
-                              NULL);
-       g_io_channel_unref(gio);
-
-       return STC_ERROR_NONE;
-}
-
-static void __action_when_rstn_limit_exceeded_tethering(stc_rstn_key_s *rstn_key,
-               stc_rstn_value_s *rstn_value, classid_bytes_context_s *context)
-{
-       char *mac_str = NULL;
-       struct nfacct_rule *counter = context->counter;
-
-       /* get the station mac based on classid */
-       stc_plugin_tether_get_station_by_classid(counter->classid, &mac_str);
-       if (!mac_str) {
-               STC_LOGE("station not found for classid(%d)", counter->classid);
-               return;
-       }
-
-       STC_LOGI("station mac %s, classid %u, iftype %u, iotype %d, \
-                       intend %d, ifname %s, bytes %lld", mac_str,
-                       counter->classid, counter->iftype, counter->iotype,
-                       counter->intend, counter->ifname, context->bytes);
-
-       /* Block tethering station immediately */
-       counter->intend = NFACCT_TETH_BLOCK;
-       __del_tethering_iptables_rule(counter->classid, mac_str,
-                       NFACCT_TETH_BLOCK, rstn_key->iftype);
-
-       __add_tethering_iptables_rule(counter->classid, mac_str,
-                       NFACCT_TETH_BLOCK, rstn_key->iftype);
-       counter->intend = NFACCT_TETH_COUNTER;
-
-       g_free(mac_str);
-}
-
-static void __action_when_rstn_limit_exceeded(stc_rstn_limit_type_e limit_type,
-                                               stc_rstn_key_s *rstn_key,
-                                               stc_rstn_value_s *rstn_value,
-                                               classid_bytes_context_s *context)
-{
-       gboolean rv;
-       char iftype[MAX_INT_LENGTH] = { 0, };
-       char byte[MAX_INT_LENGTH] = { 0, };
-       const char *signal_name = NULL;
-       const char *net_popup_content = NULL;
-       const char *net_popup_type = NULL;
-       stc_s *stc = (stc_s *)stc_get_manager();
-
-       if (stc == NULL) {
-               STC_LOGE("Failed to get stc data");
-               return;
-       }
-
-       switch (limit_type) {
-       case STC_RSTN_LIMIT_TYPE_DATA_WARN:
-       {
-               signal_name = "WarnThresholdCrossed";
-               net_popup_content = "warn threshold crossed";
-               net_popup_type = "warning_noti";
-       }
-       break;
-       case STC_RSTN_LIMIT_TYPE_DATA:
-       case STC_RSTN_LIMIT_TYPE_MONTHLY:
-       case STC_RSTN_LIMIT_TYPE_WEEKLY:
-       case STC_RSTN_LIMIT_TYPE_DAILY:
-       {
-               signal_name = "RestrictionThresholdCrossed";
-               net_popup_content = "restriction threshold crossed";
-               net_popup_type = "restriction_noti";
-
-               /* Apply restriction for tethering apps if app_id is of tethering client
-                * otherwise do the normal iptables rule */
-               if (context->counter->intend == NFACCT_TETH_COUNTER) {
-
-                       if (g_str_has_suffix(rstn_key->app_id, STC_TETHERING_APP_SUFFIX) &&
-                                       rstn_value->classid != STC_TETHERING_APP_CLASSID) {
-                               __action_when_rstn_limit_exceeded_tethering(rstn_key, rstn_value,
-                                               context);
-                       }
-
-               } else {
-                       /* block immediately */
-                       context->counter->intend = NFACCT_BLOCK;
-                       __del_iptables_in(context->counter);
-                       __del_iptables_out(context->counter);
-                       __add_iptables_in(context->counter);
-                       __add_iptables_out(context->counter);
-
-                       __del_ip6tables_in(context->counter);
-                       __del_ip6tables_out(context->counter);
-                       __add_ip6tables_in(context->counter);
-                       __add_ip6tables_out(context->counter);
-                       context->counter->intend = NFACCT_COUNTER;
-               }
-
-               rstn_value->limit_exceeded |= (1 << limit_type);
-
-               __set_rstn_noti_state(STC_RSTN_STATE_SET);
-       }
-       break;
-       default:
-               break;
-       }
-
-       if (signal_name == NULL) {
-               STC_LOGE("Invalid parameter: limit_type");
-               return;
-       }
-
-       /* emit signal */
-       rv = stc_manager_dbus_emit_signal(stc->connection,
-                                                 STC_DBUS_SERVICE_RESTRICTION_PATH,
-                                                 STC_DBUS_INTERFACE_RESTRICTION,
-                                                 signal_name,
-                                                 g_variant_new("(si)",
-                                                 rstn_key->app_id,
-                                                 rstn_key->iftype));
-
-       if (rv == TRUE)
-               rstn_value->limit_notified |= (1 << limit_type);
-
-       snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
-       snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit[limit_type]);
-       stc_plugin_appstatus_send_message(net_popup_content,
-                       net_popup_type, rstn_key->app_id, iftype, byte);
-}
-
-static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
-                                     stc_rstn_value_s *rstn_value,
-                                     classid_bytes_context_s *context)
-{
-       int i;
-       switch (context->counter->iotype) {
-       case NFACCT_COUNTER_IN:
-       case NFACCT_COUNTER_OUT:
-               if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
-                       rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
-                       (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
-                       rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
-                       (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
-                       rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
-                       (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
-                       rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
-                       (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
-                       rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
-                       table_counters_info info;
-                       memset(&info, 0, sizeof(table_counters_info));
-                       table_counters_get(rstn_value->restriction_id, &info);
-
-                       rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
-                       rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
-                       rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter;
-                       rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter;
-                       rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter;
-               }
-
-               for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
-                       if (rstn_value->limit[i] >= 0 &&
-                               !(rstn_value->limit_notified & (1 << i))) {
-                               rstn_value->counter[i] += context->bytes;
-                               if (rstn_value->limit[i] <= rstn_value->counter[i])
-                                       __action_when_rstn_limit_exceeded(i,
-                                                                               rstn_key,
-                                                                               rstn_value,
-                                                                               context);
-                       }
-               }
-
-               g_system->rstns_tree_updated = TRUE;
-               __print_rstn(rstn_key, rstn_value);
-               break;
-       default:
-               STC_LOGE("unknown iotype");
-       }
-
-       return FALSE;
-}
-
-static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
-                                               stc_rstn_value_s *rstn_value,
-                                               classid_bytes_context_s *context)
-{
-       if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
-               context->counter->iftype == STC_IFACE_DATACALL) ||
-               (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
-               context->counter->iftype == STC_IFACE_WIFI) ||
-               (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
-               context->counter->iftype == STC_IFACE_BLUETOOTH) ||
-               (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
-                context->counter->iftype == STC_IFACE_WIFI) ||
-               (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
-                context->counter->iftype == STC_IFACE_BLUETOOTH) ||
-               (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
-                context->counter->iftype == STC_IFACE_USB) ||
-               (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
-                context->counter->iftype == STC_IFACE_P2P)) {
-               context->counter->classid = rstn_value->classid;
-               return __rstn_counter_update(rstn_key, rstn_value, context);
-       }
-
-       return FALSE;
-}
-
-static gboolean __rstn_counter_update_foreach_classid(gpointer key,
-                                                     gpointer value,
-                                                     gpointer data)
-{
-       gboolean rv = FALSE;
-       stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
-       stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
-       classid_bytes_context_s *context = (classid_bytes_context_s *)data;
-       uint32_t classid;
-
-       if (context->counter->intend != NFACCT_COUNTER &&
-                       context->counter->intend != NFACCT_TETH_COUNTER)
-               goto try_next_callback;
-
-       if (rstn_value->limit_exceeded == TRUE) {
-               context->data_limit_exceeded = TRUE; //LCOV_EXCL_LINE
-               goto try_next_callback; //LCOV_EXCL_LINE
-       }
-
-       classid = context->counter->classid;
-       rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
-
-       context->counter->classid = classid;
-       if (rstn_value->classid != context->counter->classid)
-               goto try_next_callback;
-
-       rv = __rstn_counter_update(rstn_key, rstn_value, context);
-
-try_next_callback:
-       return rv;
-}
-//LCOV_EXCL_STOP
-
-static gboolean __update_app_statistics(gpointer key, gpointer value,
-                                       gpointer data)
-{
-       stc_app_key_s *app_key = (stc_app_key_s *)key;
-       stc_app_value_s *app_value = (stc_app_value_s *)value;
-       time_t *touch_time = (time_t *)data;
-       stc_db_classid_iftype_key stat_key;
-       stc_db_app_stats stat;
-       default_connection_s *default_connection = stc_get_default_connection();
-
-       memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
-       memset(&stat, 0 , sizeof(stc_db_app_stats));
-
-       /* Do not update statistics for Tethering
-        * if tethering is in-active found */
-       if (default_connection &&
-               default_connection->tether_state == FALSE &&
-               !strcmp(app_key->app_id, STC_TOTAL_TETHERING))
-               return FALSE;
-
-       /* Do not update statistics for Wi-Fi
-        * if tethering is active on wlan0 iface */
-       if (default_connection && default_connection->tether_state &&
-               default_connection->tether_iface.type == STC_IFACE_WIFI &&
-               !strcmp(app_key->app_id, STC_TOTAL_WIFI))
-               return FALSE;
-
-       stat_key.classid = app_value->classid;
-
-       if (app_value->classid == STC_TETHERING_APP_CLASSID &&
-               default_connection->tether_state == TRUE)
-               stat_key.iftype = default_connection->tether_iface.type;
-       else if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX))
-               stat_key.iftype = default_connection->tether_iface.type;
-       else
-               stat_key.iftype = default_connection->type;
-
-       if (STC_IFACE_DATACALL == stat_key.iftype)
-               stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
-       else
-               stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
-
-       if (app_value->classid == STC_TETHERING_APP_CLASSID &&
-               default_connection->tether_state == TRUE)
-               g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
-                         MAX_IFACE_LENGTH);
-       else if (g_str_has_suffix(app_key->app_id, STC_TETHERING_APP_SUFFIX))
-               g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
-                         MAX_IFACE_LENGTH);
-       else
-               g_strlcpy(stat_key.ifname, default_connection->ifname,
-                         MAX_IFACE_LENGTH);
-
-       stat.app_id = g_strdup(app_key->app_id);
-       stat.snd_count = app_value->counter.out_bytes;
-       stat.rcv_count = app_value->counter.in_bytes;
-       stat.is_roaming = default_connection->roaming;
-
-       if (strstr(stat.app_id, "_BACKGROUND")) {
-               stat.ground = STC_APP_STATE_BACKGROUND;
-       } else {
-               if (strstr(stat.app_id, "TOTAL_"))
-                       stat.ground = STC_APP_STATE_UNKNOWN;
-               else
-                       stat.ground = STC_APP_STATE_FOREGROUND;
-       }
-
-       table_statistics_insert(&stat_key, &stat, *touch_time);
-
-       app_value->counter.out_bytes = 0;
-       app_value->counter.in_bytes = 0;
-
-       FREE(stat.app_id);
-       FREE(stat_key.subscriber_id);
-
-       return FALSE;
-}
-
-static gboolean __flush_apps_stats_to_database(gpointer user_data)
-{
-       time_t current_time = 0;
-       stc_s *stc = stc_get_manager();
-
-       if (stc && stc->carg)
-               current_time = stc->carg->last_run_time;
-
-       if (g_system->apps_tree_updated == FALSE)
-               return G_SOURCE_REMOVE;
-
-       g_system->apps_tree_updated = FALSE;
-
-       if (g_system->apps)
-               g_tree_foreach(g_system->apps,
-                              __update_app_statistics,
-                              &current_time);
-
-       STC_LOGI("Flushed app stats to database");
-       return G_SOURCE_REMOVE;
-}
-
-//LCOV_EXCL_START
-static gboolean __update_counter_statistics(gpointer key, gpointer value,
-                                           gpointer data)
-{
-       stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
-       table_counters_info info = {
-               .restriction_id = rstn_value->restriction_id,
-               .data_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
-               .warn_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
-               .monthly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
-               .weekly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
-               .daily_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY]
-       };
-
-       table_counters_update_counters(&info);
-
-       return FALSE;
-}
-
-static gboolean __flush_rstns_counter_to_database(gpointer user_data)
-{
-       time_t current_time = 0;
-       stc_s *stc = stc_get_manager();
-
-       if (stc && stc->carg)
-               current_time = stc->carg->last_run_time;
-
-       if (g_system->rstns_tree_updated == FALSE)
-               return G_SOURCE_REMOVE;
-
-       g_system->rstns_tree_updated = FALSE;
-
-       if (g_system->rstns)
-               g_tree_foreach(g_system->rstns,
-                              __update_counter_statistics,
-                              &current_time);
-
-       STC_LOGI("Flushed rstns counters to database");
-       return G_SOURCE_REMOVE;
-}
-//LCOV_EXCL_STOP
-
-static void __app_counter_update(stc_app_key_s *app_key,
-                                stc_app_value_s *app_value,
-                                classid_bytes_context_s *context)
-{
-       switch (context->counter->iotype) {
-       case NFACCT_COUNTER_IN:
-               app_value->data_usage.in_bytes += context->bytes;
-               app_value->counter.in_bytes = context->bytes;
-               g_system->apps_tree_updated = TRUE;
-
-               /*
-               __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
-               */
-               break;
-       case NFACCT_COUNTER_OUT:
-               app_value->data_usage.out_bytes += context->bytes;
-               app_value->counter.out_bytes = context->bytes;
-               g_system->apps_tree_updated = TRUE;
-
-               /*
-               __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
-               */
-               break;
-       default:
-               STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
-       }
-}
-
-static void __interface_counter_update(stc_app_key_s *app_key,
-                                      stc_app_value_s *app_value,
-                                      classid_bytes_context_s *context)
-{
-       if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
-            context->counter->iftype == STC_IFACE_DATACALL) ||
-           (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
-            context->counter->iftype == STC_IFACE_WIFI) ||
-           (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
-            context->counter->iftype == STC_IFACE_BLUETOOTH) ||
-               (app_value->classid == STC_TETHERING_APP_CLASSID &&
-                context->counter->iftype == STC_IFACE_WIFI) ||
-               (app_value->classid == STC_TETHERING_APP_CLASSID &&
-                context->counter->iftype == STC_IFACE_BLUETOOTH) ||
-               (app_value->classid == STC_TETHERING_APP_CLASSID &&
-                context->counter->iftype == STC_IFACE_USB) ||
-               (app_value->classid == STC_TETHERING_APP_CLASSID &&
-                context->counter->iftype == STC_IFACE_P2P))
-                __app_counter_update(app_key, app_value, context);
-}
-
-
-static gboolean __apps_counter_update_foreach_classid(gpointer key,
-                                                     gpointer value,
-                                                     gpointer data)
-{
-       stc_app_key_s *app_key = (stc_app_key_s *)key;
-       stc_app_value_s *app_value = (stc_app_value_s *)value;
-       classid_bytes_context_s *context = (classid_bytes_context_s *)data;
-
-       if (context->counter->intend != NFACCT_COUNTER &&
-                       context->counter->intend != NFACCT_TETH_COUNTER)
-               goto try_next_callback;
-
-       __interface_counter_update(app_key, app_value, context);
-
-       if (app_value->classid != context->counter->classid)
-               goto try_next_callback;
-
-       __app_counter_update(app_key, app_value, context);
-
-try_next_callback:
-       return FALSE;
-}
-
-static gboolean __reset_time_counter_foreach_rstn(gpointer key,
-                                                 gpointer value,
-                                                 gpointer data)
-{
-       stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
-       stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
-       reset_time_limits_context_s *context = (reset_time_limits_context_s *)data;
-       int i;
-       time_t now_month_start_ts;
-
-       if (rstn_value->month_start_date == 0) {
-               table_counters_info info;
-               memset(&info, 0, sizeof(table_counters_info));
-               table_counters_get_timestamps(rstn_value->restriction_id, &info);
-
-               if (info.month_start_date == 0)
-                       rstn_value->month_start_date = 1;
-               else
-                       rstn_value->month_start_date = info.month_start_date;
-               rstn_value->month_start_ts = info.month_start_ts;
-       }
-
-       now_month_start_ts =
-               stc_time_get_month_start(context->now,
-                                        rstn_value->month_start_date);
+       int ret = 0;
 
-       if (rstn_value->month_start_ts != now_month_start_ts) {
-               rstn_value->month_start_ts = now_month_start_ts;
-               context->month_start_ts = now_month_start_ts;
-               context->is_updated |= (1 << STC_RSTN_LIMIT_TYPE_MONTHLY);
+       ret = vconf_get_int(key, value);
+       if (ret != VCONF_OK) {
+               STC_LOGE("Failed to get vconfkey [%s] value", key);
+               return -1;
        }
 
-       if (context->is_updated) {
-               table_counters_info info;
-               memset(&info, 0, sizeof(table_counters_info));
+       return 0;
+}
 
-               info.restriction_id = rstn_value->restriction_id;
-               info.month_start_date = rstn_value->month_start_date;
-               info.month_start_ts = rstn_value->month_start_ts;
-               info.week_start_ts = context->week_start_ts;
-               info.day_start_ts = context->day_start_ts;
+static stc_error_e __close_contr_sock(stc_system_s *system)
+{
+       ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
 
-               table_counters_update_timestamps(&info);
+       /* close netlink socket for updating kernel counters */
+       if (system->contr_sock != -1) {
+               close(system->contr_sock);
+               system->contr_sock = -1;
        }
 
-       for (i = STC_RSTN_LIMIT_TYPE_MONTHLY; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
-
-               if ((context->is_updated) & (1 << i)) {
-                       /* reset limit */
-                       rstn_value->counter[i] = 0;
-
-                       if (rstn_value->limit_exceeded & (1 << i)) {
-                               /* remove iptables rule */
-                               char *default_ifname = stc_default_connection_get_ifname();
-                               struct nfacct_rule counter;
-                               stc_s *stc = stc_get_manager();
-                               if (stc == NULL) {
-                                       STC_LOGE("Can't get stc data");
-                                       g_free(default_ifname);
-                                       goto try_next_callback;
-                               }
-
-                               if (!stc->carg) {
-                                       stc->carg = MALLOC0(counter_arg_s, 1);
-                                       if (stc->carg == NULL) {
-                                               g_free(default_ifname);
-                                               goto try_next_callback;
-                                       }
-
-                                       stc->carg->sock =
-                                               stc_monitor_get_counter_socket();
-                               }
-
-                               memset(&counter, 0, sizeof(struct nfacct_rule));
-
-                               counter.carg = stc->carg;
-                               counter.classid = rstn_value->classid;
-                               counter.intend = NFACCT_BLOCK;
-                               counter.iftype = rstn_key->iftype;
-                               g_strlcpy(counter.ifname, default_ifname,
-                                         MAX_IFACE_LENGTH);
-
-                               g_free(default_ifname);
-
-                               /* iptables rule */
-                               __del_iptables_in(&counter);
-                               __del_iptables_out(&counter);
-
-                               /* ip6tables rule */
-                               __del_ip6tables_in(&counter);
-                               __del_ip6tables_out(&counter);
-
-                               rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
-                               rstn_value->limit_exceeded &= ~(1 << i);
-                               rstn_value->limit_notified &= ~(1 << i);
-                       }
-               }
+       if (system->contr_gsource_id != 0) {
+               g_source_remove(system->contr_gsource_id);
+               system->contr_gsource_id = 0;
        }
 
-try_next_callback:
-       return FALSE;
+       return STC_ERROR_NONE;
 }
 
-static void __reset_time_counters_if_required(void)
+static gboolean __process_contr_reply(GIOChannel *source,
+                                     GIOCondition condition,
+                                     gpointer user_data);
+
+static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
 {
-       reset_time_limits_context_s context;
+       GIOChannel *gio = NULL;
+       ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
 
-       if (g_system == NULL) {
-               STC_LOGE("stc monitor not initialized!");
-               return;
+       /* close netlink socket for updating kernel counters */
+       if (system->contr_sock != -1) {
+               close(system->contr_sock);
+               system->contr_sock = -1;
        }
 
-       context.now = time(NULL);
-       context.week_start_ts = stc_time_get_week_start(context.now);
-       context.day_start_ts = stc_time_get_day_start(context.now);
-       context.is_updated = 0;
-
-       if (g_system->last_week_ts != context.week_start_ts) {
-               g_system->last_week_ts = context.week_start_ts;
-               context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_WEEKLY);
+       if (system->contr_gsource_id != 0) {
+               g_source_remove(system->contr_gsource_id);
+               system->contr_gsource_id = 0;
        }
 
-       if (g_system->last_day_ts != context.day_start_ts) {
-               g_system->last_day_ts = context.day_start_ts;
-               context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_DAILY);
+       /* create netlink socket for updating kernel counters */
+       system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
+       if (system->contr_sock < 0) {
+               STC_LOGE("failed to open socket");
+               FREE(system);
+               return STC_ERROR_FAIL;
        }
 
-       if (g_system->rstns) {
-               g_tree_foreach(g_system->rstns,
-                              __reset_time_counter_foreach_rstn,
-                              &context);
-               if (context.is_updated)
-                       STC_LOGD("Counter reset completed month_start [%ld], week_start [%ld], day_start [%ld]",
-                                context.month_start_ts, g_system->last_week_ts, g_system->last_day_ts);
-       }
+       gio = g_io_channel_unix_new(system->contr_sock);
+       system->contr_gsource_id =
+               g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
+                              (GIOFunc) __process_contr_reply,
+                              NULL);
+       g_io_channel_unref(gio);
+
+       return STC_ERROR_NONE;
+}
+
+static void __check_rstn_limit_exceeded(gpointer data,
+                               gpointer user_data)
+{
+       stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
+       int32_t *limit_exceeded = (int32_t *)user_data;
+
+       if (rstn_data->limit_exceeded != 0)
+               *limit_exceeded = rstn_data->limit_exceeded;
 }
 
 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
-                                struct counter_arg *carg)
+                               struct counter_arg *carg)
 {
-       __reset_time_counters_if_required();
+       stc_monitor_rstn_reset_time_counters_if_required();
 
        struct nfacct_rule counter = {
                .carg = carg,
@@ -1991,25 +131,58 @@ static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
        };
 
        if (!recreate_counter_by_name(cnt_name, &counter)) {
-               STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
-               return; //LCOV_EXCL_LINE
+               STC_LOGE("Can't parse counter name %s", cnt_name);
+               return;
        }
 
        if (STC_DEBUG_LOG)
-               STC_LOGI("classid %u, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
+               STC_LOGI("classid[\033[1;36m%u\033[0;m] iftype[%u] "
+                       "iotype[%d] intend[%d] ifname[%s] bytes[%lld]",
                        context.counter->classid, context.counter->iftype,
                        context.counter->iotype, context.counter->intend,
                        context.counter->ifname, context.bytes);
 
-       if (g_system->rstns)
-               g_tree_foreach(g_system->rstns,
-                              __rstn_counter_update_foreach_classid,
-                              &context);
+       if (context.counter->intend == NFACCT_COUNTER ||
+               context.counter->intend == NFACCT_TETH_COUNTER) {
+               if (g_system->apps) {
+                       stc_app_value_s *lookup_app;
+                       stc_rstn_value_s *lookup_rstn;
+
+                       stc_monitor_app_update_iface_counter(&context);
+
+                       lookup_app = g_hash_table_lookup(g_system->apps,
+                                               GUINT_TO_POINTER(context.counter->classid));
+                       if (lookup_app)
+                               stc_monitor_app_update_counter(lookup_app, &context);
 
-       if (g_system->apps)
-               g_tree_foreach(g_system->apps,
-                              __apps_counter_update_foreach_classid,
-                              &context);
+                       lookup_rstn = g_hash_table_lookup(g_system->rstns,
+                                               GUINT_TO_POINTER(context.counter->classid));
+                       if (lookup_rstn) {
+                               int32_t limit_exceeded = 0;
+                               g_slist_foreach(lookup_rstn->rules,
+                                       __check_rstn_limit_exceeded, &limit_exceeded);
+
+                               if (limit_exceeded != 0)
+                                       return;
+                       }
+               }
+
+               if (g_system->rstns) {
+                       stc_rstn_value_s *lookup_value;
+                       uint32_t classid = context.counter->classid;
+
+                       stc_monitor_rstn_update_iface_counter(&context);
+                       context.counter->classid = classid;
+
+                       lookup_value = g_hash_table_lookup(g_system->rstns,
+                                                       GUINT_TO_POINTER(classid));
+                       if (lookup_value) {
+                               g_slist_foreach(lookup_value->rules,
+                                       stc_monitor_rstn_update_counter,
+                                       &context);
+                       }
+               }
+       }
 }
 
 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
@@ -2060,8 +233,8 @@ static void __process_network_counter(struct genl *ans,
        netlink_serialization_command *netlink =
                netlink_create_command(&ser_params);
        if (!netlink) {
-               STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
-               return; //LCOV_EXCL_LINE
+               STC_LOGE("Can not create command");
+               return;
        }
 
        netlink->deserialize_answer(&(netlink->params));
@@ -2085,27 +258,27 @@ static gboolean __process_contr_reply(GIOChannel *source,
            (condition & G_IO_NVAL)) {
                /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
 
-               STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
+               STC_LOGE("Counter socket received G_IO event, closing socket."
                         "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
                         (condition & G_IO_ERR), (condition & G_IO_HUP),
                         (condition & G_IO_NVAL));
-               __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
-               return FALSE; //LCOV_EXCL_LINE
+               __close_and_reopen_contr_sock(g_system);
+               return FALSE;
        }
 
        ans = MALLOC0(struct genl, 1);
        if (ans == NULL) {
-               STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
-               return TRUE; //LCOV_EXCL_LINE
+               STC_LOGE("Failed allocate memory to genl reply message");
+               return TRUE;
        }
 
        if (stc == NULL) {
-               STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
-               goto out; //LCOV_EXCL_LINE
+               STC_LOGE("Can't get stc data");
+               goto out;
        }
 
        ret = read_netlink(sock, ans, sizeof(struct genl));
-       /* STC_LOGD("Counter data received ret [%d]", ret); */
+
        if (ret == 0)
                goto out;
 
@@ -2114,8 +287,8 @@ static gboolean __process_contr_reply(GIOChannel *source,
 
        __process_network_counter(ans, stc->carg);
 
-       g_idle_add(__flush_apps_stats_to_database, NULL);
-       g_idle_add(__flush_rstns_counter_to_database, NULL);
+       g_idle_add(stc_monitor_app_flush_stats_to_db, NULL);
+       g_idle_add(stc_monitor_rstn_flush_contr_to_db, NULL);
 out:
 
        FREE(ans);
@@ -2132,7 +305,7 @@ static gboolean __update_contr_cb(void *user_data)
                if (stc->carg == NULL)
                        return TRUE;  /* we need to continue the timer */
 
-               stc->carg->sock = stc_monitor_get_counter_socket();
+               stc->carg->sock = g_system->contr_sock;
        }
 
 #ifdef TIZEN_GTESTS
@@ -2147,326 +320,6 @@ static gboolean __update_contr_cb(void *user_data)
        return TRUE;
 }
 
-/*
-//LCOV_EXCL_START
-static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
-                                         gpointer data)
-{
-       stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
-       stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
-
-       __print_rstn(rstn_key, rstn_value);
-       return FALSE;
-}
-
-static void __rstn_tree_printall(void)
-{
-       g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
-}
-//LCOV_EXCL_STOP
-*/
-
-static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
-                                       const stc_rstn_key_s *key)
-{
-       stc_rstn_value_s *lookup;
-
-       ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
-
-       lookup = g_tree_lookup(rstns_tree, key);
-
-       return lookup;
-}
-
-static gboolean __remove_restriction(gpointer key, gpointer value,
-                                    gpointer data)
-{
-       stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
-       stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
-
-       __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
-       __process_tethering_restriction(RST_UNSET, rstn_key, rstn_value, data);
-       __print_rstn(rstn_key, rstn_value);
-       return FALSE;
-}
-
-static gboolean __add_restriction_debug(gpointer key, gpointer value,
-                                       gpointer data)
-{
-       stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
-       stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
-
-       /* rstn rule is activated */
-       if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
-               return FALSE;
-
-       if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
-               __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
-               __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
-       } else {
-               __process_restriction(RST_SET, rstn_key, rstn_value, data);
-               __process_tethering_restriction(RST_SET, rstn_key, rstn_value, data);
-       }
-
-       __print_rstn(rstn_key, rstn_value);
-
-       return FALSE;
-}
-
-//LCOV_EXCL_START
-static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
-{
-       stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
-       stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
-
-       /* rstn rule is activated */
-       if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
-               return FALSE;
-
-       if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
-               __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
-               __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
-       } else {
-               __process_restriction(RST_SET, rstn_key, rstn_value, data);
-               __process_tethering_restriction(RST_SET, rstn_key, rstn_value, data);
-       }
-
-       return FALSE;
-}
-//LCOV_EXCL_STOP
-
-static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
-{
-       stc_rstn_value_s *lookup_value;
-
-       ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
-
-       lookup_value = __rstn_lookup(g_system->rstns, key);
-       if (!lookup_value) {
-               STC_LOGE("key not found"); //LCOV_EXCL_LINE
-               return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
-       }
-
-       /* remove counter also */
-       table_counters_delete(lookup_value->restriction_id);
-       __remove_restriction(key, lookup_value, NULL);
-
-       if (!g_tree_remove(g_system->rstns, key)) {
-               STC_LOGD("key not found"); //LCOV_EXCL_LINE
-               return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
-       }
-
-       return STC_ERROR_NONE;
-}
-
-static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
-                                  stc_rstn_value_s *value, gboolean debug)
-{
-       stc_rstn_key_s *rstn_key;
-       stc_rstn_value_s *rstn_value;
-
-       ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
-
-       rstn_value = __rstn_lookup(g_system->rstns, key);
-       if (rstn_value)
-               __rstn_tree_remove(key);
-
-       rstn_key = MALLOC0(stc_rstn_key_s, 1);
-       if (!rstn_key) {
-               STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
-               return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
-       }
-
-       rstn_value = MALLOC0(stc_rstn_value_s, 1);
-       if (!rstn_value) {
-               STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
-               FREE(rstn_key); //LCOV_EXCL_LINE
-               return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
-       }
-
-       rstn_key->app_id = g_strdup(key->app_id);
-       rstn_key->ifname = g_strdup(key->ifname);
-       rstn_key->mac = g_strdup(key->mac);
-       rstn_key->subscriber_id = g_strdup(key->subscriber_id);
-       rstn_key->iftype = key->iftype;
-       rstn_key->roaming = key->roaming;
-
-       g_tree_insert(g_system->rstns, rstn_key, rstn_value);
-
-       rstn_value->restriction_id = value->restriction_id;
-       rstn_value->rstn_state = value->rstn_state;
-       rstn_value->rstn_type = value->rstn_type;
-       rstn_value->classid = value->classid;
-
-       int i;
-       for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
-               rstn_value->limit[i] = value->limit[i];
-               rstn_value->counter[i] = 0;
-       }
-
-       rstn_value->limit_exceeded = 0;
-       rstn_value->limit_notified = 0;
-       rstn_value->month_start_date = value->month_start_date;
-       rstn_value->month_start_ts = value->month_start_ts;
-
-       if (debug == TRUE)
-               __add_restriction_debug(key, rstn_value, NULL);
-       else
-               __add_restriction(key, rstn_value, NULL);
-
-       return STC_ERROR_NONE;
-}
-
-//LCOV_EXCL_START
-static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
-                                           void *user_data)
-{
-       stc_cb_ret_e ret = STC_CONTINUE;
-
-       stc_rstn_key_s key;
-       stc_rstn_value_s value;
-
-       memset(&key, 0, sizeof(stc_rstn_key_s));
-       memset(&value, 0, sizeof(stc_rstn_value_s));
-
-       key.app_id = g_strdup(info->app_id);
-       key.ifname = g_strdup(info->ifname);
-       key.subscriber_id = g_strdup(info->subscriber_id);
-       key.iftype = info->iftype;
-       key.roaming = info->roaming;
-
-       value.rstn_type = info->rstn_type;
-       value.rstn_state = STC_RSTN_STATE_UNKNOWN;
-       value.restriction_id = info->restriction_id;
-
-       if (info->app_id)
-               value.classid = get_classid_by_app_id(info->app_id, TRUE);
-       else
-               value.classid = STC_UNKNOWN_CLASSID;
-
-       value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
-       value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
-       value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
-       value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
-       value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
-
-       if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
-               ret = STC_CANCEL;
-
-       FREE(key.app_id);
-       FREE(key.ifname);
-       FREE(key.subscriber_id);
-       return ret;
-}
-
-static void __fill_restritions_list(void)
-{
-       table_restrictions_foreach(__insert_restriction_cb, NULL);
-
-       /* __rstn_tree_printall(); */
-}
-
-static gboolean __add_rstn_foreach_application(gpointer key,
-                                              gpointer value,
-                                              gpointer data)
-{
-       stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
-       stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
-       gchar *app_id = (gchar *)data;
-
-       /* rstn rule is not for applications */
-       if (rstn_key->app_id == NULL)
-               goto out;
-
-       /* rstn rule is not for this application */
-       if (g_strcmp0(rstn_key->app_id, app_id) != 0)
-               goto out;
-
-       /* rstn rule is already applied */
-       if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
-               goto out;
-
-       /* add restriction to system */
-       if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
-               __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
-               __process_tethering_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
-       } else {
-               __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
-               __process_tethering_restriction(RST_SET, rstn_key, rstn_value, NULL);
-       }
-
-       __print_rstn(rstn_key, rstn_value);
-out:
-       return FALSE;
-}
-//LCOV_EXCL_STOP
-
-static void __add_rstns_for_application(gchar *app_id)
-{
-       g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
-                      app_id);
-}
-
-static void __add_application_by_interface(const char *app_id)
-{
-       stc_app_key_s app_key;
-       stc_app_value_s app_value;
-
-       if (app_id == NULL)
-               return; //LCOV_EXCL_LINE
-
-       memset(&app_key, 0, sizeof(stc_app_key_s));
-       memset(&app_value, 0, sizeof(stc_app_value_s));
-
-       app_key.pkg_id = g_strdup(app_id);
-       app_key.app_id = g_strdup(app_id);
-
-       app_value.type = STC_APP_TYPE_NONE;
-       app_value.processes = NULL;
-       app_value.counter.in_bytes = 0;
-       app_value.counter.out_bytes = 0;
-
-       stc_monitor_application_add(app_key, app_value);
-
-       FREE(app_key.pkg_id);
-       FREE(app_key.app_id);
-}
-
-static gboolean __processes_tree_foreach_background(gpointer key,
-                                                   gpointer value,
-                                                   gpointer data)
-{
-       stc_process_key_s *proc_key = (stc_process_key_s *)key;
-       stc_app_key_s *app_key = (stc_app_key_s *)data;
-
-       place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
-
-       return FALSE;
-}
-
-static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
-                                       gpointer data)
-{
-       stc_app_key_s *app_key = (stc_app_key_s *)key;
-       stc_app_value_s *app_value = (stc_app_value_s *)value;
-
-       if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
-               g_tree_foreach(app_value->processes,
-                              __processes_tree_foreach_background, app_key);
-
-       return FALSE;
-}
-
-static stc_error_e __process_update_background(void)
-{
-       ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
-
-       g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
-
-       return STC_ERROR_NONE;
-}
-//LCOV_EXCL_STOP
-
 static void __fill_exceptions_list(void)
 {
        stc_plugin_fill_exception_list();
@@ -2477,7 +330,8 @@ stc_error_e stc_monitor_init(void)
        stc_system_s *system = MALLOC0(stc_system_s, 1);
        GIOChannel *gio = NULL;
 
-       ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
+       ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY,
+                               "stc_system_s malloc fail!");
 
        /* initializing current classid */
        init_current_classid();
@@ -2486,20 +340,15 @@ stc_error_e stc_monitor_init(void)
        cgroup_init();
 
        /* creating monitored application tree */
-       system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
-                                      __apps_tree_key_free,
-                                      __apps_tree_value_free);
-
-       system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
-                                       __rstns_tree_key_free,
-                                       __rstns_tree_value_free);
+       system->apps = stc_monitor_apps_init();
+       system->rstns = stc_monitor_rstns_init();
 
        /* create netlink socket for updating kernel counters */
        system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
        if (system->contr_sock < 0) {
-               STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
-               FREE(system); //LCOV_EXCL_LINE
-               return STC_ERROR_FAIL; //LCOV_EXCL_LINE
+               STC_LOGE("failed to open socket");
+               FREE(system);
+               return STC_ERROR_FAIL;
        }
 
        gio = g_io_channel_unix_new(system->contr_sock);
@@ -2511,14 +360,13 @@ stc_error_e stc_monitor_init(void)
 
        g_system = system;
 
-       __add_application_by_interface(STC_TOTAL_DATACALL);
-       __add_application_by_interface(STC_TOTAL_WIFI);
-       __add_application_by_interface(STC_TOTAL_BLUETOOTH);
-       __add_application_by_interface(STC_TOTAL_IPV4);
-       __add_application_by_interface(STC_TOTAL_IPV6);
-       __add_application_by_interface(STC_TOTAL_TETHERING);
+       stc_monitor_app_add_by_iface(STC_TOTAL_DATACALL);
+       stc_monitor_app_add_by_iface(STC_TOTAL_WIFI);
+       stc_monitor_app_add_by_iface(STC_TOTAL_BLUETOOTH);
+       stc_monitor_app_add_by_iface(STC_TOTAL_IPV4);
+       stc_monitor_app_add_by_iface(STC_TOTAL_IPV6);
+       /* stc_monitor_app_add_by_iface(STC_TOTAL_TETHERING); */
 
-       /* creating restriction rules tree */
        __update_contr_cb(NULL);
 
        /* registering periodic kernel counters update callback */
@@ -2526,16 +374,16 @@ stc_error_e stc_monitor_init(void)
                                                         __update_contr_cb,
                                                         NULL);
        if (g_system->contr_timer_id == 0) {
-               STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
-               __close_contr_sock(g_system); //LCOV_EXCL_LINE
-               return STC_ERROR_FAIL; //LCOV_EXCL_LINE
+               STC_LOGE("Failed to register kernel counters update timer");
+               __close_contr_sock(g_system);
+               return STC_ERROR_FAIL;
        }
 
        __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
                        (int *)&g_system->background_state);
 
        __fill_exceptions_list();
-       __fill_restritions_list();
+       stc_monitor_rstns_load();
 
        return STC_ERROR_NONE;
 }
@@ -2554,11 +402,11 @@ stc_error_e stc_monitor_deinit(void)
        }
 
        /* destroy monitored application tree */
-       g_tree_destroy(g_system->apps);
+       g_hash_table_destroy(g_system->apps);
        g_system->apps = NULL;
 
        /* destroy restriction rules tree */
-       g_tree_destroy(g_system->rstns);
+       g_hash_table_destroy(g_system->rstns);
        g_system->rstns = NULL;
 
        FREE(g_system);
@@ -2566,335 +414,162 @@ stc_error_e stc_monitor_deinit(void)
        return STC_ERROR_NONE;
 }
 
-API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
-                                       const stc_app_value_s app_value)
+GHashTable *stc_monitor_get_system_apps(void)
 {
-       stc_error_e ret = STC_ERROR_NONE;
-       stc_app_key_s *key;
-       stc_app_value_s *value;
-       stc_app_value_s *lookup;
+       ret_value_msg_if(g_system == NULL, NULL,
+                               "stc monitor not initialized!");
 
-       ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
-
-       lookup = __application_lookup(g_system->apps, &app_key);
-       if (lookup)
-               return STC_ERROR_NONE; //LCOV_EXCL_LINE
-
-       key = MALLOC0(stc_app_key_s, 1);
-       if (!key) {
-               STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
-               return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
-       }
-
-       value = MALLOC0(stc_app_value_s, 1);
-       if (!value) {
-               STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
-               FREE(key); //LCOV_EXCL_LINE
-               return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
-       }
-
-       key->app_id = g_strdup(app_key.app_id);
-       key->pkg_id = g_strdup(app_key.pkg_id);
-
-       value->type = app_value.type;
-       value->data_usage.in_bytes = app_value.data_usage.in_bytes;
-       value->data_usage.out_bytes = app_value.data_usage.out_bytes;
-       g_strlcpy(value->mac, app_value.mac, MAC_ADDRESS_LEN);
+       ret_value_msg_if(g_system->apps == NULL, NULL,
+                               "apps is null!");
 
-       value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
-                                          __processes_tree_key_free,
-                                          __processes_tree_value_free);
-
-       /* create cgroup and update classid */
-       value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
-
-       /* update classid for tethering station based on its mac address */
-       if (g_str_has_suffix(app_key.app_id, STC_TETHERING_APP_SUFFIX) &&
-                               value->classid != STC_TETHERING_APP_CLASSID)
-               stc_plugin_tether_set_station_classid(value->mac, value->classid);
-
-       g_tree_insert(g_system->apps, key, value);
-
-       /* add nfacct rule for this classid */
-       __add_application_monitor(key, value, stc_get_default_connection());
-       __add_rstns_for_application(app_key.app_id);
-
-       return ret;
+       return g_system->apps;
 }
 
-API stc_error_e stc_monitor_application_remove(const stc_app_key_s app_key)
+GHashTable *stc_monitor_get_system_rstns(void)
 {
-       stc_error_e ret = STC_ERROR_NONE;
-       stc_app_value_s *app_lookup;
-
-       ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
-
-       app_lookup = __application_lookup(g_system->apps, &app_key);
-       if (!app_lookup) {
-               if (STC_DEBUG_LOG)
-                       STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
-               return STC_ERROR_FAIL; //LCOV_EXCL_LINE
-       }
+       ret_value_msg_if(g_system == NULL, NULL,
+                               "stc monitor not initialized!");
 
-       /* remove nfacct rule for this classid */
-       __remove_application_monitor((gpointer) &app_key, app_lookup,
-                                    stc_get_default_connection());
+       ret_value_msg_if(g_system->rstns == NULL, NULL,
+                               "rstns is null!");
 
-       /* remove ristrictions if any */
-       __remove_rstns_for_application(app_key.app_id);
+       return g_system->rstns;
+}
 
-       /* remove app_key from the stc-manager */
-       if (!g_tree_remove(g_system->apps, &app_key)) {
-               ret = STC_ERROR_NO_DATA;
-               STC_LOGE("key not found");
-       }
+int stc_monitor_get_contr_sock(void)
+{
+       ret_value_msg_if(g_system == NULL, 0,
+                               "stc monitor not initialized!");
 
-       return ret;
+       return g_system->contr_sock;
 }
 
-API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
-                                   const stc_process_key_s proc_key,
-                                   const stc_process_value_s proc_value)
+time_t stc_monitor_get_last_month_ts(void)
 {
-       stc_error_e ret = STC_ERROR_NONE;
-       stc_app_value_s *app_lookup;
-       stc_process_key_s *key;
-       stc_process_value_s *value;
-       stc_process_value_s *proc_lookup;
+       ret_value_msg_if(g_system == NULL, 0,
+                               "stc monitor not initialized!");
 
-       ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
+       return g_system->last_month_ts;
+}
 
-       app_lookup = __application_lookup(g_system->apps, &app_key);
-       if (!app_lookup) {
-               if (STC_DEBUG_LOG)
-                       STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
-               return STC_ERROR_FAIL; //LCOV_EXCL_LINE
-       }
+void stc_monitor_set_last_month_ts(time_t time)
+{
+       ret_msg_if(g_system == NULL, "stc monitor not initialized!");
 
-       proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
-       if (proc_lookup)
-               return STC_ERROR_NONE; //LCOV_EXCL_LINE
+       g_system->last_month_ts = time;
+}
 
-       key = MALLOC0(stc_process_key_s, 1);
-       if (!key) {
-               STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
-               return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
-       }
+time_t stc_monitor_get_last_week_ts(void)
+{
+       ret_value_msg_if(g_system == NULL, 0,
+                               "stc monitor not initialized!");
 
-       value = MALLOC0(stc_process_value_s, 1);
-       if (!value) {
-               STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
-               FREE(key); //LCOV_EXCL_LINE
-               return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
-       }
+       return g_system->last_week_ts;
+}
 
-       key->pid = proc_key.pid;
+void stc_monitor_set_last_week_ts(time_t time)
+{
+       ret_msg_if(g_system == NULL, "stc monitor not initialized!");
 
-       value->ground = proc_value.ground;
+       g_system->last_week_ts = time;
+}
 
-       g_tree_insert(app_lookup->processes, key, value);
+time_t stc_monitor_get_last_day_ts(void)
+{
+       ret_value_msg_if(g_system == NULL, 0,
+                               "stc monitor not initialized!");
 
-       /* add pid to application cgroup */
-       place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
+       return g_system->last_day_ts;
+}
 
-       /*
-       __apps_tree_printall(); //LCOV_EXCL_LINE
-       */
+void stc_monitor_set_last_day_ts(time_t time)
+{
+       ret_msg_if(g_system == NULL, "stc monitor not initialized!");
 
-       return ret;
+       g_system->last_day_ts = time;
 }
 
-API stc_error_e stc_monitor_process_remove(pid_t pid)
+void stc_monitor_set_rstns_updated(gboolean value)
 {
-       stc_error_e ret = STC_ERROR_NONE;
-       stc_process_key_s proc_key = {
-               .pid = pid
-       };
+       ret_msg_if(g_system == NULL, "stc monitor not initialized!");
 
-       remove_pid_context_s context = {
-               .app_key = NULL,
-               .proc_key = &proc_key,
-               .entry_removed = FALSE,
-       };
-
-       ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
+       g_system->rstns_updated = value;
+}
 
-       g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
-                      &context);
+gboolean stc_monitor_get_rstns_updated(void)
+{
+       ret_value_msg_if(g_system == NULL, FALSE,
+                               "stc monitor not initialized!");
 
-       if (context.entry_removed)
-               __application_remove_if_empty(context.app_key);
+       return g_system->rstns_updated;
+}
 
-       /*
-       __apps_tree_printall(); //LCOV_EXCL_LINE
-       */
+void stc_monitor_set_apps_updated(gboolean value)
+{
+       ret_msg_if(g_system == NULL, "stc monitor not initialized!");
 
-       return ret;
+       g_system->apps_updated = value;
 }
 
-//LCOV_EXCL_START
-API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
-                                             const stc_process_key_s proc_key,
-                                             stc_app_state_e ground)
+gboolean stc_monitor_get_apps_updated(void)
 {
-       stc_error_e ret = STC_ERROR_NONE;
-       stc_app_value_s *app_lookup;
-       stc_process_value_s *proc_lookup;
-
-       ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
+       ret_value_msg_if(g_system == NULL, FALSE,
+                               "stc monitor not initialized!");
 
-       app_lookup = __application_lookup(g_system->apps, &app_key);
-       if (!app_lookup) {
-               STC_LOGD("app_key not found");
-               return STC_ERROR_FAIL;
-       }
+       return g_system->apps_updated;
+}
 
-       proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
-       if (!proc_lookup) {
-               STC_LOGD("proc_key not found");
-               return STC_ERROR_FAIL;
-       }
+void stc_monitor_set_background_state(gboolean value)
+{
+       ret_msg_if(g_system == NULL, "stc monitor not initialized!");
 
-       if (proc_lookup->ground != ground)
-               proc_lookup->ground = ground;
+       g_system->background_state = value;
+}
 
-       place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
+gboolean stc_monitor_get_background_state(void)
+{
+       ret_value_msg_if(g_system == NULL, FALSE,
+                               "stc monitor not initialized!");
 
-       return ret;
+       return g_system->background_state;
 }
-//LCOV_EXCL_STOP
 
-void stc_monitor_update_rstn_by_default_connection(void *data)
+void stc_monitor_update_by_connection(void *data)
 {
-       static default_connection_s old_connection;
-       default_connection_s *new_connection = (default_connection_s *)data;
-
-       if (old_connection.path != NULL) {
-               //LCOV_EXCL_START
-               if (g_system->apps)
-                       g_tree_foreach(g_system->apps,
-                                      __remove_application_monitor,
-                                      (gpointer)&old_connection);
+       stc_connection_s *connection = (stc_connection_s *)data;
 
-               if (g_system->rstns)
-                       g_tree_foreach(g_system->rstns,
-                                      __remove_restriction,
-                                      (gpointer)&old_connection);
+       if (connection != NULL && connection->path != NULL) {
+               stc_monitor_app_remove_by_connection(connection);
+               stc_monitor_rstn_remove_by_connection(connection);
 
                iptables_flush_chains();
-               //LCOV_EXCL_STOP
-       }
 
-       FREE(old_connection.path);
-       FREE(old_connection.ifname);
-       FREE(old_connection.tether_iface.ifname);
-       old_connection.type = 0;
-       old_connection.roaming = 0;
-       old_connection.tether_state = FALSE;
-       old_connection.tether_iface.type = 0;
-
-       if (new_connection != NULL && new_connection->path != NULL) {
-               if (g_system->apps)
-                       g_tree_foreach(g_system->apps,
-                                      __add_application_monitor,
-                                      (gpointer)new_connection);
-
-               if (g_system->rstns)
-                       g_tree_foreach(g_system->rstns, __add_restriction,
-                                      NULL);
-
-               old_connection.path = g_strdup(new_connection->path);
-               old_connection.ifname = g_strdup(new_connection->ifname);
-               old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname);
-               old_connection.type = new_connection->type;
-               old_connection.roaming = new_connection->roaming;
-               old_connection.tether_state = new_connection->tether_state;
-               old_connection.tether_iface.type = new_connection->tether_iface.type;
+               stc_monitor_app_add_by_connection(connection);
+               stc_monitor_rstn_add_by_connection(connection);
        }
 }
 
-stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
+void stc_monitor_add_by_connection(void *data)
 {
-       stc_error_e ret;
-
-       stc_rstn_key_s key;
-       stc_rstn_value_s value;
-
-       memset(&key, 0, sizeof(stc_rstn_key_s));
-       memset(&value, 0, sizeof(stc_rstn_value_s));
-
-       key.app_id = g_strdup(info->app_id);
-       key.ifname = g_strdup(info->ifname);
-       key.mac = g_strdup(info->mac);
-       key.subscriber_id = g_strdup(info->subscriber_id);
-       key.iftype = info->iftype;
-       key.roaming = info->roaming;
-
-       value.rstn_type = info->rstn_type;
-       value.rstn_state = STC_RSTN_STATE_UNKNOWN;
-       value.restriction_id = info->restriction_id;
-
-       if (info->app_id)
-               value.classid = get_classid_by_app_id(info->app_id, TRUE);
-       else
-               value.classid = STC_UNKNOWN_CLASSID;
-
-       if (value.classid == STC_BACKGROUND_APP_CLASSID) {
-               g_system->background_state = TRUE; //LCOV_EXCL_LINE
-               __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, TRUE); //LCOV_EXCL_LINE
-               __process_update_background(); //LCOV_EXCL_LINE
-       }
+       stc_connection_s *connection = (stc_connection_s *)data;
 
-       value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
-       value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
-       value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
-       value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
-       value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
-       value.month_start_date = info->month_start_date;
-       value.month_start_ts = stc_time_get_month_start(time(NULL),
-                                                                               info->month_start_date);
-
-       ret = __rstn_tree_add(&key, &value, TRUE);
-
-       FREE(key.app_id);
-       FREE(key.ifname);
-       FREE(key.mac);
-       FREE(key.subscriber_id);
-       return ret;
+       if (connection != NULL && connection->path != NULL) {
+               stc_monitor_app_add_by_connection(connection);
+               stc_monitor_rstn_add_by_connection(connection);
+       }
 }
 
-stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
+void stc_monitor_remove_by_connection(void *data)
 {
-       stc_error_e ret;
-
-       stc_rstn_key_s key = {
-               .app_id = g_strdup(info->app_id),
-               .ifname = g_strdup(info->ifname),
-               .subscriber_id = g_strdup(info->subscriber_id),
-               .iftype = info->iftype,
-               .roaming = info->roaming,
-       };
+       stc_connection_s *connection = (stc_connection_s *)data;
 
-       if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
-               g_system->background_state = FALSE; //LCOV_EXCL_LINE
-               __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, FALSE); //LCOV_EXCL_LINE
-               __process_update_background(); //LCOV_EXCL_LINE
+       if (connection != NULL && connection->path != NULL) {
+               stc_monitor_app_remove_by_connection(connection);
+               stc_monitor_rstn_remove_by_connection(connection);
        }
-
-       ret = __rstn_tree_remove(&key);
-
-       FREE(key.app_id);
-       FREE(key.ifname);
-       FREE(key.subscriber_id);
-       return ret;
 }
 
 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
 {
        return stc_plugin_check_exception_by_cmdline(cmdline);
 }
-
-int stc_monitor_get_counter_socket(void)
-{
-       return g_system->contr_sock;
-}
old mode 100755 (executable)
new mode 100644 (file)
index ec1f674..2a1799c
@@ -451,6 +451,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
 
                switch (rule->s_ip_type) {
                case STC_FW_IP_RANGE:
+                       /* fall through */
                case STC_FW_IP_MASK:
                        addr = g_try_malloc0(INET_ADDRSTRLEN);
                        if (addr) {
@@ -459,6 +460,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
                                                        g_variant_new_string(addr));
                                FREE(addr);
                        }
+                       /* fall through */
                case STC_FW_IP_SINGLE:
                        addr = g_try_malloc0(INET_ADDRSTRLEN);
                        if (addr) {
@@ -474,6 +476,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
 
                switch (rule->d_ip_type) {
                case STC_FW_IP_RANGE:
+                       /* fall through */
                case STC_FW_IP_MASK:
                        addr = g_try_malloc0(INET_ADDRSTRLEN);
                        if (addr) {
@@ -482,6 +485,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
                                                        g_variant_new_string(addr));
                                FREE(addr);
                        }
+                       /* fall through */
                case STC_FW_IP_SINGLE:
                        addr = g_try_malloc0(INET_ADDRSTRLEN);
                        if (addr) {
@@ -499,6 +503,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
 
                switch (rule->s_ip_type) {
                case STC_FW_IP_RANGE:
+                       /* fall through */
                case STC_FW_IP_MASK:
                        addr = g_try_malloc0(INET6_ADDRSTRLEN);
                        if (addr) {
@@ -507,6 +512,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
                                                        g_variant_new_string(addr));
                                FREE(addr);
                        }
+                       /* fall through */
                case STC_FW_IP_SINGLE:
                        addr = g_try_malloc0(INET6_ADDRSTRLEN);
                        if (addr) {
@@ -522,6 +528,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
 
                switch (rule->d_ip_type) {
                case STC_FW_IP_RANGE:
+                       /* fall through */
                case STC_FW_IP_MASK:
                        addr = g_try_malloc0(INET6_ADDRSTRLEN);
                        if (addr) {
@@ -530,6 +537,7 @@ static void __fw_rule_make_params(gpointer data, gpointer user_data)
                                                        g_variant_new_string(addr));
                                FREE(addr);
                        }
+                       /* fall through */
                case STC_FW_IP_SINGLE:
                        addr = g_try_malloc0(INET6_ADDRSTRLEN);
                        if (addr) {
@@ -1157,6 +1165,11 @@ gboolean __validate_fw_rule(firewall_rule_s *rule)
                return FALSE;
        }
 
+       if (rule->target >= STC_FW_RULE_TARGET_MAX) {
+               __STC_LOG_FUNC_EXIT__;
+               return FALSE;
+       }
+
        if (rule->target == STC_FW_RULE_TARGET_LOG &&
                (rule->log_prefix == NULL ||
                rule->log_prefix[0] == '\0')) {
index 2e5803c..04af66c 100644 (file)
@@ -19,7 +19,7 @@
 #include "stc-statistics.h"
 #include "stc-restriction.h"
 #include "stc-firewall.h"
-#include "stc-default-connection.h"
+#include "stc-connection.h"
 #include "stc-manager-util.h"
 #include "stc-manager-plugin-appstatus.h"
 #include "stc-manager-plugin-procfs.h"
@@ -306,7 +306,7 @@ static void __stc_manager_gdbus_on_bus_acquired(GDBusConnection *connection,
                                                    stc->connection);
 
        iptables_init();
-       stc_default_connection_monitor_init(stc);
+       stc_connection_monitor_init(stc);
 
        stc_plugin_appstatus_register_state_changed_cb(stc,
                        stc_plugin_procfs_app_status_changed, NULL);
@@ -353,7 +353,7 @@ void stc_manager_gdbus_deinit(gpointer stc_data)
        stc_s *stc = (stc_s *)stc_data;
 
        stc_plugin_appstatus_deregister_state_changed_cb(stc);
-       stc_default_connection_monitor_deinit(stc);
+       stc_connection_monitor_deinit(stc);
 
        g_bus_unown_name(stc->gdbus_owner_id);
 
index b929427..51dbd4f 100644 (file)
@@ -150,10 +150,10 @@ void stc_util_initialize_config(void)
        snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
 
        keyfile = __load_key_file(path);
-       if (!keyfile)
+       if (!keyfile) {
                keyfile = g_key_file_new(); //LCOV_EXCL_LINE
-
-       g_key_file_set_integer(keyfile, path, INFO_DEBUGLOG, 0);
+               g_key_file_set_integer(keyfile, path, INFO_DEBUGLOG, 0);
+       }
 
        __save_key_file(keyfile, path);
 }
old mode 100755 (executable)
new mode 100644 (file)
index 7b4876a..79acd88
@@ -118,7 +118,9 @@ static stc_s *__stc_manager_init(void)
        inotify_initialize();
        inotify_register(INFO_STORAGE_DIR, __stc_inotify_handler);
 
+#ifndef TIZEN_TV_EXT
        cgroup_set_release_agent(NET_CLS_SUBSYS, NET_RELEASE_AGENT);
+#endif
 
        err = stc_db_initialize();
        if (err != STC_ERROR_NONE) {
@@ -126,13 +128,16 @@ static stc_s *__stc_manager_init(void)
                return NULL; //LCOV_EXCL_LINE
        }
 
+#ifndef TIZEN_TV_EXT
        stc_plugin_appstatus_init();
        stc_plugin_exception_init();
        stc_plugin_procfs_init();
        stc_plugin_tether_init();
+#endif
 
        stc_firewall_init();
 
+#ifndef TIZEN_TV_EXT
        err = stc_monitor_init();
        if (err != STC_ERROR_NONE) {
                STC_LOGD("Failed to initialize stc manager"); //LCOV_EXCL_LINE
@@ -140,6 +145,8 @@ static stc_s *__stc_manager_init(void)
        }
 
        stc_plugin_procfs_load_pid();
+#endif
+
        stc_manager_gdbus_init((gpointer)stc);
 
        STC_LOGI("stc manager initialized");
@@ -178,6 +185,13 @@ int stc_commit_iptables(char *cmd, int *err_num, char **err_str)
 
        if (pid == 0) {
                errno = 0;
+
+               if (!g_strcmp0(args[1], STC_CMD_INSERT)) {
+                       STC_LOGE("Invalid arguments");
+                       g_strfreev(args);
+                       exit(-1);
+               }
+
                if (execv(args[0], args) == -1) {
                        STC_LOGE("Failed to execute [%s]", *err_str);
                        g_strfreev(args);
index f4cd753..6b207a5 100644 (file)
@@ -43,16 +43,23 @@ static const gchar *stc_err_strs[] = {
 void __initialize_rstn_rule(table_restrictions_info *rule)
 {
        rule->app_id = NULL;
-       rule->ifname = NULL;
+
        rule->iftype = STC_IFACE_ALL;
+       rule->ifname = NULL;
+
+       rule->subscriber_id = NULL;
+       rule->roaming = STC_ROAMING_DISABLE;
+
        rule->rstn_type = STC_RSTN_TYPE_UNKNOWN;
        rule->data_limit = -1;
        rule->data_warn_limit = -1;
+
+       rule->month_start_date = 1;
        rule->monthly_limit = -1;
        rule->weekly_limit = -1;
        rule->daily_limit = -1;
-       rule->roaming = STC_ROAMING_DISABLE;
-       rule->subscriber_id = NULL;
+
+       rule->mac = NULL;
 }
 
 gboolean __validate_rstn_rule(table_restrictions_info *rule)
@@ -70,7 +77,7 @@ gboolean __validate_rstn_rule(table_restrictions_info *rule)
                return FALSE; //LCOV_EXCL_LINE
        }
 
-       if (rule->roaming >= STC_ROAMING_LAST_ELEM) {
+       if (rule->roaming > STC_ROAMING_ENABLE) {
                __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
                return FALSE; //LCOV_EXCL_LINE
        }
@@ -260,6 +267,7 @@ gboolean handle_restriction_set(StcRestriction *object,
        __STC_LOG_FUNC_ENTER__;
        GVariantIter *iter = NULL;
        table_restrictions_info rule;
+       stc_error_e ret = STC_ERROR_NONE;
 
        memset(&rule, 0, sizeof(table_restrictions_info));
        __initialize_rstn_rule(&rule);
@@ -279,10 +287,15 @@ gboolean handle_restriction_set(StcRestriction *object,
                return TRUE;
        }
 
-       table_restrictions_update(&rule);
-       /* update restriction rule in runtime structure */
-       stc_monitor_rstns_tree_add(&rule);
+       ret = table_restrictions_update(&rule);
+       if (ret != STC_ERROR_NONE) {
+               STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
+                                                STC_ERROR_DB_FAILED);
+               __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
+               return TRUE;
+       }
 
+       stc_monitor_rstn_add(&rule);
        STC_DBUS_REPLY_ERROR_NONE(invocation);
        __STC_LOG_FUNC_EXIT__;
        return TRUE;
@@ -296,6 +309,7 @@ gboolean handle_restriction_unset(StcRestriction *object,
        __STC_LOG_FUNC_ENTER__;
        GVariantIter *iter = NULL;
        table_restrictions_info rule;
+       stc_error_e ret = STC_ERROR_NONE;
 
        memset(&rule, 0, sizeof(table_restrictions_info));
        __initialize_rstn_rule(&rule);
@@ -315,10 +329,16 @@ gboolean handle_restriction_unset(StcRestriction *object,
                return TRUE;
        }
 
-       table_restrictions_delete(rule.app_id, rule.iftype, rule.subscriber_id);
-       /* remove restriction rule from runtime structure */
-       stc_monitor_rstns_tree_remove(&rule);
+       ret = table_restrictions_delete(rule.app_id, rule.iftype, rule.ifname,
+                                       rule.subscriber_id, rule.roaming);
+       if (ret != STC_ERROR_NONE) {
+               STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
+                                                STC_ERROR_DB_FAILED);
+               __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
+               return TRUE;
+       }
 
+       stc_monitor_rstn_remove(&rule);
        STC_DBUS_REPLY_ERROR_NONE(invocation);
        __STC_LOG_FUNC_EXIT__;
        return TRUE;
index 7cb1f4f..6ae9e48 100644 (file)
@@ -10,7 +10,7 @@ SET(REQUIRES_LIST ${REQUIRES_LIST}
        gmock
        dlog
 )
-
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
 INCLUDE(FindPkgConfig)
 PKG_CHECK_MODULES(GTEST_TEST_PKG REQUIRED ${REQUIRES_LIST})
 
diff --git a/unittest/common.cpp b/unittest/common.cpp
new file mode 100644 (file)
index 0000000..3fbaf8d
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2018 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.
+ */
+
+#include "common.h"
+
+GMainLoop *MainLoop::m_mainLoop = NULL;
+guint MainLoop::m_timerId = 0;
+
+MainLoop::MainLoop(void)
+{
+}
+
+MainLoop::~MainLoop(void)
+{
+       if (m_mainLoop)
+               g_main_loop_quit(m_mainLoop);
+       if (m_timerId)
+               g_source_remove(m_timerId);
+
+       m_mainLoop = NULL;
+       m_timerId = 0;
+}
+
+gboolean MainLoop::timeoutCb(gpointer data)
+{
+       if (m_mainLoop)
+               g_main_loop_quit(m_mainLoop);
+       if (m_timerId)
+               g_source_remove(m_timerId);
+
+       m_mainLoop = NULL;
+       m_timerId = 0;
+
+       return false;
+}
+
+void MainLoop::run(unsigned int timeout)
+{
+       if (m_timerId > 0)
+               return;
+
+       m_mainLoop = g_main_loop_new(NULL, false);
+       m_timerId = g_timeout_add(timeout,
+                       (GSourceFunc) &MainLoop::timeoutCb,
+                       NULL);
+       g_main_loop_run(m_mainLoop);
+}
+
+void MainLoop::quit(void)
+{
+       timeoutCb(NULL);
+}
+
diff --git a/unittest/common.h b/unittest/common.h
new file mode 100644 (file)
index 0000000..c0f97aa
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2018 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.
+ */
+
+#include <glib.h>
+class MainLoop {
+private:
+               static GMainLoop *m_mainLoop;
+               static guint m_timerId;
+
+               static gboolean timeoutCb(gpointer data);
+public:
+               MainLoop(void);
+               ~MainLoop(void);
+               void run(unsigned int timeout);
+               void quit(void);
+};
diff --git a/unittest/firewall.cpp b/unittest/firewall.cpp
new file mode 100644 (file)
index 0000000..44bcfe5
--- /dev/null
@@ -0,0 +1,539 @@
+/*
+ * 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.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <iostream>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "firewall.h"
+#include "common.h"
+
+static void AsyncReadyCallback(GObject *source_object,
+       GAsyncResult *res, gpointer user_data)
+{
+       MainLoop *M = (MainLoop *)user_data;
+       GLOGD("Succeeded to response async callback");
+       M->quit();
+}
+
+FRule::FRule()
+{
+       this->chain[0] = '\0';
+       this->direction = 0;
+       this->siptype = 0;
+       this->diptype = 0;
+       this->sporttype = 0;
+       this->dporttype = 0;
+       this->protocol = 0;
+       this->family = 0;
+       this->sip1[0] = '\0';
+       this->dip1[0] = '\0';
+       this->sip2[0] = '\0';
+       this->dip2[0] = '\0';
+       this->dport1 = 0;
+       this->dport2 = 0;
+       this->sport1 = 0;
+       this->sport2 = 0;
+       this->iface[0] = '\0';
+       this->target = 0;
+}
+
+FRule::~FRule()
+{
+}
+
+Firewall::Firewall()
+{
+       Create();
+}
+
+Firewall::~Firewall()
+{
+       Destroy();
+}
+
+error_e Firewall::SetRule(const char *chain, guint16 direction,
+               guint16 siptype, guint16 diptype, guint16 sporttype,
+               guint16 dporttype, guint16 protocol, guint16 family,
+               const char *sip1, const char *dip1, const char *sip2,
+               const char *dip2, guint32 dport1, guint32 dport2,
+               guint32 sport1, guint32 sport2, const char *iface,
+               guint16 target)
+{
+       if (chain == NULL || strlen(chain) == 0)
+               this->m_Rule.chain[0] = '\0';
+       else
+               g_strlcpy(this->m_Rule.chain, chain, CHAIN_LEN);
+
+       if (sip1 == NULL || strlen(sip1) == 0)
+               this->m_Rule.sip1[0] = '\0';
+       else
+               g_strlcpy(this->m_Rule.sip1, sip1, IP_LEN);
+
+       if (sip2 == NULL || strlen(sip2) == 0)
+               this->m_Rule.sip2[0] = '\0';
+       else
+               g_strlcpy(this->m_Rule.sip2, sip2, IP_LEN);
+
+       if (dip1 == NULL || strlen(dip1) == 0)
+               this->m_Rule.dip1[0] = '\0';
+       else
+               g_strlcpy(this->m_Rule.dip1, dip1, IP_LEN);
+
+       if (dip2 == NULL || strlen(dip2) == 0)
+               this->m_Rule.dip2[0] = '\0';
+       else
+               g_strlcpy(this->m_Rule.dip2, dip2, IP_LEN);
+
+       if (iface == NULL || strlen(iface) == 0)
+               this->m_Rule.iface[0] = '\0';
+       else
+               g_strlcpy(this->m_Rule.iface, iface, IFACE_LEN);
+
+
+       this->m_Rule.direction = direction;
+       this->m_Rule.siptype = siptype;
+       this->m_Rule.diptype = diptype;
+       this->m_Rule.sporttype = sporttype;
+       this->m_Rule.dporttype = dporttype;
+       this->m_Rule.protocol = protocol;
+       this->m_Rule.family = family;
+       this->m_Rule.dport1 = dport1;
+       this->m_Rule.dport2 = dport2;
+       this->m_Rule.sport1 = sport1;
+       this->m_Rule.sport2 = sport2;
+       this->m_Rule.target = target;
+
+       return ERROR_NONE;
+}
+
+void Firewall::MakeRuleParams(GVariant **params)
+{
+       GVariantBuilder *builder;
+
+       builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_CHAIN,
+                       g_variant_new_string(this->m_Rule.chain));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_DIRECTION,
+                       g_variant_new_uint16(this->m_Rule.direction));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_IFNAME,
+                       g_variant_new_string(this->m_Rule.iface));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_PROTOCOL,
+                       g_variant_new_uint16(this->m_Rule.protocol));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_TARGET,
+                       g_variant_new_uint16(this->m_Rule.target));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_FAMILY,
+                       g_variant_new_uint16(this->m_Rule.family));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_SIPTYPE,
+                       g_variant_new_uint16(this->m_Rule.siptype));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_SIP1,
+                       g_variant_new_string(this->m_Rule.sip1));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_SIP2,
+                       g_variant_new_string(this->m_Rule.sip2));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_DIPTYPE,
+                       g_variant_new_uint16(this->m_Rule.diptype));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_DIP1,
+                       g_variant_new_string(this->m_Rule.dip1));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_DIP2,
+                       g_variant_new_string(this->m_Rule.dip2));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_SPORTTYPE,
+                       g_variant_new_uint16(this->m_Rule.sporttype));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_SPORT1,
+                       g_variant_new_uint32(this->m_Rule.sport1));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_SPORT2,
+                       g_variant_new_uint32(this->m_Rule.sport2));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_DPORTTYPE,
+                       g_variant_new_uint16(this->m_Rule.dporttype));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_DPORT1,
+                       g_variant_new_uint32(this->m_Rule.dport1));
+
+       g_variant_builder_add(builder, "{sv}",
+                       FIREWALL_RULE_DPORT2,
+                       g_variant_new_uint32(this->m_Rule.dport2));
+
+       *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
+       g_variant_builder_unref(builder);
+}
+
+error_e Firewall::GetAllRule(void)
+{
+       MainLoop M;
+       error_e error = ERROR_NONE;
+
+       error = InvokeMethodNonblock(STC_MGR_SERVICE,
+               STC_MGR_FIREWALL_PATH,
+               STC_MGR_FIREWALL_INTERFACE,
+               STC_MGR_METHOD_FIREWALL_GET_ALL,
+               NULL,
+               DBUS_REPLY_TIMEOUT,
+               AsyncReadyCallback,
+               &M);
+
+       if (error != ERROR_NONE) {
+               GLOGD("Failed to invoke dbus method nonblock");
+               return error;
+       }
+
+       GLOGD("Succeeded to get all restriction");
+
+       M.run(GMAINTIMEOUT);
+       return ERROR_NONE;
+}
+
+error_e Firewall::UpdateRule(void)
+{
+       GVariant *message = NULL;
+       GVariant *params = NULL;
+       error_e error = ERROR_NONE;
+       int result = 0;
+
+       MakeRuleParams(&params);
+
+       message = InvokeMethod(STC_MGR_SERVICE,
+               STC_MGR_FIREWALL_PATH,
+               STC_MGR_FIREWALL_INTERFACE,
+               STC_MGR_METHOD_FIREWALL_UPDATE,
+               params,
+               &error);
+
+       if (message == NULL) {
+               GLOGD("Failed to invoke dbus method");
+               return error;
+       }
+
+       g_variant_get(message, "(i)", &result);
+       GLOGD("Succeeded to set restriction [%d]", result);
+       g_variant_unref(message);
+
+       return ERROR_NONE;
+}
+
+error_e Firewall::RemoveRule(void)
+{
+       GVariant *message = NULL;
+       GVariant *params = NULL;
+       error_e error = ERROR_NONE;
+       int result = 0;
+
+       MakeRuleParams(&params);
+
+       message = InvokeMethod(STC_MGR_SERVICE,
+               STC_MGR_FIREWALL_PATH,
+               STC_MGR_FIREWALL_INTERFACE,
+               STC_MGR_METHOD_FIREWALL_REMOVE,
+               params,
+               &error);
+
+       if (message == NULL) {
+               GLOGD("Failed to invoke dbus method");
+               return error;
+       }
+
+       g_variant_get(message, "(i)", &result);
+       GLOGD("Succeeded to set restriction [%d]", result);
+       g_variant_unref(message);
+
+       return ERROR_NONE;
+}
+
+error_e Firewall::AddRule(void)
+{
+       GVariant *message = NULL;
+       GVariant *params = NULL;
+       error_e error = ERROR_NONE;
+       int result = 0;
+
+       MakeRuleParams(&params);
+
+       message = InvokeMethod(STC_MGR_SERVICE,
+               STC_MGR_FIREWALL_PATH,
+               STC_MGR_FIREWALL_INTERFACE,
+               STC_MGR_METHOD_FIREWALL_ADD,
+               params,
+               &error);
+
+       if (message == NULL) {
+               GLOGD("Failed to invoke dbus method");
+               return error;
+       }
+
+       g_variant_get(message, "(i)", &result);
+       GLOGD("Succeeded to set restriction [%d]", result);
+       g_variant_unref(message);
+
+       return ERROR_NONE;
+}
+
+error_e Firewall::SetChain(const char *chain, unsigned int target)
+{
+       GVariant *message = NULL;
+       error_e error = ERROR_NONE;
+       int result = 0;
+
+       message = InvokeMethod(STC_MGR_SERVICE,
+               STC_MGR_FIREWALL_PATH,
+               STC_MGR_FIREWALL_INTERFACE,
+               STC_MGR_METHOD_FIREWALL_SET,
+               g_variant_new("(su)", chain, target),
+               &error);
+
+       if (message == NULL) {
+               GLOGD("Failed to invoke dbus method");
+               return error;
+       }
+
+       g_variant_get(message, "(i)", &result);
+       GLOGD("Succeeded to set restriction [%d]", result);
+       g_variant_unref(message);
+
+       return ERROR_NONE;
+}
+
+error_e Firewall::UnsetChain(const char *chain)
+{
+       GVariant *message = NULL;
+       error_e error = ERROR_NONE;
+       int result = 0;
+
+       message = InvokeMethod(STC_MGR_SERVICE,
+               STC_MGR_FIREWALL_PATH,
+               STC_MGR_FIREWALL_INTERFACE,
+               STC_MGR_METHOD_FIREWALL_UNSET,
+               g_variant_new("(s)", chain),
+               &error);
+
+       if (message == NULL) {
+               GLOGD("Failed to invoke dbus method");
+               return error;
+       }
+
+       g_variant_get(message, "(i)", &result);
+       GLOGD("Succeeded to set restriction [%d]", result);
+       g_variant_unref(message);
+
+       return ERROR_NONE;
+}
+
+error_e Firewall::FlushChain(const char *chain)
+{
+       GVariant *message = NULL;
+       error_e error = ERROR_NONE;
+       int result = 0;
+
+       message = InvokeMethod(STC_MGR_SERVICE,
+               STC_MGR_FIREWALL_PATH,
+               STC_MGR_FIREWALL_INTERFACE,
+               STC_MGR_METHOD_FIREWALL_FLUSH,
+               g_variant_new("(s)", chain),
+               &error);
+
+       if (message == NULL) {
+               GLOGD("Failed to invoke dbus method");
+               return error;
+       }
+
+       g_variant_get(message, "(i)", &result);
+       GLOGD("Succeeded to set restriction [%d]", result);
+       g_variant_unref(message);
+
+       return ERROR_NONE;
+}
+
+error_e Firewall::GetAllChain(void)
+{
+       MainLoop M;
+       error_e error = ERROR_NONE;
+
+       error = InvokeMethodNonblock(STC_MGR_SERVICE,
+               STC_MGR_FIREWALL_PATH,
+               STC_MGR_FIREWALL_INTERFACE,
+               STC_MGR_METHOD_FIREWALL_GET_CHAIN,
+               NULL,
+               DBUS_REPLY_TIMEOUT,
+               AsyncReadyCallback,
+               &M);
+
+       if (error != ERROR_NONE) {
+               GLOGD("Failed to invoke dbus method nonblock");
+               return error;
+       }
+
+       GLOGD("Succeeded to get all restriction");
+
+       M.run(GMAINTIMEOUT);
+       return ERROR_NONE;
+}
+
+error_e Firewall::RemoveChain(const char *chain)
+{
+       GVariant *message = NULL;
+       error_e error = ERROR_NONE;
+       int result = 0;
+
+       message = InvokeMethod(STC_MGR_SERVICE,
+               STC_MGR_FIREWALL_PATH,
+               STC_MGR_FIREWALL_INTERFACE,
+               STC_MGR_METHOD_FIREWALL_REMOVE_CHAIN,
+               g_variant_new("(s)", chain),
+               &error);
+
+       if (message == NULL) {
+               GLOGD("Failed to invoke dbus method");
+               return error;
+       }
+
+       g_variant_get(message, "(i)", &result);
+       GLOGD("Succeeded to set restriction [%d]", result);
+       g_variant_unref(message);
+
+       return ERROR_NONE;
+}
+
+error_e Firewall::AddChain(const char *chain)
+{
+       GVariant *message = NULL;
+       error_e error = ERROR_NONE;
+       int result = 0;
+
+       message = InvokeMethod(STC_MGR_SERVICE,
+               STC_MGR_FIREWALL_PATH,
+               STC_MGR_FIREWALL_INTERFACE,
+               STC_MGR_METHOD_FIREWALL_ADD_CHAIN,
+               g_variant_new("(s)", chain),
+               &error);
+
+       if (message == NULL) {
+               GLOGD("Failed to invoke dbus method");
+               return error;
+       }
+
+       g_variant_get(message, "(i)", &result);
+       GLOGD("Succeeded to set restriction [%d]", result);
+       g_variant_unref(message);
+
+       return ERROR_NONE;
+}
+
+error_e Firewall::GetLock(int *state)
+{
+       GVariant *message = NULL;
+       error_e error = ERROR_NONE;
+
+       message = InvokeMethod(STC_MGR_SERVICE,
+               STC_MGR_FIREWALL_PATH,
+               STC_MGR_FIREWALL_INTERFACE,
+               STC_MGR_METHOD_FIREWALL_GET_LOCK,
+               NULL,
+               &error);
+
+       if (message == NULL) {
+               GLOGD("Failed to invoke dbus method");
+               return error;
+       }
+
+       g_variant_get(message, "(i)", &state);
+       GLOGD("Succeeded to get lock state[%d]", state);
+       g_variant_unref(message);
+
+       return ERROR_NONE;
+}
+
+error_e Firewall::Unlock()
+{
+       GVariant *message = NULL;
+       error_e error = ERROR_NONE;
+       int result = 0;
+
+       message = InvokeMethod(STC_MGR_SERVICE,
+               STC_MGR_FIREWALL_PATH,
+               STC_MGR_FIREWALL_INTERFACE,
+               STC_MGR_METHOD_FIREWALL_UNLOCK,
+               NULL,
+               &error);
+
+       if (message == NULL) {
+               GLOGD("Failed to invoke dbus method");
+               return error;
+       }
+
+       g_variant_get(message, "(i)", &result);
+       GLOGD("Succeeded to unlock result[%d]", result);
+       g_variant_unref(message);
+
+       return ERROR_NONE;
+}
+
+error_e Firewall::Lock()
+{
+       GVariant *message = NULL;
+       error_e error = ERROR_NONE;
+       int result = 0;
+
+       message = InvokeMethod(STC_MGR_SERVICE,
+               STC_MGR_FIREWALL_PATH,
+               STC_MGR_FIREWALL_INTERFACE,
+               STC_MGR_METHOD_FIREWALL_LOCK,
+               NULL,
+               &error);
+
+       if (message == NULL) {
+               GLOGD("Failed to invoke dbus method");
+               return error;
+       }
+
+       g_variant_get(message, "(i)", &result);
+       GLOGD("Succeeded to unlock result[%d]", result);
+       g_variant_unref(message);
+
+       return ERROR_NONE;
+}
+
diff --git a/unittest/firewall.h b/unittest/firewall.h
new file mode 100644 (file)
index 0000000..f52e10b
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * 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.
+ */
+#ifndef __STC_MGR_FIREWALL_H__
+#define __STC_MGR_FIREWALL_H__
+
+#include <glib.h>
+#include <gio/gio.h>
+
+#include "stcmgr.h"
+#include "gdbus.h"
+
+#define FIREWALL_RULE_CHAIN      "chain"
+#define FIREWALL_RULE_DIRECTION  "direction"
+#define FIREWALL_RULE_IFNAME     "ifname"
+#define FIREWALL_RULE_PROTOCOL   "protocol"
+#define FIREWALL_RULE_TARGET     "target"
+#define FIREWALL_RULE_TARGETTYPE "target_type"
+
+#define FIREWALL_RULE_FAMILY     "family"
+#define FIREWALL_RULE_SIPTYPE    "s_ip_type"
+#define FIREWALL_RULE_SIP1       "s_ip1"
+#define FIREWALL_RULE_SIP2       "s_ip2"
+#define FIREWALL_RULE_DIPTYPE    "d_ip_type"
+#define FIREWALL_RULE_DIP1       "d_ip1"
+#define FIREWALL_RULE_DIP2       "d_ip2"
+#define FIREWALL_RULE_SPORTTYPE  "s_port_type"
+#define FIREWALL_RULE_SPORT1     "s_port1"
+#define FIREWALL_RULE_SPORT2     "s_port2"
+#define FIREWALL_RULE_DPORTTYPE  "d_port_type"
+#define FIREWALL_RULE_DPORT1     "d_port1"
+#define FIREWALL_RULE_DPORT2     "d_port2"
+
+#define FIREWALL_RULE_LOG_LEVEL       "log_level"
+#define FIREWALL_RULE_LOG_PREFIX      "log_prefix"
+#define FIREWALL_RULE_NFLOG_GROUP     "nflog_group"
+#define FIREWALL_RULE_NFLOG_PREFIX    "nflog_prefix"
+#define FIREWALL_RULE_NFLOG_RANGE     "nflog_range"
+#define FIREWALL_RULE_NFLOG_THRESHOLD "nflog_threshold"
+
+#define CHAIN_LEN 256
+#define IFACE_LEN 256
+#define IP_LEN 16
+
+class FRule {
+private:
+public:
+       char chain[CHAIN_LEN];
+       guint16 direction;
+       guint16 siptype;
+       guint16 diptype;
+       guint16 sporttype;
+       guint16 dporttype;
+       guint16 protocol;
+       guint16 family;
+       char sip1[IP_LEN];
+       char dip1[IP_LEN];
+       char sip2[IP_LEN];
+       char dip2[IP_LEN];
+       guint32 dport1;
+       guint32 dport2;
+       guint32 sport1;
+       guint32 sport2;
+       char iface[IFACE_LEN];
+       guint16 target;
+
+       FRule();
+       ~FRule();
+};
+
+class Firewall : public GDbus {
+private:
+       FRule m_Rule;
+public:
+       Firewall();
+       ~Firewall();
+       error_e SetRule(const char *app_id, const char *iface_name,
+               const char *subscriber_id, iface_type_e iface_type,
+               int64_t data_limit, int64_t data_warn_limit,
+               roaming_type_e roaming_type, const char *mac);
+       void MakeRuleParams(GVariant **params);
+       void MakeGetParams(GVariant **params);
+       void MakeStatusParams(GVariant **params);
+       error_e Lock(void);
+       error_e Unlock(void);
+       error_e GetLock(int *state);
+       error_e AddChain(const char * chain);
+       error_e RemoveChain(const char * chain);
+       error_e FlushChain(const char * chain);
+       error_e GetAllChain();
+       error_e SetChain(const char *chain, unsigned int target);
+       error_e UnsetChain(const char *chain);
+       error_e SetRule(const char *chain, guint16 direction,
+                       guint16 siptype, guint16 diptype, guint16 sporttype,
+                       guint16 dporttype, guint16 protocol, guint16 family,
+                       const char *sip1, const char *dip1, const char *sip2,
+                       const char *dip2, guint32 dport1, guint32 dport2,
+                       guint32 sport1, guint32 sport2, const char *iface,
+                       guint16 target);
+       error_e AddRule();
+       error_e RemoveRule();
+       error_e UpdateRule();
+       error_e GetAllRule();
+};
+
+#endif /* __STC_MGR_FIREWALL_H__ */
index 91596a9..f545629 100644 (file)
 #define STC_MGR_STATISTICS_INTERFACE    STC_MGR_SERVICE ".statistics"
 #define STC_MGR_RESTRICTION_INTERFACE   STC_MGR_SERVICE ".restriction"
 #define STC_MGR_MANAGER_INTERFACE       STC_MGR_SERVICE ".manager"
+#define STC_MGR_FIREWALL_INTERFACE      STC_MGR_SERVICE ".firewall"
 
 #define STC_MGR_STATISTICS_PATH         "/net/stc/statistics"
 #define STC_MGR_RESTRICTION_PATH        "/net/stc/restriction"
 #define STC_MGR_MANAGER_PATH            "/net/stc/manager"
+#define STC_MGR_FIREWALL_PATH           "/net/stc/firewall"
 
 #define STC_MGR_METHOD_STATISTICS_INIT              "Init"
 #define STC_MGR_METHOD_STATISTICS_GET_PER_APP_ID    "Get"
 
 #define STC_MGR_METHOD_MANAGER_STOP                 "Stop"
 
+#define STC_MGR_METHOD_FIREWALL_GET_ALL             "GetAllRule"
+#define STC_MGR_METHOD_FIREWALL_UPDATE              "UpdateRule"
+#define STC_MGR_METHOD_FIREWALL_REMOVE              "RemoveRule"
+#define STC_MGR_METHOD_FIREWALL_ADD                 "AddRule"
+#define STC_MGR_METHOD_FIREWALL_SET                 "SetChain"
+#define STC_MGR_METHOD_FIREWALL_UNSET               "UnsetChain"
+#define STC_MGR_METHOD_FIREWALL_FLUSH               "FlushChain"
+#define STC_MGR_METHOD_FIREWALL_GET_CHAIN           "GetAllChain"
+#define STC_MGR_METHOD_FIREWALL_REMOVE_CHAIN        "RemoveChain"
+#define STC_MGR_METHOD_FIREWALL_ADD_CHAIN           "AddChain"
+#define STC_MGR_METHOD_FIREWALL_GET_LOCK            "GetLock"
+#define STC_MGR_METHOD_FIREWALL_LOCK                "Lock"
+#define STC_MGR_METHOD_FIREWALL_UNLOCK              "Unlock"
+
 class GDbus {
 private:
        GDBusConnection *m_pConnection;
index 0c09ddc..9a3bb4b 100644 (file)
 #include <gtest/gtest.h>
 
 #include "restriction.h"
-
-static GMainLoop *g_pMainLoop = NULL;
-static guint g_nTimeoutId = 0;
-
-static gboolean CallbackTimeut(gpointer data)
-{
-       if (g_pMainLoop)
-               g_main_loop_quit(g_pMainLoop);
-
-       return false;
-}
-
-static void RunMainLoop(void)
-{
-       g_pMainLoop = g_main_loop_new(NULL, false);
-       g_nTimeoutId = g_timeout_add(GMAINTIMEOUT, CallbackTimeut, g_pMainLoop);
-       g_main_loop_run(g_pMainLoop);
-
-       g_source_remove(g_nTimeoutId);
-       g_pMainLoop = NULL;
-}
-
-static void QuitMainLoop(void)
-{
-       if (g_pMainLoop)
-               g_main_loop_quit(g_pMainLoop);
-}
+#include "common.h"
 
 static void AsyncReadyCallback(GObject *source_object,
        GAsyncResult *res, gpointer user_data)
 {
+       MainLoop *M = (MainLoop *)user_data;
        GLOGD("Succeeded to response async callback");
-       QuitMainLoop();
+       M->quit();
 }
 
 RRule::RRule()
@@ -84,7 +59,7 @@ Restriction::~Restriction()
 error_e Restriction::SetRule(const char *app_id, const char *iface_name,
                const char *subscriber_id, iface_type_e iface_type,
                int64_t data_limit, int64_t data_warn_limit,
-               roaming_type_e roaming_type)
+               roaming_type_e roaming_type, const char *mac)
 {
        if (app_id == NULL || strlen(app_id) == 0)
                this->m_Rule.m_AppID[0] = '\0';
@@ -101,6 +76,11 @@ error_e Restriction::SetRule(const char *app_id, const char *iface_name,
        else
                g_strlcpy(this->m_Rule.m_SubscriberID, subscriber_id, SUBSCRIBER_ID_LEN);
 
+       if (mac == NULL || strlen(mac) == 0)
+               this->m_Rule.m_Mac[0] = '\0';
+       else
+               g_strlcpy(this->m_Rule.m_Mac, mac, MAC_LEN);
+
        switch(iface_type) {
        case IFACE_UNKNOWN:
        case IFACE_DATACALL:
@@ -164,6 +144,11 @@ void Restriction::MakeRuleParams(GVariant **params)
                RESTRICTION_RULE_DATA_WARN_LIMIT,
                g_variant_new_int64(this->m_Rule.m_DataWarnLimit));
 
+       g_variant_builder_add(builder, "{sv}",
+               RESTRICTION_RULE_MAC,
+               g_variant_new_string(this->m_Rule.m_Mac));
+
+
        *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
        g_variant_builder_unref(builder);
 }
@@ -293,6 +278,7 @@ error_e Restriction::GetRstrictionStatus(restriction_status_e *status)
 
 error_e Restriction::GetRstriction(void)
 {
+       MainLoop M;
        GVariant *params = NULL;
        error_e error = ERROR_NONE;
 
@@ -305,7 +291,7 @@ error_e Restriction::GetRstriction(void)
                params,
                DBUS_REPLY_TIMEOUT,
                AsyncReadyCallback,
-               NULL);
+               &M);
 
        if (error != ERROR_NONE) {
                GLOGD("Failed to invoke dbus method nonblock");
@@ -314,12 +300,13 @@ error_e Restriction::GetRstriction(void)
 
        GLOGD("Succeeded to get restriction per app ID");
 
-       RunMainLoop();
+       M.run(GMAINTIMEOUT);
        return ERROR_NONE;
 }
 
 error_e Restriction::GetAllRstriction(void)
 {
+       MainLoop M;
        error_e error = ERROR_NONE;
 
        error = InvokeMethodNonblock(STC_MGR_SERVICE,
@@ -329,7 +316,7 @@ error_e Restriction::GetAllRstriction(void)
                NULL,
                DBUS_REPLY_TIMEOUT,
                AsyncReadyCallback,
-               NULL);
+               &M);
 
        if (error != ERROR_NONE) {
                GLOGD("Failed to invoke dbus method nonblock");
@@ -338,6 +325,6 @@ error_e Restriction::GetAllRstriction(void)
 
        GLOGD("Succeeded to get all restriction");
 
-       RunMainLoop();
+       M.run(GMAINTIMEOUT);
        return ERROR_NONE;
-}
\ No newline at end of file
+}
index c0b0f2d..06cbf2c 100644 (file)
@@ -29,6 +29,7 @@
 #define RESTRICTION_RULE_SUBSCRIBER_ID      "subscriber_id"
 #define RESTRICTION_RULE_DATA_LIMIT         "data_limit"
 #define RESTRICTION_RULE_DATA_WARN_LIMIT    "data_warn_limit"
+#define RESTRICTION_RULE_MAC                "mac"
 
 typedef enum {
        RESTRICTION_UNKNOWN,
@@ -45,6 +46,7 @@ public:
        char m_AppID[APP_ID_LEN];
        char m_IfaceName[IFNAME_LEN];
        char m_SubscriberID[SUBSCRIBER_ID_LEN];
+       char m_Mac[MAC_LEN];
        iface_type_e m_IfaceType;
        int64_t m_DataLimit;
        int64_t m_DataWarnLimit;
@@ -63,7 +65,7 @@ public:
        error_e SetRule(const char *app_id, const char *iface_name,
                const char *subscriber_id, iface_type_e iface_type,
                int64_t data_limit, int64_t data_warn_limit,
-               roaming_type_e roaming_type);
+               roaming_type_e roaming_type, const char *mac);
        void MakeRuleParams(GVariant **params);
        void MakeGetParams(GVariant **params);
        void MakeStatusParams(GVariant **params);
index 49ebd6a..2b4b5e5 100644 (file)
 #include <gtest/gtest.h>
 
 #include "statistics.h"
-
-static GMainLoop *g_pMainLoop = NULL;
-static guint g_nTimeoutId = 0;
-
-static gboolean CallbackTimeut(gpointer data)
-{
-       if (g_pMainLoop)
-               g_main_loop_quit(g_pMainLoop);
-
-       return false;
-}
-
-static void RunMainLoop(void)
-{
-       g_pMainLoop = g_main_loop_new(NULL, false);
-       g_nTimeoutId = g_timeout_add(GMAINTIMEOUT, CallbackTimeut, g_pMainLoop);
-       g_main_loop_run(g_pMainLoop);
-
-       g_source_remove(g_nTimeoutId);
-       g_pMainLoop = NULL;
-}
-
-static void QuitMainLoop(void)
-{
-       if (g_pMainLoop)
-               g_main_loop_quit(g_pMainLoop);
-}
+#include "common.h"
 
 static void AsyncReadyCallback(GObject *source_object,
        GAsyncResult *res, gpointer user_data)
 {
+       MainLoop *M = (MainLoop *)user_data;
        GLOGD("Succeeded to response async callback");
-       QuitMainLoop();
+       M->quit();
 }
 
 SRule::SRule()
@@ -209,6 +184,7 @@ error_e Statistics::InitStatistics(void)
 
 error_e Statistics::GetStatistics(void)
 {
+       MainLoop M;
        GVariant *params = NULL;
        error_e error = ERROR_NONE;
 
@@ -221,14 +197,14 @@ error_e Statistics::GetStatistics(void)
                params,
                DBUS_REPLY_TIMEOUT,
                AsyncReadyCallback,
-               NULL);
+               &M);
 
        if (error != ERROR_NONE) {
                GLOGD("Failed to invoke dbus method nonblock");
                return error;
        }
 
-       RunMainLoop();
+       M.run(GMAINTIMEOUT);
        GLOGD("Succeeded to get statistics per app ID");
 
        return ERROR_NONE;
@@ -236,6 +212,7 @@ error_e Statistics::GetStatistics(void)
 
 error_e Statistics::GetTotalStatistics(void)
 {
+       MainLoop M;
        GVariant *params = NULL;
        error_e error = ERROR_NONE;
 
@@ -248,14 +225,14 @@ error_e Statistics::GetTotalStatistics(void)
                params,
                DBUS_REPLY_TIMEOUT,
                AsyncReadyCallback,
-               NULL);
+               &M);
 
        if (error != ERROR_NONE) {
                GLOGD("Failed to invoke dbus method nonblock");
                return error;
        }
 
-       RunMainLoop();
+       M.run(GMAINTIMEOUT);
        GLOGD("Succeeded to get total statistics");
 
        return ERROR_NONE;
@@ -263,6 +240,7 @@ error_e Statistics::GetTotalStatistics(void)
 
 error_e Statistics::GetAllStatistics(void)
 {
+       MainLoop M;
        GVariant *params = NULL;
        error_e error = ERROR_NONE;
 
@@ -275,14 +253,14 @@ error_e Statistics::GetAllStatistics(void)
                params,
                DBUS_REPLY_TIMEOUT,
                AsyncReadyCallback,
-               NULL);
+               &M);
 
        if (error != ERROR_NONE) {
                GLOGD("Failed to invoke dbus method nonblock");
                return error;
        }
 
-       RunMainLoop();
+       M.run(GMAINTIMEOUT);
        GLOGD("Succeeded to get all statistics");
 
        return ERROR_NONE;
index 79f9c2c..888544e 100644 (file)
@@ -21,6 +21,7 @@
 #define APP_ID_LEN 128
 #define IFNAME_LEN 16
 #define SUBSCRIBER_ID_LEN 128
+#define MAC_LEN 18
 
 #ifdef USE_DLOG
 #include <dlog.h>
index 1141021..8b467f7 100644 (file)
 #include "restriction.h"
 #include "statistics.h"
 #include "manager.h"
+#include "stc-manager.h"
+#include "firewall.h"
+
+#define GTEST_MAC "1:c:e:b:00:da"
 
 using ::testing::InitGoogleTest;
 using ::testing::Test;
 using ::testing::TestCase;
 
+typedef enum {
+       FIREWALL_UNKONWN,
+       FIREWALL_UNLOCKED,
+       FIREWALL_LOCKED
+} firewall_lock_e;
+
 TEST(StcManager_Statistics, Init_p)
 {
        error_e ret = ERROR_NONE;
@@ -138,7 +148,8 @@ TEST(StcManager_Restriction, Set_p)
                IFACE_DATACALL,
                0,
                0,
-               ROAMING_UNKNOWN);
+               ROAMING_UNKNOWN,
+               GTEST_MAC);
        EXPECT_EQ(ERROR_NONE, ret);
 
        ret = rest.SetRstriction();
@@ -150,7 +161,8 @@ TEST(StcManager_Restriction, Set_p)
                IFACE_WIFI,
                2,
                1,
-               ROAMING_UNKNOWN);
+               ROAMING_UNKNOWN,
+               GTEST_MAC);
        EXPECT_EQ(ERROR_NONE, ret);
 
        ret = rest.SetRstriction();
@@ -162,32 +174,14 @@ TEST(StcManager_Restriction, Set_p)
                IFACE_WIRED,
                100000,
                80000,
-               ROAMING_UNKNOWN);
+               ROAMING_UNKNOWN,
+               GTEST_MAC);
        EXPECT_EQ(ERROR_NONE, ret);
 
        ret = rest.SetRstriction();
        EXPECT_EQ(ERROR_NONE, ret);
 }
 
-TEST(StcManager_Restriction, Exclude_p)
-
-{
-       error_e ret = ERROR_NONE;
-       Restriction rest;
-
-       ret = rest.SetRule("TOTAL_DATACALL",
-               "seth_w0",
-               NULL,
-               IFACE_DATACALL,
-               0,
-               0,
-               ROAMING_UNKNOWN);
-       EXPECT_EQ(ERROR_NONE, ret);
-
-       ret = rest.ExcludeRstriction();
-       EXPECT_EQ(ERROR_NONE, ret);
-}
-
 TEST(StcManager_Restriction, Get_p)
 {
        error_e ret = ERROR_NONE;
@@ -199,7 +193,8 @@ TEST(StcManager_Restriction, Get_p)
                IFACE_DATACALL,
                0,
                0,
-               ROAMING_UNKNOWN);
+               ROAMING_UNKNOWN,
+               GTEST_MAC);
        EXPECT_EQ(ERROR_NONE, ret);
 
        ret = rest.GetRstriction();
@@ -211,7 +206,8 @@ TEST(StcManager_Restriction, Get_p)
                IFACE_WIFI,
                0,
                0,
-               ROAMING_UNKNOWN);
+               ROAMING_UNKNOWN,
+               GTEST_MAC);
        EXPECT_EQ(ERROR_NONE, ret);
 
        ret = rest.GetRstriction();
@@ -223,7 +219,8 @@ TEST(StcManager_Restriction, Get_p)
                IFACE_DATACALL,
                0,
                0,
-               ROAMING_UNKNOWN);
+               ROAMING_UNKNOWN,
+               GTEST_MAC);
        EXPECT_EQ(ERROR_NONE, ret);
 
        ret = rest.GetRstriction();
@@ -239,40 +236,6 @@ TEST(StcManager_Restriction, GetAll_p)
        EXPECT_EQ(ERROR_NONE, ret);
 }
 
-TEST(StcManager_Restriction, GetRst_p)
-{
-       error_e ret = ERROR_NONE;
-       Restriction rest;
-       restriction_status_e status = RESTRICTION_UNKNOWN;
-
-       ret = rest.SetRule("Test_tel",
-               "seth_w0",
-               NULL,
-               IFACE_DATACALL,
-               0,
-               0,
-               ROAMING_UNKNOWN);
-       EXPECT_EQ(ERROR_NONE, ret);
-
-       ret = rest.GetRstrictionStatus(&status);
-       EXPECT_EQ(ERROR_NONE, ret);
-       EXPECT_LE(RESTRICTION_UNKNOWN, status);
-       EXPECT_GE(RESTRICTION_LAST_ELEM, status);
-
-       ret = rest.SetRule("Test_eth",
-               "eth0",
-               NULL,
-               IFACE_WIRED,
-               100000,
-               80000,
-               ROAMING_UNKNOWN);
-       EXPECT_EQ(ERROR_NONE, ret);
-
-       ret = rest.GetRstrictionStatus(&status);
-       EXPECT_EQ(ERROR_NONE, ret);
-       EXPECT_LE(RESTRICTION_UNKNOWN, status);
-       EXPECT_GE(RESTRICTION_LAST_ELEM, status);
-}
 
 TEST(StcManager_Restriction, Unset_p)
 {
@@ -285,7 +248,8 @@ TEST(StcManager_Restriction, Unset_p)
                IFACE_DATACALL,
                0,
                0,
-               ROAMING_UNKNOWN);
+               ROAMING_UNKNOWN,
+               GTEST_MAC);
        EXPECT_EQ(ERROR_NONE, ret);
 
        ret = rest.UnsetRstriction();
@@ -297,7 +261,8 @@ TEST(StcManager_Restriction, Unset_p)
                IFACE_WIFI,
                2,
                1,
-               ROAMING_UNKNOWN);
+               ROAMING_UNKNOWN,
+               GTEST_MAC);
        EXPECT_EQ(ERROR_NONE, ret);
 
        ret = rest.UnsetRstriction();
@@ -309,7 +274,8 @@ TEST(StcManager_Restriction, Unset_p)
                IFACE_WIRED,
                100000,
                80000,
-               ROAMING_UNKNOWN);
+               ROAMING_UNKNOWN,
+               GTEST_MAC);
        EXPECT_EQ(ERROR_NONE, ret);
 
        ret = rest.UnsetRstriction();
@@ -321,7 +287,8 @@ TEST(StcManager_Restriction, Unset_p)
                IFACE_DATACALL,
                0,
                0,
-               ROAMING_UNKNOWN);
+               ROAMING_UNKNOWN,
+               GTEST_MAC);
        EXPECT_EQ(ERROR_NONE, ret);
 
        ret = rest.UnsetRstriction();
@@ -361,6 +328,269 @@ TEST(StcManager_Statistics, Reset_p)
        EXPECT_EQ(ERROR_NONE, ret);
 }
 
+TEST(StcManager_Firewall, Lock_p)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       int state;
+       f.GetLock(&state);
+       if (state == FIREWALL_LOCKED)
+               f.Unlock();
+
+       ret = f.Lock();
+       EXPECT_EQ(ERROR_NONE, ret);
+       f.Unlock();
+}
+
+TEST(StcManager_Firewall, Unlock_p)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       int state;
+       f.GetLock(&state);
+       if (state != FIREWALL_LOCKED)
+               f.Lock();
+
+       ret = f.Unlock();
+       EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, GetLock_p)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       int state;
+       f.GetLock(&state);
+       EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, AddChain_p)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       ret = f.AddChain("gtest");
+       EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, AddRule_p)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       ret = f.SetRule("gtest",
+                       STC_FW_DIRECTION_IN,
+                       STC_FW_IP_SINGLE,
+                       STC_FW_IP_SINGLE,
+                       STC_FW_PORT_SINGLE,
+                       STC_FW_PORT_SINGLE,
+                       STC_FW_PROTOCOL_TCP,
+                       STC_FW_FAMILY_V4,
+                       "1.1.1.1",
+                       "1.1.1.1",
+                       "2.2.2.2",
+                       "2.2.2.2",
+                       9000,
+                       9000,
+                       9000,
+                       9000,
+                       "wlan0",
+                       STC_FW_RULE_TARGET_ACCEPT);
+
+       EXPECT_EQ(ERROR_NONE, ret);
+
+       ret = f.AddRule();
+       EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, AddRule_n)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       ret = f.SetRule("gtest",
+                       STC_FW_DIRECTION_IN,
+                       STC_FW_IP_SINGLE,
+                       STC_FW_IP_SINGLE,
+                       STC_FW_PORT_SINGLE,
+                       STC_FW_PORT_SINGLE,
+                       STC_FW_PROTOCOL_TCP,
+                       STC_FW_FAMILY_V4,
+                       "1.1.1.1",
+                       "1.1.1.1",
+                       "2.2.2.2",
+                       "2.2.2.2",
+                       9000,
+                       9000,
+                       9000,
+                       9000,
+                       "wlan0",
+                       STC_FW_RULE_TARGET_MAX);
+       EXPECT_EQ(ERROR_NONE, ret);
+
+       ret = f.AddRule();
+       EXPECT_NE(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, UpdateRule_n)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       ret = f.SetRule("gtest",
+                       STC_FW_DIRECTION_IN,
+                       STC_FW_IP_SINGLE,
+                       STC_FW_IP_SINGLE,
+                       STC_FW_PORT_SINGLE,
+                       STC_FW_PORT_SINGLE,
+                       STC_FW_PROTOCOL_TCP,
+                       STC_FW_FAMILY_V4,
+                       "1.1.1.1",
+                       "1.1.1.1",
+                       "2.2.2.2",
+                       "2.2.2.2",
+                       9000,
+                       9000,
+                       9000,
+                       9000,
+                       "wlan0",
+                       STC_FW_RULE_TARGET_MAX);
+       EXPECT_EQ(ERROR_NONE, ret);
+
+       ret = f.UpdateRule();
+       EXPECT_NE(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, RemoveRule_n)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       ret = f.SetRule("gtest",
+                       STC_FW_DIRECTION_IN,
+                       STC_FW_IP_SINGLE,
+                       STC_FW_IP_SINGLE,
+                       STC_FW_PORT_SINGLE,
+                       STC_FW_PORT_SINGLE,
+                       STC_FW_PROTOCOL_TCP,
+                       STC_FW_FAMILY_V4,
+                       "1.1.1.1",
+                       "1.1.1.1",
+                       "2.2.2.2",
+                       "2.2.2.2",
+                       9000,
+                       9000,
+                       9000,
+                       9000,
+                       "wlan0",
+                       STC_FW_RULE_TARGET_MAX);
+       EXPECT_EQ(ERROR_NONE, ret);
+
+       ret = f.RemoveRule();
+       EXPECT_NE(ERROR_NONE, ret);
+}
+
+
+TEST(StcManager_Firewall, SetChain_p)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       ret = f.SetChain("gtest", 1);
+       EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, SetChain_n)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       ret = f.SetChain(NULL, 0);
+       EXPECT_NE(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, UnsetChain_p)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       ret = f.UnsetChain("gtest");
+       EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, UnsetChain_n)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       ret = f.UnsetChain(NULL);
+       EXPECT_NE(ERROR_NONE, ret);
+}
+
+
+TEST(StcManager_Firewall, RemoveChain_p)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       ret = f.RemoveChain("gtest");
+       EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, RemoveChain_n)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       ret = f.RemoveChain("not_a_chain");
+       EXPECT_NE(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, FlushChain_p)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       ret = f.AddChain("gtest_chains");
+       EXPECT_EQ(ERROR_NONE, ret);
+
+       ret = f.FlushChain("gtest_chains");
+       EXPECT_EQ(ERROR_NONE, ret);
+
+       f.RemoveChain("gtest_chains");
+}
+
+
+TEST(StcManager_Firewall, FlushChain_n)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       ret = f.FlushChain(NULL);
+       EXPECT_NE(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, GetAllChain_p)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       ret = f.GetAllChain();
+       EXPECT_EQ(ERROR_NONE, ret);
+}
+
+TEST(StcManager_Firewall, GetAllRule_p)
+{
+       error_e ret = ERROR_NONE;
+       Firewall f;
+
+       ret = f.GetAllRule();
+       EXPECT_EQ(ERROR_NONE, ret);
+}
+
 TEST(StcManager_Manager, Stop_p)
 {
        error_e ret = ERROR_NONE;