Refactoring structures for monitoring and restrictions
[platform/core/connectivity/stc-manager.git] / src / monitor / stc-monitor.c
index 9fdf529..f81cf7c 100755 (executable)
 #include "helper-nfacct-rule.h"
 #include "helper-net-cls.h"
 #include "helper-cgroup.h"
+#include "helper-iptables.h"
 #include "counter.h"
 #include "table-statistics.h"
 #include "table-counters.h"
-#include "stc-exception.h"
 #include "stc-monitor.h"
-#include "stc-manager-plugin.h"
-
-#define MAX_INT_LENGTH 128
-#define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
-
-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_reached;
-} classid_bytes_context_s;
+#include "stc-manager-plugin-exception.h"
+#include "stc-manager-plugin-tether.h"
 
 static stc_system_s *g_system = NULL;
 
-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;
-
-       return NFACCT_JUMP_UNKNOWN;
-}
-
-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;
-
-       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);
-}
-
-#if STC_DEBUG_LOG
-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 [%d], 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 [%s], App ID [%s],"
-                " 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;
-}
-#endif
-
-#if 0
-static void __apps_tree_printall(void)
-{
-       g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
-}
-#endif
-
-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)) {
-#if STC_DEBUG_LOG
-               STC_LOGD("key not found");
-#endif
-               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;
-}
-
-static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
-                                            gpointer data)
-{
-       guint *pid_count = (guint *)data;
-       (*pid_count)++;
-       return TRUE;
-}
-
-static gboolean __add_application_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 FALSE;
-
-       if (stc && connection && connection->ifname) {
-               struct nfacct_rule counter;
-
-               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_COUNTER;
-               counter.iftype = connection->type;
-               g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
-
-               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;
-       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 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_COUNTER;
-               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 [%lu], ifname [%s], "
-                "iftype [%d], rst_state [%d], "
-                "limit [ (%lld) bytes], "
-                "warn_limit [ (%lld) bytes], "
-                "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->rst_state,
-                rstn_value->data_limit,
-                rstn_value->data_warn_limit,
-                rstn_value->data_counter,
-                rstn_key->roaming, rstn_key->subscriber_id);
-}
-
-static void __process_restriction(enum traffic_restriction_type rst_type,
-                                 stc_rstn_key_s *rstn_key,
-                                 stc_rstn_value_s *rstn_value, void *data)
-{
-       int64_t effective_data_limit, effective_data_warn_limit;
-       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)
-               return;
-
-       /* classid is invalid */
-       if (rstn_value->classid == STC_UNKNOWN_CLASSID)
-               return;
-
-       effective_data_limit = rstn_value->data_limit;
-       effective_data_warn_limit = rstn_value->data_warn_limit;
-
-       if (rst_type == RST_SET) {
-               /* TODO: Change this to runtime memory */
-               table_counters_info info;
-
-               memset(&info, 0, sizeof(table_counters_info));
-               table_counters_get(rstn_value->restriction_id, &info);
-
-               effective_data_limit -= info.data_counter;
-               effective_data_warn_limit -= info.data_counter;
-
-               if (effective_data_limit < 0) {
-                       effective_data_limit = 0;
-                       rstn_value->data_limit_reached = TRUE;
-               }
-
-               if (effective_data_warn_limit < 0)
-                       effective_data_warn_limit = 0;
-
-               STC_LOGD("datausage [%lld] bytes", info.data_counter);
-       }
-
-       STC_LOGD("rstn_id [%llu], effective_data_limit [%lld] bytes, "
-                "effective_data_warn_limit [%lld] bytes",
-                rstn_value->restriction_id, effective_data_limit,
-                effective_data_warn_limit);
-
-       switch (rst_type) {
-       case RST_SET:
-               if (effective_data_limit <= 0) {
-                       char *default_ifname = stc_default_connection_get_ifname();
-                       struct nfacct_rule counter;
-                       stc_s *stc = stc_get_manager();
-
-                       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_counter_socket();
-                       }
-
-                       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);
-
-                       generate_counter_name(&counter);
-                       g_free(default_ifname);
-
-                       /* iptables rule */
-                       __add_iptables_in(&counter);
-                       __add_iptables_out(&counter);
-
-                       /* ip6tables rule */
-                       __add_ip6tables_in(&counter);
-                       __add_ip6tables_out(&counter);
-               }
-
-               rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
-               rstn_value->data_limit_reached = FALSE;
-               break;
-       case RST_EXCLUDE:
-               ;//Do Nothing
-               break;
-       case RST_UNSET:
-               {
-                       char *default_ifname = stc_default_connection_get_ifname();
-                       struct nfacct_rule counter;
-                       stc_s *stc = stc_get_manager();
-
-                       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_counter_socket();
-                       }
-
-                       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);
-
-                       generate_counter_name(&counter);
-                       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->rst_state = STC_RESTRICTION_REMOVED;
-                       rstn_value->data_limit_reached = FALSE;
-               }
-               break;
-       default:
-               ;//Do Nothing
-       }
-}
-
-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->rst_state == STC_RESTRICTION_REMOVED)
-               goto out;
-
-       /* remove restriction from system */
-       __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
-
-       __print_rstn(rstn_key, rstn_value);
-out:
-       return FALSE;
-}
-
-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)
+static int __vconf_get_int(const char *key, int *value)
 {
-       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");
-               return STC_ERROR_NO_DATA;
-       }
-
-       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);
-       }
+       int ret = 0;
 
-       if (!g_tree_remove(g_system->apps, app_key)) {
-               ret = STC_ERROR_NO_DATA;
-               STC_LOGE("key not found");
+       ret = vconf_get_int(key, value);
+       if (ret != VCONF_OK) {
+               STC_LOGE("Failed to get vconfkey [%s] value", key);
+               return -1;
        }
 
-       return ret;
+       return 0;
 }
 
 static stc_error_e __close_contr_sock(stc_system_s *system)
@@ -712,866 +80,249 @@ static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
        }
 
        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)) {
-               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 gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
-                                     stc_rstn_value_s *rstn_value,
-                                     classid_bytes_context_s *context)
-{
-       switch (context->counter->iotype) {
-       case NFACCT_COUNTER_IN:
-       case NFACCT_COUNTER_OUT:
-               rstn_value->data_counter += context->bytes;
-
-               if (rstn_value->data_counter >= rstn_value->data_warn_limit &&
-                   rstn_value->warn_limit_crossed_notified == FALSE) {
-
-                       gboolean rv;
-                       char iftype[MAX_INT_LENGTH];
-                       char byte[MAX_INT_LENGTH];
-                       stc_s *stc = (stc_s *)stc_get_manager();
-                       ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
-
-                       /* emit signal */
-                       rv = stc_manager_dbus_emit_signal(stc->connection,
-                                                         STC_DBUS_SERVICE_RESTRICTION_PATH,
-                                                         STC_DBUS_INTERFACE_RESTRICTION,
-                                                         "WarnThresholdCrossed",
-                                                         g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
-                       if (rv == TRUE)
-                               rstn_value->warn_limit_crossed_notified = TRUE;
-
-                       snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
-                       snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_warn_limit);
-                       stc_send_warn_message_to_net_popup("warn threshold crossed",
-                                                          "warning_noti",
-                                                          rstn_key->app_id,
-                                                          iftype, byte);
-               }
-
-               if (rstn_value->data_counter >= rstn_value->data_limit &&
-                   rstn_value->rstn_limit_crossed_notified == FALSE) {
-
-                       gboolean rv;
-                       char iftype[MAX_INT_LENGTH];
-                       char byte[MAX_INT_LENGTH];
-                       stc_s *stc = (stc_s *)stc_get_manager();
-                       ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
-
-                       /* 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->data_limit_reached = TRUE;
-
-                       /* emit signal */
-                       rv = stc_manager_dbus_emit_signal(stc->connection,
-                                                         STC_DBUS_SERVICE_RESTRICTION_PATH,
-                                                         STC_DBUS_INTERFACE_RESTRICTION,
-                                                         "RestrictionThresholdCrossed",
-                                                         g_variant_new("(si)", rstn_key->app_id, rstn_key->iftype));
-                       if (rv == TRUE)
-                               rstn_value->rstn_limit_crossed_notified = TRUE;
-
-                       snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
-                       snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->data_limit);
-                       stc_send_restriction_message_to_net_popup("restriction threshold crossed",
-                                                                 "restriction_noti", rstn_key->app_id,
-                                                                 iftype, byte);
-               }
-
-               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)) {
-               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)
-               goto try_next_callback;
-
-       if (rstn_value->data_limit_reached == TRUE) {
-               context->data_limit_reached = TRUE;
-               goto try_next_callback;
-       }
-
-       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;
-}
-
-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));
-
-       stat_key.classid = app_value->classid;
-       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");
-
-       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;
-       stat.ground = STC_APP_STATE_UNKNOWN;
-
-       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 = time(0);
-
-       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;
-}
-
-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->data_counter
-       };
-
-       table_counters_update_counters(&info);
-
-       return FALSE;
-}
-
-static gboolean __flush_rstns_counter_to_database(gpointer user_data)
-{
-       time_t current_time = time(0);
-
-       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;
-}
-
-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;
-
-#if STC_DEBUG_LOG
-               __apps_tree_foreach_print(app_key, app_value, NULL);
-#endif
-               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;
-
-#if STC_DEBUG_LOG
-               __apps_tree_foreach_print(app_key, app_value, NULL);
-#endif
-               break;
-       default:
-               STC_LOGE("unknown iotype");
-       }
-}
-
-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_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)
-               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 void __fill_nfacct_result(char *cnt_name, int64_t bytes,
-                                struct counter_arg *carg)
-{
-       struct nfacct_rule counter = {
-               .carg = carg,
-               .name = {0},
-               .ifname = {0},
-               0
-       };
-
-       classid_bytes_context_s context = {
-               .counter = &counter,
-               .bytes = bytes,
-               .data_limit_reached = FALSE,
-       };
-
-       STC_LOGD("cnt_name %s", cnt_name);
-
-       if (!recreate_counter_by_name(cnt_name, &counter)) {
-               STC_LOGE("Can't parse counter name %s", cnt_name);
-               return;
-       }
-
-       STC_LOGI("classid %lu, 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 (g_system->apps)
-               g_tree_foreach(g_system->apps,
-                              __apps_counter_update_foreach_classid,
-                              &context);
-}
-
-static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
-                          void *user_data)
-{
-       struct counter_arg *carg = user_data;
-       char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
-       if (carg->initiate) {
-               /**
-                * TODO: this will be used when daemon starts to update existing
-                * counter data if present.
-                *
-                populate_counters(cnt_name, carg);
-                */
-       } else {
-               int64_t *bytes_p =
-                       (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
-               int bytes = be64toh(*bytes_p);
-               if (bytes) {
-                       ++carg->serialized_counters;
-                       __fill_nfacct_result(cnt_name, bytes, carg);
-               }
-       }
-
-       return 0;
-}
-
-static int __post_fill_counters(void *user_data)
-{
-       struct counter_arg *carg = user_data;
-
-       if (carg->initiate)
-               carg->initiate = 0;
-
-       return 0;
-}
-
-static void __process_network_counter(struct genl *ans,
-                                     struct counter_arg *carg)
-{
-       struct netlink_serialization_params ser_params = {
-               .carg = carg,
-               .ans = ans,
-               .eval_attr = __fill_counters,
-               .post_eval_attr = __post_fill_counters,
-       };
-
-       netlink_serialization_command *netlink =
-               netlink_create_command(&ser_params);
-       if (!netlink) {
-               STC_LOGE("Can not create command");
-               return;
-       }
-
-       netlink->deserialize_answer(&(netlink->params));
-}
-
-static gboolean __process_contr_reply(GIOChannel *source,
-                                     GIOCondition condition,
-                                     gpointer user_data)
-{
-       int sock = g_io_channel_unix_get_fd(source);
-       struct genl ans;
-       int ret;
-       stc_s *stc = stc_get_manager();
-
-       if ((condition & G_IO_ERR) ||
-           (condition & G_IO_HUP) ||
-           (condition & G_IO_NVAL)) {
-               /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
-
-               STC_LOGE("Counter socket received G_IO event, closing socket."
-                        "G_IO_ERR [%d], G_IO_HUP [%d], G_IO_NVAL [%s]",
-                        (condition & G_IO_ERR), (condition & G_IO_HUP),
-                        (condition & G_IO_NVAL));
-               __close_and_reopen_contr_sock(g_system);
-               return FALSE;
-       }
-
-       if (stc == NULL) {
-               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;
-
-       stc->carg->ans_len = ret;
-       __process_network_counter(&ans, stc->carg);
-
-       g_idle_add(__flush_apps_stats_to_database, NULL);
-       g_idle_add(__flush_rstns_counter_to_database, NULL);
-out:
-       return TRUE;
-}
-
-static gboolean __update_contr_cb(void *user_data)
-{
-       /* Here we just sent command, answer we receive in another callback */
-       stc_s *stc = stc_get_manager();
-       ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
-       if (!stc->carg) {
-               stc->carg = MALLOC0(counter_arg_s, 1);
-               if (stc->carg == NULL)
-                       return TRUE;  /* we need to continue the timer */
-
-               stc->carg->sock = stc_monitor_get_counter_socket();
-       }
-
-       /* STC_LOGD("Get all counters"); */
-       nfacct_send_get_all(stc->carg);
-
-       /* we need to continue the timer */
-       return TRUE;
-}
-#if 0
-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);
-}
-#endif
-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;
-
-       /* rstn rule is already removed */
-       if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
-               return FALSE;
-
-       __process_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->rst_state == STC_RESTRICTION_ACTIVATED)
-               return FALSE;
-
-       if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
-               __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
-       else
-               __process_restriction(RST_SET, rstn_key, rstn_value, data);
-
-       __print_rstn(rstn_key, rstn_value);
-
-       return FALSE;
-}
-
-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->rst_state == STC_RESTRICTION_ACTIVATED)
-               return FALSE;
-
-       if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
-               __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
-       else
-               __process_restriction(RST_SET, rstn_key, rstn_value, data);
-
-       return FALSE;
-}
-
-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");
-               return STC_ERROR_NO_DATA;
-       }
-
-       __remove_restriction(key, lookup_value, NULL);
-
-       /* remove counter also */
-       table_counters_delete(lookup_value->restriction_id);
-
-       if (!g_tree_remove(g_system->rstns, key)) {
-               STC_LOGD("key not found");
-               return STC_ERROR_NO_DATA;
-       }
-
-       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_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) {
-               stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
-               if (!rstn_key) {
-                       STC_LOGE("rstn_key allocation failed");
-                       return STC_ERROR_OUT_OF_MEMORY;
-               }
-
-               rstn_value = MALLOC0(stc_rstn_value_s, 1);
-               if (!rstn_value) {
-                       STC_LOGE("rstn_value allocation failed");
-                       FREE(rstn_key);
-                       return STC_ERROR_OUT_OF_MEMORY;
-               }
-
-               rstn_key->app_id = g_strdup(key->app_id);
-               rstn_key->ifname = g_strdup(key->ifname);
-               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);
+               g_source_remove(system->contr_gsource_id);
+               system->contr_gsource_id = 0;
        }
 
-       rstn_value->restriction_id = value->restriction_id;
-       rstn_value->rst_state = value->rst_state;
-       rstn_value->classid = value->classid;
-       rstn_value->data_limit = value->data_limit;
-       rstn_value->data_warn_limit = value->data_warn_limit;
-       rstn_value->data_counter = 0;
-       rstn_value->warn_limit_crossed_notified = FALSE;
-       rstn_value->rstn_limit_crossed_notified = FALSE;
+       /* 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 (debug == TRUE)
-               __add_restriction_debug(key, rstn_value, NULL);
-       else
-               __add_restriction(key, rstn_value, NULL);
+       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 stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
-                                           void *user_data)
+static void __check_rstn_limit_exceeded(gpointer data,
+                               gpointer 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));
+       stc_rstn_data_s *rstn_data = (stc_rstn_data_s *)data;
+       int32_t *limit_exceeded = (int32_t *)user_data;
 
-       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.rst_state = info->rst_state;
-       value.restriction_id = info->restriction_id;
-
-       if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
-               value.classid = get_classid_by_app_id(info->app_id, TRUE);
-       else
-               value.classid = STC_UNKNOWN_CLASSID;
-
-       value.data_limit = info->data_limit;
-       value.data_warn_limit = info->data_warn_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();
+       if (rstn_data->limit_exceeded != 0)
+               *limit_exceeded = rstn_data->limit_exceeded;
 }
 
-static gboolean __add_rstn_foreach_application(gpointer key,
-                                              gpointer value,
-                                              gpointer data)
+static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
+                               struct counter_arg *carg)
 {
-       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;
+       stc_monitor_rstn_reset_time_counters_if_required();
 
-       /* rstn rule is not for this application */
-       if (g_strcmp0(rstn_key->app_id, app_id) != 0)
-               goto out;
+       struct nfacct_rule counter = {
+               .carg = carg,
+               .name = {0},
+               .ifname = {0},
+               0
+       };
 
-       /* rstn rule is already applied */
-       if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
-               goto out;
+       classid_bytes_context_s context = {
+               .counter = &counter,
+               .bytes = bytes,
+               .data_limit_exceeded = FALSE,
+       };
 
-       /* add restriction to system */
-       if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
-               __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
-       else
-               __process_restriction(RST_SET, rstn_key, rstn_value, data);
+       if (!recreate_counter_by_name(cnt_name, &counter)) {
+               STC_LOGE("Can't parse counter name %s", cnt_name);
+               return;
+       }
 
-       __print_rstn(rstn_key, rstn_value);
-out:
-       return FALSE;
-}
+       if (STC_DEBUG_LOG)
+               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);
 
-static void __add_rstns_for_application(gchar *app_id)
-{
-       g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
-                      app_id);
-}
+       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;
 
-static void __add_application_by_interface(const char *app_id)
-{
-       stc_app_key_s app_key;
-       stc_app_value_s app_value;
+                       stc_monitor_app_update_iface_counter(&context);
 
-       if (app_id == NULL)
-               return;
+                       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);
 
-       memset(&app_key, 0, sizeof(stc_app_key_s));
-       memset(&app_value, 0, sizeof(stc_app_value_s));
+                               if (limit_exceeded != 0)
+                                       return;
+                       }
 
-       app_key.pkg_id = g_strdup(app_id);
-       app_key.app_id = g_strdup(app_id);
+                       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);
+               }
 
-       app_value.type = STC_APP_TYPE_NONE;
-       app_value.processes = NULL;
-       app_value.counter.in_bytes = 0;
-       app_value.counter.out_bytes = 0;
+               if (g_system->rstns) {
+                       stc_rstn_value_s *lookup_value;
+                       uint32_t classid = context.counter->classid;
 
-       stc_monitor_application_add(app_key, app_value);
+                       stc_monitor_rstn_update_iface_counter(&context);
+                       context.counter->classid = classid;
 
-       FREE(app_key.pkg_id);
-       FREE(app_key.app_id);
+                       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 __vconf_get_int(const char *key, int *value)
+static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
+                          void *user_data)
 {
-       int ret = 0;
-
-       ret = vconf_get_int(key, value);
-       if (ret != VCONF_OK) {
-               STC_LOGE("Failed to get vconfkey [%s] value", key);
-               return -1;
+       struct counter_arg *carg = user_data;
+       char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
+       if (carg->initiate) {
+               /**
+                * TODO: this will be used when daemon starts to update existing
+                * counter data if present.
+                *
+                populate_counters(cnt_name, carg);
+                */
+       } else {
+               int64_t *bytes_p =
+                       (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
+               int bytes = be64toh(*bytes_p);
+               if (bytes) {
+                       ++carg->serialized_counters;
+                       __fill_nfacct_result(cnt_name, bytes, carg);
+               }
        }
 
        return 0;
 }
 
-static int __vconf_set_int(const char *key, int value)
+static int __post_fill_counters(void *user_data)
 {
-       int ret = 0;
+       struct counter_arg *carg = user_data;
 
-       ret = vconf_set_int(key, value);
-       if (ret != VCONF_OK) {
-               STC_LOGE("Failed to set vconfkey [%s] value", key);
-               return -1;
-       }
+       if (carg->initiate)
+               carg->initiate = 0;
 
        return 0;
 }
 
-static guint __get_background_state(void)
-{
-       return g_system->background_state;;
-}
-
-static void __set_background_state(guint state)
-{
-       g_system->background_state = state;
-}
-
-static gboolean __processes_tree_foreach_background(gpointer key,
-                                                   gpointer value,
-                                                   gpointer data)
+static void __process_network_counter(struct genl *ans,
+                                     struct counter_arg *carg)
 {
-       stc_process_key_s *proc_key = (stc_process_key_s *)key;
-       stc_app_key_s *app_key = (stc_app_key_s *)data;
+       struct netlink_serialization_params ser_params = {
+               .carg = carg,
+               .ans = ans,
+               .eval_attr = __fill_counters,
+               .post_eval_attr = __post_fill_counters,
+       };
 
-       if (g_system->background_state)
-               place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
-       else
-               place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
+       netlink_serialization_command *netlink =
+               netlink_create_command(&ser_params);
+       if (!netlink) {
+               STC_LOGE("Can not create command");
+               return;
+       }
 
-       return FALSE;
+       netlink->deserialize_answer(&(netlink->params));
 }
 
-static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
-                                       gpointer data)
+static gboolean __process_contr_reply(GIOChannel *source,
+                                     GIOCondition condition,
+                                     gpointer user_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);
+       int sock = g_io_channel_unix_get_fd(source);
+       struct genl *ans;
+       int ret;
+       stc_s *stc = stc_get_manager();
 
-       return FALSE;
-}
+#ifdef TIZEN_GTESTS
+       void __gcov_flush(void);
+       __gcov_flush();
+#endif
 
-static stc_error_e __process_update_background(void)
-{
-       ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
+       if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
+           (condition & G_IO_NVAL)) {
+               /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
 
-       g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
+               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);
+               return FALSE;
+       }
 
-       return STC_ERROR_NONE;
-}
+       ans = MALLOC0(struct genl, 1);
+       if (ans == NULL) {
+               STC_LOGE("Failed allocate memory to genl reply message");
+               return TRUE;
+       }
 
-#if 0
-static void __excn_hash_foreach_print(gpointer key, gpointer value,
-                                         gpointer data)
-{
-       const char *process_name = key;
-       const char *exe_type = value;
+       if (stc == NULL) {
+               STC_LOGE("Can't get stc data");
+               goto out;
+       }
 
-       STC_LOGI("excn info => process_name [%s] exe_type [%s]",
-               process_name, exe_type);
-}
+       ret = read_netlink(sock, ans, sizeof(struct genl));
 
-static void __excn_hash_printall(void)
-{
-       g_hash_table_foreach(g_system->excns_hash,
-               __excn_hash_foreach_print, NULL);
-}
-#endif
+       if (ret == 0)
+               goto out;
 
-static gboolean __remove_exception_app(gpointer key, gpointer value,
-                                       gpointer data)
-{
-       const char *exe_type = value;
+       stc->carg->ans_len = ret;
+       stc->carg->last_run_time = time(NULL);
 
-       if (g_strcmp0(exe_type, EXE_TYPE_APPLICATION) == 0)
-               return TRUE;
+       __process_network_counter(ans, stc->carg);
 
-       return FALSE;
-}
+       g_idle_add(stc_monitor_app_flush_stats_to_db, NULL);
+       g_idle_add(stc_monitor_rstn_flush_contr_to_db, NULL);
+out:
 
-static void __remove_exception_appall(void)
-{
-       g_hash_table_foreach_remove(g_system->excns_hash,
-               __remove_exception_app, NULL);
+       FREE(ans);
+       return TRUE;
 }
 
-static stc_cb_ret_e __insert_exception_cb(const stc_exceptions_info *info,
-                                           void *user_data)
+static gboolean __update_contr_cb(void *user_data)
 {
-       stc_cb_ret_e ret = STC_CONTINUE;
+       /* Here we just sent command, answer we receive in another callback */
+       stc_s *stc = stc_get_manager();
+       ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
+       if (!stc->carg) {
+               stc->carg = MALLOC0(counter_arg_s, 1);
+               if (stc->carg == NULL)
+                       return TRUE;  /* we need to continue the timer */
 
-       if (g_hash_table_insert(g_system->excns_hash,
-                       g_strdup(info->process_name),
-                       g_strdup(info->exe_type)) != TRUE)
-               ret = STC_CANCEL;
+               stc->carg->sock = g_system->contr_sock;
+       }
 
-       return ret;
-}
+#ifdef TIZEN_GTESTS
+       void __gcov_flush(void);
+       __gcov_flush();
+#endif
 
-static void __fill_exceptions_list(void)
-{
-       table_exceptions_foreach(__insert_exception_cb, NULL);
-       pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
+       /* STC_LOGD("Get all counters"); */
+       nfacct_send_get_all(stc->carg);
 
-       /* __excn_hash_printall(); */
+       /* we need to continue the timer */
+       return TRUE;
 }
 
-static gboolean __update_exceptions_app_list(void *user_data)
+static void __fill_exceptions_list(void)
 {
-       __remove_exception_appall();
-       pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
-
-       /* __excn_hash_printall(); */
-
-       return TRUE;
+       stc_plugin_fill_exception_list();
 }
 
 stc_error_e stc_monitor_init(void)
@@ -1579,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();
@@ -1588,20 +340,12 @@ 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->excns_hash = g_hash_table_new_full(g_str_hash,
-                                       g_str_equal, g_free, g_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)) {
+       if (system->contr_sock < 0) {
                STC_LOGE("failed to open socket");
                FREE(system);
                return STC_ERROR_FAIL;
@@ -1616,13 +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);
+       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 */
@@ -1639,11 +383,7 @@ stc_error_e stc_monitor_init(void)
                        (int *)&g_system->background_state);
 
        __fill_exceptions_list();
-       __fill_restritions_list();
-
-       g_system->excns_timer_id = g_timeout_add_seconds(EXCNS_TIMER_INTERVAL,
-                                                        __update_exceptions_app_list,
-                                                        NULL);
+       stc_monitor_rstns_load();
 
        return STC_ERROR_NONE;
 }
@@ -1661,307 +401,174 @@ stc_error_e stc_monitor_deinit(void)
                g_system->contr_timer_id = 0;
        }
 
-       /* remove exceptions app list update timer */
-       if (g_system->excns_timer_id > 0) {
-               g_source_remove(g_system->excns_timer_id);
-               g_system->excns_timer_id = 0;
-       }
-
        /* 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;
 
-       /* destroy exception hash table */
-       g_hash_table_destroy(g_system->excns_hash);
-       g_system->excns_hash = NULL;
-
        FREE(g_system);
 
        return STC_ERROR_NONE;
 }
 
-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) {
-               STC_LOGD("app_key already present");
-               return STC_ERROR_NONE;
-       }
+       ret_value_msg_if(g_system == NULL, NULL,
+                               "stc monitor not initialized!");
 
-       key = MALLOC0(stc_app_key_s, 1);
-       if (!key) {
-               STC_LOGE("key allocation failed");
-               return STC_ERROR_OUT_OF_MEMORY;
-       }
+       ret_value_msg_if(g_system->apps == NULL, NULL,
+                               "apps is null!");
 
-       value = MALLOC0(stc_app_value_s, 1);
-       if (!value) {
-               STC_LOGE("value allocation failed");
-               FREE(key);
-               return STC_ERROR_OUT_OF_MEMORY;
-       }
+       return g_system->apps;
+}
 
-       key->app_id = g_strdup(app_key.app_id);
-       key->pkg_id = g_strdup(app_key.pkg_id);
+GHashTable *stc_monitor_get_system_rstns(void)
+{
+       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;
+       ret_value_msg_if(g_system->rstns == NULL, NULL,
+                               "rstns is null!");
 
-       value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
-                                          __processes_tree_key_free,
-                                          __processes_tree_value_free);
+       return g_system->rstns;
+}
 
-       /* create cgroup and update classid */
-       value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
+int stc_monitor_get_contr_sock(void)
+{
+       ret_value_msg_if(g_system == NULL, 0,
+                               "stc monitor not initialized!");
 
-       g_tree_insert(g_system->apps, key, value);
+       return g_system->contr_sock;
+}
 
-       /* add nfacct rule for this classid */
-       __add_application_monitor(key, value, stc_get_default_connection());
-       __add_rstns_for_application(app_key.app_id);
+time_t stc_monitor_get_last_month_ts(void)
+{
+       ret_value_msg_if(g_system == NULL, 0,
+                               "stc monitor not initialized!");
 
-       return ret;
+       return g_system->last_month_ts;
 }
 
-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)
+void stc_monitor_set_last_month_ts(time_t time)
 {
-       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_msg_if(g_system == NULL, "stc monitor not initialized!");
 
-       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) {
-               STC_LOGD("app_key not found");
-               return STC_ERROR_FAIL;
-       }
+       g_system->last_month_ts = time;
+}
 
-       proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
-       if (proc_lookup) {
-               STC_LOGD("proc_key already present");
-               return STC_ERROR_NONE;
-       }
+time_t stc_monitor_get_last_week_ts(void)
+{
+       ret_value_msg_if(g_system == NULL, 0,
+                               "stc monitor not initialized!");
 
-       key = MALLOC0(stc_process_key_s, 1);
-       if (!key) {
-               STC_LOGE("key allocation failed");
-               return STC_ERROR_OUT_OF_MEMORY;
-       }
+       return g_system->last_week_ts;
+}
 
-       value = MALLOC0(stc_process_value_s, 1);
-       if (!value) {
-               STC_LOGE("value allocation failed");
-               FREE(key);
-               return STC_ERROR_OUT_OF_MEMORY;
-       }
+void stc_monitor_set_last_week_ts(time_t time)
+{
+       ret_msg_if(g_system == NULL, "stc monitor not initialized!");
 
-       key->pid = proc_key.pid;
+       g_system->last_week_ts = time;
+}
 
-       value->ground = proc_value.ground;
+time_t stc_monitor_get_last_day_ts(void)
+{
+       ret_value_msg_if(g_system == NULL, 0,
+                               "stc monitor not initialized!");
 
-       g_tree_insert(app_lookup->processes, key, value);
+       return g_system->last_day_ts;
+}
 
-       /* add pid to application cgroup */
-       place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
+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;
 }
 
-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,
-       };
+       g_system->rstns_updated = value;
+}
 
-       ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
+gboolean stc_monitor_get_rstns_updated(void)
+{
+       ret_value_msg_if(g_system == NULL, FALSE,
+                               "stc monitor not initialized!");
 
-       g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
-                      &context);
+       return g_system->rstns_updated;
+}
 
-       if (context.entry_removed)
-               __application_remove_if_empty(context.app_key);
+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;
 }
 
-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;
+}
 
-       if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
-               place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
-       else
-               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;
 }
 
-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) {
-               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();
        }
 
        FREE(old_connection.path);
        FREE(old_connection.ifname);
+       FREE(old_connection.tether_iface.ifname);
        old_connection.type = 0;
        old_connection.roaming = 0;
+       old_connection.tether_state = FALSE;
+       old_connection.tether_iface.type = 0;
 
        if (new_connection != NULL && new_connection->path != NULL) {
-               if (g_system->apps)
-                       g_tree_foreach(g_system->apps,
-                                      __add_application_monitor,
-                                      (gpointer)new_connection);
-
-               if (g_system->rstns)
-                       g_tree_foreach(g_system->rstns, __add_restriction,
-                                      NULL);
+               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);
+               old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname);
                old_connection.type = new_connection->type;
                old_connection.roaming = new_connection->roaming;
+               old_connection.tether_state = new_connection->tether_state;
+               old_connection.tether_iface.type = new_connection->tether_iface.type;
        }
 }
 
-stc_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.subscriber_id = g_strdup(info->subscriber_id);
-       key.iftype = info->iftype;
-       key.roaming = info->roaming;
-
-       value.rst_state = info->rst_state;
-       value.restriction_id = info->restriction_id;
-
-       if (value.rst_state != STC_RESTRICTION_EXCLUDED && 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) {
-               __set_background_state(TRUE);
-               __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
-               __process_update_background();
-       }
-
-       value.data_limit = info->data_limit;
-       value.data_warn_limit = info->data_warn_limit;
-
-       ret = __rstn_tree_add(&key, &value, TRUE);
-
-       FREE(key.app_id);
-       FREE(key.ifname);
-       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)) {
-               __set_background_state(FALSE);
-               __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state);
-               __process_update_background();
-       }
-
-       ret = __rstn_tree_remove(&key);
-
-       FREE(key.app_id);
-       FREE(key.ifname);
-       FREE(key.subscriber_id);
-       return ret;
-}
-
-stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
-{
-       ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
-
-       char *exe_type = NULL;
-
-       exe_type = g_hash_table_lookup(g_system->excns_hash, cmdline);
-       if (!exe_type)
-               return STC_ERROR_NO_DATA;
-
-       return STC_ERROR_NONE;
-}
-
-int stc_monitor_get_counter_socket(void)
+API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
 {
-       return g_system->contr_sock;
+       return stc_plugin_check_exception_by_cmdline(cmdline);
 }