*/
#include <linux/netlink.h>
+#include <vconf.h>
+#include <vconf-keys.h>
#include "stc-default-connection.h"
#include "helper-nl.h"
#include "helper-nfacct-rule.h"
#include "helper-net-cls.h"
+#include "helper-cgroup.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;
typedef struct {
struct nfacct_rule *counter;
int64_t bytes;
- gboolean in_limit_reached;
- gboolean out_limit_reached;
+ gboolean data_limit_reached;
} classid_bytes_context_s;
static stc_system_s *g_system = NULL;
static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
{
- return produce_net_rule(counter, 0, 0,
- NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
- NFACCT_COUNTER_IN);
+ 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)
{
- return produce_net_rule(counter, 0, 0,
- NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
- NFACCT_COUNTER_OUT);
+ 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)
{
- return produce_net_rule(counter, 0, 0,
- NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
- NFACCT_COUNTER_IN);
+ 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)
{
- return produce_net_rule(counter, 0, 0,
- NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
- NFACCT_COUNTER_OUT);
+ 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,
if (ret != 0)
return ret;
- ret = g_strcmp0(key_a->imsi, key_b->imsi);
+ ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
if (ret != 0)
return ret;
FREE(key->app_id);
FREE(key->ifname);
- FREE(key->imsi);
+ FREE(key->subscriber_id);
FREE(key);
}
return FALSE;
}
+#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)
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();
}
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);
- __add_iptables_in(&counter);
- __add_iptables_out(&counter);
+ 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;
if (!stc->carg) {
stc->carg = MALLOC0(counter_arg_s, 1);
+ if (stc->carg == NULL)
+ return FALSE;
+
stc->carg->sock = stc_monitor_get_counter_socket();
}
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;
STC_LOGI("rstn info => rstn_id [%llu], "
"app_id [%s], classid [%lu], ifname [%s], "
"iftype [%d], rst_state [%d], "
- "limit [ in (%lld), out (%lld)], "
- "warn_limit [ in (%lld), out (%lld)], "
- "counter [ in (%lld), out (%lld)], "
- "roaming [%d], imsi [%s]",
+ "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->limit.in_bytes, rstn_value->limit.out_bytes,
- rstn_value->warn_limit.in_bytes,
- rstn_value->warn_limit.out_bytes,
- rstn_value->counter.in_bytes, rstn_value->counter.out_bytes,
- rstn_key->roaming, rstn_key->imsi);
+ 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)
{
- stc_data_counter_s effective_limit, effective_warn_limit;
+ 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_limit.out_bytes = rstn_value->limit.out_bytes;
- effective_limit.in_bytes = rstn_value->limit.in_bytes;
- effective_warn_limit.out_bytes = rstn_value->warn_limit.out_bytes;
- effective_warn_limit.in_bytes = rstn_value->warn_limit.in_bytes;
+ 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 */
memset(&info, 0, sizeof(table_counters_info));
table_counters_get(rstn_value->restriction_id, &info);
- effective_limit.out_bytes -= info.sent_bytes;
- effective_limit.in_bytes -= info.rcv_bytes;
- effective_warn_limit.out_bytes -= info.sent_bytes;
- effective_warn_limit.in_bytes -= info.rcv_bytes;
+ effective_data_limit -= info.data_counter;
+ effective_data_warn_limit -= info.data_counter;
- if (effective_limit.in_bytes < 0) {
- effective_limit.in_bytes = 0;
- rstn_value->in_limit_reached = TRUE;
+ if (effective_data_limit < 0) {
+ effective_data_limit = 0;
+ rstn_value->data_limit_reached = TRUE;
}
- if (effective_limit.out_bytes < 0) {
- effective_limit.out_bytes = 0;
- rstn_value->out_limit_reached = TRUE;
- }
+ if (effective_data_warn_limit < 0)
+ effective_data_warn_limit = 0;
- if (effective_warn_limit.in_bytes < 0)
- effective_warn_limit.in_bytes = 0;
-
- if (effective_warn_limit.out_bytes < 0)
- effective_warn_limit.out_bytes = 0;
- STC_LOGD("datausage [in: %lld, out: %lld]",
- info.rcv_bytes, info.sent_bytes);
+ STC_LOGD("datausage [%lld] bytes", info.data_counter);
}
- STC_LOGD("rstn_id [%llu], effective limit [in: %lld, out: %lld], "
- "effective warn limit [in: %lld, out: %lld], "
- "datausage [in: %lld, out: %lld]",
- rstn_value->restriction_id,
- effective_limit.in_bytes, effective_limit.out_bytes,
- effective_warn_limit.in_bytes, effective_warn_limit.out_bytes);
-
- send_net_restriction(rst_type,
- rstn_value->classid,
- 0 /*quota_id*/,
- connection->type,
- effective_limit.out_bytes,
- effective_limit.in_bytes,
- effective_warn_limit.out_bytes,
- effective_warn_limit.in_bytes,
- connection->ifname);
+ 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->in_limit_reached = FALSE;
- rstn_value->out_limit_reached = FALSE;
+ rstn_value->data_limit_reached = FALSE;
break;
case RST_EXCLUDE:
;//Do Nothing
break;
case RST_UNSET:
- rstn_value->rst_state = STC_RESTRICTION_REMOVED;
- rstn_value->in_limit_reached = FALSE;
- rstn_value->out_limit_reached = FALSE;
+ {
+ 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
ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
/* close netlink socket for updating kernel counters */
- if (g_system->contr_sock != -1) {
- close(g_system->contr_sock);
- g_system->contr_sock = -1;
+ if (system->contr_sock != -1) {
+ close(system->contr_sock);
+ system->contr_sock = -1;
}
- if (g_system->contr_gsource_id != 0) {
- g_source_remove(g_system->contr_gsource_id);
- g_system->contr_gsource_id = 0;
+ if (system->contr_gsource_id != 0) {
+ g_source_remove(system->contr_gsource_id);
+ system->contr_gsource_id = 0;
}
return STC_ERROR_NONE;
}
-static gboolean __rstn_counter_update_foreach_classid(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;
- classid_bytes_context_s *context = (classid_bytes_context_s *)data;
+static gboolean __process_contr_reply(GIOChannel *source,
+ GIOCondition condition,
+ gpointer user_data);
- if (context->counter->intend != NFACCT_COUNTER)
- goto try_next_callback;
+static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
+{
+ GIOChannel *gio = NULL;
+ ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
- if (rstn_value->classid != context->counter->classid)
- goto try_next_callback;
+ /* close netlink socket for updating kernel counters */
+ if (system->contr_sock != -1) {
+ close(system->contr_sock);
+ system->contr_sock = -1;
+ }
- if (rstn_value->in_limit_reached == TRUE) {
- context->in_limit_reached = TRUE;
- goto try_next_callback;
+ if (system->contr_gsource_id != 0) {
+ g_source_remove(system->contr_gsource_id);
+ system->contr_gsource_id = 0;
}
- if (rstn_value->out_limit_reached == TRUE) {
- context->out_limit_reached = TRUE;
- goto try_next_callback;
+ /* 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:
- rstn_value->counter.in_bytes += context->bytes;
-
- /* block immediately */
- if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) {
- __del_iptables_in(context->counter);
- __add_iptables_in(context->counter);
- rstn_value->in_limit_reached = TRUE;
- //context->in_limit_reached = TRUE;
+ 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);
}
- g_system->rstns_tree_updated = TRUE;
- __print_rstn(rstn_key, rstn_value);
- break;
- case NFACCT_COUNTER_OUT:
- rstn_value->counter.out_bytes += context->bytes;
+ if (rstn_value->data_counter >= rstn_value->data_limit &&
+ rstn_value->rstn_limit_crossed_notified == FALSE) {
- /* block immediately */
- if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) {
+ 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);
- rstn_value->out_limit_reached = TRUE;
- //context->out_limit_reached = TRUE;
+
+ __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;
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);
+ }
-try_next_callback:
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)
{
time_t *touch_time = (time_t *)data;
stc_db_classid_iftype_key stat_key;
stc_db_app_stats stat;
- char *default_ifname = stc_default_connection_get_ifname();
+ 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 = stc_default_connection_get_type();
+ stat_key.iftype = default_connection->type;
+
if (STC_IFACE_DATACALL == stat_key.iftype)
- stat_key.imsi = g_strdup("unknown");
+ stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
else
- stat_key.imsi = g_strdup("noneimsi");
- g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH);
+ 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.delta_snd = 0;
- stat.delta_rcv = 0;
- stat.is_roaming = stc_default_connection_get_roaming();
+ stat.is_roaming = default_connection->roaming;
stat.ground = STC_APP_STATE_UNKNOWN;
table_statistics_insert(&stat_key, &stat, *touch_time);
app_value->counter.in_bytes = 0;
FREE(stat.app_id);
- FREE(stat_key.imsi);
- FREE(default_ifname);
+ FREE(stat_key.subscriber_id);
return FALSE;
}
stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
table_counters_info info = {
.restriction_id = rstn_value->restriction_id,
- .sent_bytes = rstn_value->counter.out_bytes,
- .rcv_bytes = rstn_value->counter.in_bytes
+ .data_counter = rstn_value->data_counter
};
table_counters_update_counters(&info);
return G_SOURCE_REMOVE;
}
-static gboolean __apps_counter_update_foreach_classid(gpointer key,
- gpointer value,
- gpointer data)
+static void __app_counter_update(stc_app_key_s *app_key,
+ stc_app_value_s *app_value,
+ classid_bytes_context_s *context)
{
- 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;
-
- if (app_value->classid != context->counter->classid)
- goto try_next_callback;
-
switch (context->counter->iotype) {
case NFACCT_COUNTER_IN:
app_value->data_usage.in_bytes += context->bytes;
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;
classid_bytes_context_s context = {
.counter = &counter,
.bytes = bytes,
- .in_limit_reached = FALSE,
- .out_limit_reached = FALSE
+ .data_limit_reached = FALSE,
};
STC_LOGD("cnt_name %s", cnt_name);
(condition & G_IO_NVAL)) {
/* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
- __close_contr_sock(g_system);
+ 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;
}
ret = read_netlink(sock,
&ans, sizeof(struct genl));
- STC_LOGD("Counter data received ret [%d]", ret);
+ /* STC_LOGD("Counter data received ret [%d]", ret); */
if (ret == 0)
goto out;
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");
+ /* STC_LOGD("Get all counters"); */
nfacct_send_get_all(stc->carg);
/* we need to continue the timer */
rstn_key->app_id = g_strdup(key->app_id);
rstn_key->ifname = g_strdup(key->ifname);
- rstn_key->imsi = g_strdup(key->imsi);
+ rstn_key->subscriber_id = g_strdup(key->subscriber_id);
rstn_key->iftype = key->iftype;
rstn_key->roaming = key->roaming;
rstn_value->restriction_id = value->restriction_id;
rstn_value->rst_state = value->rst_state;
rstn_value->classid = value->classid;
- rstn_value->limit.in_bytes = value->limit.in_bytes;
- rstn_value->limit.out_bytes = value->limit.out_bytes;
- rstn_value->warn_limit.in_bytes = value->warn_limit.in_bytes;
- rstn_value->warn_limit.out_bytes = value->warn_limit.out_bytes;
- rstn_value->counter.in_bytes = 0;
- rstn_value->counter.out_bytes = 0;
+ 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;
if (debug == TRUE)
__add_restriction_debug(key, rstn_value, NULL);
key.app_id = g_strdup(info->app_id);
key.ifname = g_strdup(info->ifname);
- key.imsi = g_strdup(info->imsi);
+ 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)
- value.classid = get_classid_by_app_id(info->app_id ?
- info->app_id :
- STC_ALL_APP, TRUE);
+ 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.limit.in_bytes = info->rcv_limit;
- value.limit.out_bytes = info->send_limit;
- value.warn_limit.in_bytes = info->rcv_warn_limit;
- value.warn_limit.out_bytes = info->send_warn_limit;
+ 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.imsi);
+ FREE(key.subscriber_id);
return ret;
}
app_id);
}
-/* used only for (STC_ALL_APP/STC_BACKGROUND_APP_NAME) */
-static void __stc_monitor_add_application_by_app_id(const char *app_id)
+static void __add_application_by_interface(const char *app_id)
{
stc_app_key_s app_key;
stc_app_value_s app_value;
FREE(app_key.pkg_id);
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);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int __vconf_set_int(const char *key, int value)
+{
+ int ret = 0;
+
+ ret = vconf_set_int(key, value);
+ if (ret != VCONF_OK) {
+ STC_LOGE("Failed to set vconfkey [%s] value", key);
+ return -1;
+ }
+
+ return 0;
+}
+
+static 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)
+{
+ stc_process_key_s *proc_key = (stc_process_key_s *)key;
+ stc_app_key_s *app_key = (stc_app_key_s *)data;
+
+ 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);
+
+ return FALSE;
+}
+
+static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
+ gpointer data)
+{
+ stc_app_key_s *app_key = (stc_app_key_s *)key;
+ stc_app_value_s *app_value = (stc_app_value_s *)value;
+
+ if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
+ g_tree_foreach(app_value->processes,
+ __processes_tree_foreach_background, app_key);
+
+ return FALSE;
+}
+
+static stc_error_e __process_update_background(void)
+{
+ ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
- __apps_tree_printall();
+ g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
+
+ return STC_ERROR_NONE;
+}
+
+#if 0
+static void __excn_hash_foreach_print(gpointer key, gpointer value,
+ gpointer data)
+{
+ const char *process_name = key;
+ const char *exe_type = value;
+
+ STC_LOGI("excn info => process_name [%s] exe_type [%s]",
+ process_name, exe_type);
+}
+
+static void __excn_hash_printall(void)
+{
+ g_hash_table_foreach(g_system->excns_hash,
+ __excn_hash_foreach_print, NULL);
+}
+#endif
+
+static gboolean __remove_exception_app(gpointer key, gpointer value,
+ gpointer data)
+{
+ const char *exe_type = value;
+
+ if (g_strcmp0(exe_type, EXE_TYPE_APPLICATION) == 0)
+ return TRUE;
+
+ return FALSE;
+}
+
+static void __remove_exception_appall(void)
+{
+ g_hash_table_foreach_remove(g_system->excns_hash,
+ __remove_exception_app, NULL);
+}
+
+static stc_cb_ret_e __insert_exception_cb(const stc_exceptions_info *info,
+ void *user_data)
+{
+ stc_cb_ret_e ret = STC_CONTINUE;
+
+ if (g_hash_table_insert(g_system->excns_hash,
+ g_strdup(info->process_name),
+ g_strdup(info->exe_type)) != TRUE)
+ ret = STC_CANCEL;
+
+ return ret;
+}
+
+static void __fill_exceptions_list(void)
+{
+ table_exceptions_foreach(__insert_exception_cb, NULL);
+ pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
+
+ /* __excn_hash_printall(); */
+}
+
+static gboolean __update_exceptions_app_list(void *user_data)
+{
+ __remove_exception_appall();
+ pkginfo_exceptions_foreach(__insert_exception_cb, NULL);
+
+ /* __excn_hash_printall(); */
+
+ return TRUE;
}
stc_error_e stc_monitor_init(void)
ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
+ /* initializing current classid */
+ init_current_classid();
+
+ /* initializing cgroups */
+ cgroup_init();
+
/* creating monitored application tree */
system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
__apps_tree_key_free,
__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);
+
/* create netlink socket for updating kernel counters */
system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
if (!(system->contr_sock)) {
g_system = system;
- /* create entry for STC_ALL_APP */
- __stc_monitor_add_application_by_app_id(STC_ALL_APP);
-
- /* create entry for STC_BACKGROUND_APP_NAME */
- __stc_monitor_add_application_by_app_id(STC_BACKGROUND_APP_NAME);
-
- /* create background cgroup */
- g_system->background_pids = NULL;
- create_net_background_cgroup(g_system->background_pids);
+ __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);
/* creating restriction rules tree */
__update_contr_cb(NULL);
return STC_ERROR_FAIL;
}
+ __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
+ (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);
+
return STC_ERROR_NONE;
}
g_system->contr_timer_id = 0;
}
- /* free background pid list */
- g_slist_free(g_system->background_pids);
- g_system->background_pids = NULL;
+ /* 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_tree_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;
key->pkg_id = g_strdup(app_key.pkg_id);
value->type = app_value.type;
- value->counter.in_bytes = app_value.counter.in_bytes;
- value->counter.out_bytes = app_value.counter.out_bytes;
+ value->data_usage.in_bytes = app_value.data_usage.in_bytes;
+ value->data_usage.out_bytes = app_value.data_usage.out_bytes;
value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
__processes_tree_key_free,
/* add pid to application cgroup */
place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
- /* add pid to background cgroup if gound state is background */
- if (proc_value.ground == STC_APP_STATE_BACKGROUND)
- add_pid_to_background_cgroup(proc_key.pid);
-
return ret;
}
ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
- /* remove pid from background cgroup */
- remove_pid_from_background_cgroup(pid);
-
g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
&context);
if (proc_lookup->ground != ground)
proc_lookup->ground = ground;
- if (ground == STC_APP_STATE_BACKGROUND)
- add_pid_to_background_cgroup(proc_key.pid);
+ 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);
key.app_id = g_strdup(info->app_id);
key.ifname = g_strdup(info->ifname);
- key.imsi = g_strdup(info->imsi);
+ 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)
- value.classid = get_classid_by_app_id(info->app_id ?
- info->app_id :
- STC_ALL_APP, TRUE);
+ 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.limit.in_bytes = info->rcv_limit;
- value.limit.out_bytes = info->send_limit;
- value.warn_limit.in_bytes = info->rcv_warn_limit;
- value.warn_limit.out_bytes = info->send_warn_limit;
+ 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.imsi);
+ FREE(key.subscriber_id);
return ret;
}
stc_rstn_key_s key = {
.app_id = g_strdup(info->app_id),
.ifname = g_strdup(info->ifname),
- .imsi = g_strdup(info->imsi),
+ .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.imsi);
+ 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)
{
return g_system->contr_sock;