Refactoring structures for monitoring and restrictions 21/190121/4
authorhyunuktak <hyunuk.tak@samsung.com>
Thu, 27 Sep 2018 06:13:32 +0000 (15:13 +0900)
committerhyunuktak <hyunuk.tak@samsung.com>
Mon, 15 Oct 2018 06:32:56 +0000 (15:32 +0900)
Change-Id: I386f8921ad31d1e9f2ea37e0e70943a9154aca9e
Signed-off-by: hyunuktak <hyunuk.tak@samsung.com>
27 files changed:
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/include/table-restrictions.h
src/database/tables/table-counters.c
src/database/tables/table-restrictions.c
src/database/tables/table-statistics.c
src/helper/helper-net-cls.c
src/helper/helper-nfacct-rule.c
src/monitor/include/stc-monitor-app.h [new file with mode: 0755]
src/monitor/include/stc-monitor-context.h [new file with mode: 0755]
src/monitor/include/stc-monitor-ipt.h [new file with mode: 0755]
src/monitor/include/stc-monitor-proc.h [new file with mode: 0755]
src/monitor/include/stc-monitor-rstn.h [new file with mode: 0755]
src/monitor/include/stc-monitor.h
src/monitor/stc-default-connection.c
src/monitor/stc-monitor-app.c [new file with mode: 0755]
src/monitor/stc-monitor-ipt.c [new file with mode: 0755]
src/monitor/stc-monitor-proc.c [new file with mode: 0755]
src/monitor/stc-monitor-rstn.c [new file with mode: 0755]
src/monitor/stc-monitor.c
src/stc-manager-util.c
src/stc-restriction.c [changed mode: 0644->0755]

index 94c57a1..1658bc6 100644 (file)
@@ -174,10 +174,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..d524683 100644 (file)
@@ -1,6 +1,6 @@
 Name:       stc-manager
 Summary:    STC(Smart Traffic Control) manager
-Version:    0.0.78
+Version:    0.0.79
 Release:    0
 Group:      Network & Connectivity/Other
 License:    Apache-2.0
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..f04446b
@@ -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
@@ -109,7 +110,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 +138,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 +199,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);
 
@@ -321,8 +323,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);
        }
@@ -362,8 +365,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 +388,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 +438,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 +590,119 @@ 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 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.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);
+               classid = get_classid_by_app_id(bg_app_id, FALSE);
+               stc_monitor_proc_remove(classid, pid);
+
+               classid = get_classid_by_app_id(app_id, TRUE);
+
+               stc_monitor_app_add(classid, app_id, pkg_id, app_value);
+               stc_monitor_proc_add(classid, app_id, proc_value);
+               stc_monitor_proc_update_ground(classid, app_id, proc_value);
 
-               FREE(app_key.pkg_id);
-               FREE(app_key.app_id);
+               FREE(bg_app_id);
                break;
        }
        case STC_CMD_SET_BACKGRD:
        {
-               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;
 
-               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.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);
+               classid = get_classid_by_app_id(app_id, FALSE);
+               stc_monitor_proc_remove(classid, pid);
+
+               classid = get_classid_by_app_id(bg_app_id, TRUE);
+
+               stc_monitor_app_add(classid, bg_app_id, pkg_id, app_value);
+               stc_monitor_proc_add(classid, bg_app_id, proc_value);
+               stc_monitor_proc_update_ground(classid, bg_app_id, proc_value);
 
-               FREE(app_key.pkg_id);
-               FREE(app_key.app_id);
+               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;
 
-               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.processes = NULL;
 
-               proc_key.pid = pid;
-
-               /* services will run always in background. */
+               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);
+               classid = get_classid_by_app_id(bg_app_id, TRUE);
+
+               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 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:
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..f0fd084 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);
@@ -609,11 +599,11 @@ 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*/
@@ -628,7 +618,7 @@ 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]", stat->app_id);
 
 handle_error:
        sqlite3_reset(stmt);
index 238b7de..f9d59e6 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;
 
@@ -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);
index a3e4647..0ae07b7 100644 (file)
@@ -476,22 +476,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 */
@@ -781,6 +773,9 @@ static stc_error_e produce_iface_rule(nfacct_rule_s *rule)
                }
                //LCOV_EXCL_STOP
        }
+
+       rule->classid = classid;
+
        return STC_ERROR_NONE;
 }
 
diff --git a/src/monitor/include/stc-monitor-app.h b/src/monitor/include/stc-monitor-app.h
new file mode 100755 (executable)
index 0000000..17016a0
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * 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-default-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_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(default_connection_s *conn);
+
+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(default_connection_s *conn);
+
+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 100755 (executable)
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 100755 (executable)
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 100755 (executable)
index 0000000..6152c56
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * 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);
+
+#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 100755 (executable)
index 0000000..0480c3b
--- /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-default-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(default_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(default_connection_s *conn);
+
+void stc_monitor_rstns_load(void);
+
+GHashTable *stc_monitor_rstns_init(void);
+
+#endif /* __STC_MONITOR_RSTN_H__ */
index d138cbc..8fa8bc9 100755 (executable)
 #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_default_connection(void *data);
 
 stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline);
 
-int stc_monitor_get_counter_socket(void);
-
 #endif /* __STC_MONITOR_H__ */
index 244a8b9..8c4b1ef 100644 (file)
@@ -426,7 +426,7 @@ static stc_error_e __get_default_profile(GDBusConnection *connection)
 
        __print_default_connection_info();
 
-       stc_monitor_update_rstn_by_default_connection(&g_default_connection);
+       stc_monitor_update_by_default_connection(&g_default_connection);
        stc_firewall_update();
 
        return STC_ERROR_NONE;
@@ -548,7 +548,7 @@ 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) {
                __print_tether_connection_info();
-               stc_monitor_update_rstn_by_default_connection(&g_default_connection);
+               stc_monitor_update_by_default_connection(&g_default_connection);
                stc_firewall_update();
                STC_LOGI("Data monitoring started for tethering iface !");
                return;
@@ -556,7 +556,7 @@ static void __vconf_key_callback(keynode_t *node, void *user_data)
 
        /* 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);
+               stc_monitor_update_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;
diff --git a/src/monitor/stc-monitor-app.c b/src/monitor/stc-monitor-app.c
new file mode 100755 (executable)
index 0000000..22405b9
--- /dev/null
@@ -0,0 +1,584 @@
+/*
+ * 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] 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->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;
+       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_value->app_id, STC_TOTAL_TETHERING))
+               return;
+
+       /* 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_value->app_id, STC_TOTAL_WIFI))
+               return;
+
+       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_value->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(SUBSCRIBERID_NONE);
+
+       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_value->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_value->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;
+}
+
+static gboolean __add_app_monitor_for_tethering(gpointer key,
+                                   gpointer value, gpointer data)
+{
+       stc_app_value_s *app_value = (stc_app_value_s *)value;
+       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_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;
+       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_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 (!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;
+       }
+
+       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);
+}
+
+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();
+
+       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->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);
+
+       /* add nfacct rule for this classid */
+       stc_monitor_app_add_monitor(GUINT_TO_POINTER(classid),
+                               app_value, stc_get_default_connection());
+       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;
+       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;
+
+       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.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(default_connection_s *conn)
+{
+       GHashTable *apps = stc_monitor_get_system_apps();
+
+       if (!apps)
+               return;
+
+       g_hash_table_foreach(apps, stc_monitor_app_add_monitor, conn);
+}
+
+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();
+
+       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;
+       }
+
+       /* remove nfacct rule for this classid */
+       stc_monitor_app_remove_monitor(GUINT_TO_POINTER(classid),
+                               app_lookup, stc_get_default_connection());
+
+       /* 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;
+       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);
+                       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.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);
+               }
+
+               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(default_connection_s *conn)
+{
+       GHashTable *apps = stc_monitor_get_system_apps();
+
+       if (!apps)
+               return;
+
+       g_hash_table_foreach(apps, stc_monitor_app_remove_monitor, conn);
+}
+
+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 100755 (executable)
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 100755 (executable)
index 0000000..8bd13a7
--- /dev/null
@@ -0,0 +1,214 @@
+/*
+ * 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-default-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;
+       guint pid_count = 0;
+
+       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;
+}
+
+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();
+       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) {
+               /* remove nfacct rule for this classid */
+               stc_monitor_app_remove_monitor(GUINT_TO_POINTER(classid),
+                                       app_lookup, stc_get_default_connection());
+               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;
+}
diff --git a/src/monitor/stc-monitor-rstn.c b/src/monitor/stc-monitor-rstn.c
new file mode 100755 (executable)
index 0000000..1bd6704
--- /dev/null
@@ -0,0 +1,1423 @@
+/*
+ * 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)
+{
+       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_contr_sock();
+       }
+
+       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 */
+       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)
+{
+       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_contr_sock();
+       }
+
+       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 */
+       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)
+{
+       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);
+               return;
+       }
+
+       if (!stc->carg) {
+               stc->carg = MALLOC0(counter_arg_s, 1);
+               if (stc->carg == NULL) {
+                       g_free(default_ifname);
+                       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 (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 */
+       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)
+{
+       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);
+               return;
+       }
+
+       if (!stc->carg) {
+               stc->carg = MALLOC0(counter_arg_s, 1);
+               if (stc->carg == NULL) {
+                       g_free(default_ifname);
+                       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 (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 */
+       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)
+{
+       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_data->ifname != NULL && g_strcmp0("", rstn_data->ifname) != 0 &&
+                       (g_strcmp0(connection->tether_iface.ifname, rstn_data->ifname) != 0))
+               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)
+{
+       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_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;
+
+       /* 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);
+               }
+
+               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 */
+                               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_contr_sock();
+                               }
+
+                               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, default_ifname,
+                                         MAX_IFACE_LENGTH);
+
+                               g_free(default_ifname);
+
+                               /* 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;
+       default_connection_s *default_connection = stc_get_default_connection();
+
+       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, default_connection->subscriber_id) != 0)
+               return;
+
+       if (rstn_data->roaming != default_connection->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(default_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_BACKGROUND_APP_ID) == 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(default_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);
+}
index 4f1129c..f81cf7c 100755 (executable)
 #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
+               STC_LOGE("Failed to get vconfkey [%s] value", key);
+               return -1;
        }
 
        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)
+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);
-
-       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);
-       }
-
-       if (context->is_updated) {
-               table_counters_info info;
-               memset(&info, 0, sizeof(table_counters_info));
-
-               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;
-
-               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_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 (g_system->apps)
-               g_tree_foreach(g_system->apps,
-                              __apps_counter_update_foreach_classid,
-                              &context);
+                               if (limit_exceeded != 0)
+                                       return;
+                       }
+
+                       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->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,223 +414,136 @@ 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, 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);
+       ret_value_msg_if(g_system == NULL, NULL,
+                               "stc monitor not initialized!");
 
-       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_default_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);
-
-               if (g_system->rstns)
-                       g_tree_foreach(g_system->rstns,
-                                      __remove_restriction,
-                                      (gpointer)&old_connection);
+               stc_monitor_app_remove_by_connection(&old_connection);
+               stc_monitor_rstn_remove_by_connection(&old_connection);
 
                iptables_flush_chains();
-               //LCOV_EXCL_STOP
        }
 
        FREE(old_connection.path);
@@ -2794,14 +555,8 @@ void stc_monitor_update_rstn_by_default_connection(void *data)
        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);
+               stc_monitor_app_add_by_connection(new_connection);
+               stc_monitor_rstn_add_by_connection(new_connection);
 
                old_connection.path = g_strdup(new_connection->path);
                old_connection.ifname = g_strdup(new_connection->ifname);
@@ -2813,88 +568,7 @@ void stc_monitor_update_rstn_by_default_connection(void *data)
        }
 }
 
-stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
-{
-       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
-       }
-
-       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;
-}
-
-stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
-{
-       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,
-       };
-
-       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
-       }
-
-       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;
-}
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 100644 (file)
new mode 100755 (executable)
index f4cd753..6b207a5
@@ -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;