#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;
typedef struct {
struct nfacct_rule *counter;
int64_t bytes;
- gboolean data_limit_reached;
+ gboolean data_limit_exceeded;
} classid_bytes_context_s;
static stc_system_s *g_system = NULL;
+//LCOV_EXCL_START
+static int __vconf_get_int(const char *key, int *value)
+{
+ int ret = 0;
+
+ ret = vconf_get_int(key, value);
+ if (ret != VCONF_OK) {
+ STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
+ return -1; //LCOV_EXCL_LINE
+ }
+
+ return 0;
+}
+
+static int __vconf_set_int(const char *key, int value)
+{
+ int ret = 0;
+
+ ret = vconf_set_int(key, value);
+ if (ret != VCONF_OK) {
+ STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
+ return -1; //LCOV_EXCL_LINE
+ }
+
+ return 0;
+}
+//LCOV_EXCL_STOP
+
static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
{
if (counter->intend == NFACCT_WARN)
return NFACCT_JUMP_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)
}
//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();
g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
}
- if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
+ 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) {
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();
counter.classid = app_value->classid;
counter.intend = NFACCT_COUNTER;
- if (connection->tether_state == FALSE &&
+ 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;
STC_LOGI("rstn info => rstn_id [%llu], "
"app_id [%s], classid [%u], ifname [%s], "
"iftype [%d], rstn_state [%d], rstn_type [%d], "
- "limit [ (%lld) bytes], "
+ "month_start_date [%d], limit [ (%lld) bytes], "
"warn_limit [ (%lld) bytes], "
- "counter [ (%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->data_limit,
- rstn_value->data_warn_limit,
- rstn_value->data_counter,
+ 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);
}
__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)
{
__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(¤t_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)
{
- int64_t effective_data_limit, effective_data_warn_limit;
default_connection_s *old_connection = (default_connection_s *)data;
default_connection_s *connection = NULL;
if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
return;
- effective_data_limit = rstn_value->data_limit;
- effective_data_warn_limit = rstn_value->data_warn_limit;
+ /* 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;
- if (rstn_type == RST_SET) {
- /* TODO: Change this to runtime memory */
+ 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));
- 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; //LCOV_EXCL_LINE
- rstn_value->data_limit_reached = TRUE; //LCOV_EXCL_LINE
+ 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(¤t_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;
}
- if (effective_data_warn_limit < 0)
- effective_data_warn_limit = 0; //LCOV_EXCL_LINE
+ 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];
- STC_LOGD("datausage [%lld] bytes", info.data_counter);
- }
+ if (effective_limit[i] < 0)
+ rstn_value->limit_exceeded |= (1 << i);
+ }
+ }
- 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);
+ STC_LOGD("rstn_id [%llu], datausage [%llu] bytes",
+ rstn_value->restriction_id, info.data_counter);
- switch (rstn_type) {
- case RST_SET:
- if (effective_data_limit <= 0)
- __add_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
- rstn_key->iftype);
+ 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;
- rstn_value->data_limit_reached = FALSE;
- break;
+ }
+ 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->data_limit_reached = TRUE;
+ 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);
rstn_key->iftype);
rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
- rstn_value->data_limit_reached = FALSE;
- break;
+ 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
}
/* 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;
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)
+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)
{
- 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_plugin_appstatus_send_warn_message("warn threshold crossed",
- "warning_noti",
- rstn_key->app_id,
- iftype, byte);
- }
+ 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);
+}
- if (rstn_value->data_counter >= rstn_value->data_limit &&
- rstn_value->rstn_limit_crossed_notified == FALSE) {
+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;
+ }
- 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");
+ 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);
__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);
- 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_plugin_appstatus_send_restriction_message("restriction threshold crossed",
- "restriction_noti", rstn_key->app_id,
- iftype, byte);
+ 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;
classid_bytes_context_s *context = (classid_bytes_context_s *)data;
uint32_t classid;
- if (context->counter->intend != NFACCT_COUNTER)
+ if (context->counter->intend != NFACCT_COUNTER &&
+ context->counter->intend != NFACCT_TETH_COUNTER)
goto try_next_callback;
- if (rstn_value->data_limit_reached == TRUE) {
- context->data_limit_reached = TRUE; //LCOV_EXCL_LINE
+ if (rstn_value->limit_exceeded == TRUE) {
+ context->data_limit_exceeded = TRUE; //LCOV_EXCL_LINE
goto try_next_callback; //LCOV_EXCL_LINE
}
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;
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);
static gboolean __flush_apps_stats_to_database(gpointer user_data)
{
time_t current_time = 0;
- time(¤t_time);
+ 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;
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
+ .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);
static gboolean __flush_rstns_counter_to_database(gpointer user_data)
{
- time_t current_time = time(0);
+ 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;
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)
+ if (context->counter->intend != NFACCT_COUNTER &&
+ context->counter->intend != NFACCT_TETH_COUNTER)
goto try_next_callback;
__interface_counter_update(app_key, app_value, context);
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);
+ }
+
+ 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();
+ }
+
+ 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);
+ }
+ }
+ }
+
+try_next_callback:
+ return FALSE;
+}
+
+static void __reset_time_counters_if_required(void)
+{
+ reset_time_limits_context_s context;
+
+ if (g_system == NULL) {
+ STC_LOGE("stc monitor not initialized!");
+ return;
+ }
+
+ 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 (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);
+ }
+
+ 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);
+ }
+}
+
static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
struct counter_arg *carg)
{
+ __reset_time_counters_if_required();
+
struct nfacct_rule counter = {
.carg = carg,
.name = {0},
classid_bytes_context_s context = {
.counter = &counter,
.bytes = bytes,
- .data_limit_reached = FALSE,
+ .data_limit_exceeded = FALSE,
};
if (!recreate_counter_by_name(cnt_name, &counter)) {
goto out;
stc->carg->ans_len = ret;
+ stc->carg->last_run_time = time(NULL);
+
__process_network_counter(ans, stc->carg);
g_idle_add(__flush_apps_stats_to_database, NULL);
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;
}
if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
return FALSE;
- if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
+ if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
__process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
- else
+ __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);
if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
return FALSE;
- if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
+ if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
__process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
- else
+ __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;
}
return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
}
- __remove_restriction(key, lookup_value, NULL);
-
/* 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
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;
rstn_value->rstn_state = value->rstn_state;
rstn_value->rstn_type = value->rstn_type;
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;
+
+ 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
value.classid = STC_UNKNOWN_CLASSID;
- value.data_limit = info->data_limit;
- value.data_warn_limit = info->data_warn_limit;
+ 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;
goto out;
/* add restriction to system */
- if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
+ if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT) {
__process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
- else
+ __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:
FREE(app_key.app_id);
}
-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;
-}
-
-//LCOV_EXCL_START
-static int __vconf_set_int(const char *key, int value)
-{
- int ret = 0;
-
- ret = vconf_set_int(key, value);
- if (ret != VCONF_OK) {
- STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
- return -1; //LCOV_EXCL_LINE
- }
-
- return 0;
-}
-
static guint __get_background_state(void)
{
return g_system->background_state;;
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);
value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
__processes_tree_key_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 */
return ret;
}
+API stc_error_e stc_monitor_application_remove(const stc_app_key_s app_key)
+{
+ 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
+ }
+
+ /* remove nfacct rule for this classid */
+ __remove_application_monitor((gpointer) &app_key, app_lookup,
+ stc_get_default_connection());
+
+ /* remove ristrictions if any */
+ __remove_rstns_for_application(app_key.app_id);
+
+ /* 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");
+ }
+
+ return ret;
+}
+
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)
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;
__process_update_background(); //LCOV_EXCL_LINE
}
- value.data_limit = info->data_limit;
- value.data_warn_limit = info->data_warn_limit;
+ 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;
}