From 125ff4a6c69597383ed28f697c859fdd6c4a5ab4 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Thu, 15 Jun 2017 09:40:04 +0530 Subject: [PATCH] [STC Manager] Support set/get for total data usage(Tx + Rx) in restriction. Description: This patch removes seperate data usage limits for Tx and Rx data and uses total data limit for setting Tx and Rx limits. Change-Id: I6882e936c9cb076dda548ef95a10e105071df767 Signed-off-by: Nishant Chaprana --- data/traffic_db.sql | 9 +- packaging/stc-manager.spec | 2 +- src/database/include/stc-db.h | 6 +- src/database/include/table-counters.h | 3 +- src/database/include/table-restrictions.h | 6 +- src/database/tables/table-counters.c | 25 ++-- src/database/tables/table-restrictions.c | 109 +++++++-------- src/monitor/include/stc-monitor.h | 9 +- src/monitor/stc-monitor.c | 215 +++++++++--------------------- src/stc-restriction.c | 40 ++---- 10 files changed, 153 insertions(+), 271 deletions(-) diff --git a/data/traffic_db.sql b/data/traffic_db.sql index 045586d..fda62f8 100644 --- a/data/traffic_db.sql +++ b/data/traffic_db.sql @@ -23,18 +23,15 @@ CREATE TABLE IF NOT EXISTS restrictions ( rst_state INT, roaming INT, imsi TEXT NOT NULL, - rcv_limit BIGINT, - send_limit BIGINT, - rcv_warn_limit BIGINT, - send_warn_limit BIGINT + data_limit BIGINT, + data_warn_limit BIGINT ); CREATE INDEX IF NOT EXISTS restrictions_index ON restrictions (binpath, iftype, ifname); CREATE TABLE IF NOT EXISTS counters ( restriction_id INTEGER NOT NULL, - sent_bytes BIGINT, - rcv_bytes BIGINT, + data_counter BIGINT, PRIMARY KEY (restriction_id) ); diff --git a/packaging/stc-manager.spec b/packaging/stc-manager.spec index 6a00c37..3afe575 100644 --- a/packaging/stc-manager.spec +++ b/packaging/stc-manager.spec @@ -1,6 +1,6 @@ Name: stc-manager Summary: STC(Smart Traffic Control) manager -Version: 0.0.14 +Version: 0.0.15 Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 diff --git a/src/database/include/stc-db.h b/src/database/include/stc-db.h index a240994..6f718c3 100755 --- a/src/database/include/stc-db.h +++ b/src/database/include/stc-db.h @@ -57,10 +57,8 @@ typedef enum { */ typedef struct { char *app_id; - unsigned int snd_count; - unsigned int rcv_count; - unsigned int delta_snd; - unsigned int delta_rcv; + int64_t rcv_count; + int64_t snd_count; #ifndef CONFIG_DATAUSAGE_NFACCT pid_t pid; diff --git a/src/database/include/table-counters.h b/src/database/include/table-counters.h index 70e545c..a0ef60d 100755 --- a/src/database/include/table-counters.h +++ b/src/database/include/table-counters.h @@ -19,8 +19,7 @@ typedef struct { uint64_t restriction_id; - int64_t sent_bytes; - int64_t rcv_bytes; + int64_t data_counter; } table_counters_info; typedef stc_cb_ret_e(*table_counters_info_cb)(const table_counters_info *info, diff --git a/src/database/include/table-restrictions.h b/src/database/include/table-restrictions.h index 2814199..fe275e1 100755 --- a/src/database/include/table-restrictions.h +++ b/src/database/include/table-restrictions.h @@ -24,10 +24,8 @@ typedef struct { stc_iface_type_e iftype; stc_restriction_state_e rst_state; stc_roaming_type_e roaming; - int64_t rcv_limit; - int64_t send_limit; - int64_t rcv_warn_limit; - int64_t send_warn_limit; + int64_t data_limit; + int64_t data_warn_limit; uint64_t restriction_id; } table_restrictions_info; diff --git a/src/database/tables/table-counters.c b/src/database/tables/table-counters.c index 9fb6fba..f8946f7 100755 --- a/src/database/tables/table-counters.c +++ b/src/database/tables/table-counters.c @@ -26,13 +26,13 @@ #define DELETE_COUNTER "DELETE FROM counters WHERE restriction_id=?" -#define SELECT_COUNTER "SELECT sent_bytes, rcv_bytes FROM counters WHERE restriction_id=?" +#define SELECT_COUNTER "SELECT data_counter FROM counters WHERE restriction_id=?" -#define INSERT_COUNTER "INSERT INTO counters (restriction_id, sent_bytes, rcv_bytes) " \ - " VALUES (?, ?, ?)" +#define INSERT_COUNTER "INSERT INTO counters (restriction_id, data_counter) " \ + " VALUES (?, ?)" -#define UPDATE_COUNTER "REPLACE INTO counters (restriction_id, sent_bytes, rcv_bytes) " \ - " VALUES (?, ?, ?)" +#define UPDATE_COUNTER "REPLACE INTO counters (restriction_id, data_counter) " \ + " VALUES (?, ?)" static void __finalize_delete(void); @@ -175,6 +175,8 @@ stc_error_e table_counters_get(uint64_t restriction_id, if (info == NULL) goto handle_error; + info->restriction_id = restriction_id; + DB_ACTION(sqlite3_bind_int64(stmt, 1, restriction_id)); do { @@ -184,12 +186,10 @@ stc_error_e table_counters_get(uint64_t restriction_id, case SQLITE_DONE: break; case SQLITE_ROW: - info->sent_bytes = sqlite3_column_int64(stmt, 0); - info->rcv_bytes = sqlite3_column_int64(stmt, 1); + info->data_counter = sqlite3_column_int64(stmt, 0); - STC_LOGD("rstn_id [%llu] counters [sent = %lld rcv = %lld]", - restriction_id, info->sent_bytes, - info->rcv_bytes); + STC_LOGD("rstn_id [%llu] data_counters [%lld] bytes", + restriction_id, info->data_counter); break; case SQLITE_ERROR: default: @@ -210,14 +210,13 @@ stc_error_e table_counters_update_counters(const table_counters_info *info) stc_error_e error_code = STC_ERROR_NONE; sqlite3_stmt *stmt = update_counter; - if (!info->rcv_bytes && !info->sent_bytes) { + if (!info->data_counter) { error_code = STC_ERROR_INVALID_PARAMETER; goto handle_error; } DB_ACTION(sqlite3_bind_int64(stmt, 1, info->restriction_id)); - DB_ACTION(sqlite3_bind_int64(stmt, 2, info->sent_bytes)); - DB_ACTION(sqlite3_bind_int64(stmt, 3, info->rcv_bytes)); + DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_counter)); if (sqlite3_step(stmt) != SQLITE_DONE) { STC_LOGE("Failed to update counter: %s\n", diff --git a/src/database/tables/table-restrictions.c b/src/database/tables/table-restrictions.c index aec6f08..a5cb108 100755 --- a/src/database/tables/table-restrictions.c +++ b/src/database/tables/table-restrictions.c @@ -26,43 +26,43 @@ /* DELETE statements */ #define DELETE_RESTRICTIONS "DELETE FROM restrictions " \ - "WHERE binpath=? AND iftype=? AND imsi = ?" + " WHERE binpath = ? AND iftype = ? AND imsi = ?" /* SELECT statements */ -#define SELECT_RESTRICTIONS "SELECT binpath, rcv_limit, " \ - "send_limit, iftype, rst_state, roaming, ifname, imsi, " \ - "rcv_warn_limit, send_warn_limit, restriction_id FROM restrictions" +#define SELECT_RESTRICTIONS "SELECT binpath, data_limit, " \ + " iftype, rst_state, roaming, ifname, imsi, " \ + " data_warn_limit, restriction_id FROM restrictions" -#define SELECT_RESTRICTIONS_PER_APP "SELECT binpath, rcv_limit, " \ - "send_limit, iftype, rst_state, roaming, ifname, imsi, " \ - "rcv_warn_limit, send_warn_limit, restriction_id " \ - "FROM restrictions INDEXED BY restrictions_index " \ - "WHERE binpath = ?" +#define SELECT_RESTRICTIONS_PER_APP "SELECT binpath, data_limit, " \ + " iftype, rst_state, roaming, ifname, imsi, " \ + " data_warn_limit, restriction_id " \ + " FROM restrictions INDEXED BY restrictions_index " \ + " WHERE binpath = ?" #define SELECT_RESTRICTION_STATE "SELECT rst_state " \ - "FROM restrictions INDEXED BY restrictions_index " \ - "WHERE binpath = ? AND iftype = ?" + " FROM restrictions INDEXED BY restrictions_index " \ + " WHERE binpath = ? AND iftype = ?" #define SELECT_RESTRICTION_STATE_IMSI "SELECT rst_state " \ - "FROM restrictions INDEXED BY restrictions_index " \ - "WHERE binpath = ? AND iftype = ? AND imsi = ?" + " FROM restrictions INDEXED BY restrictions_index " \ + " WHERE binpath = ? AND iftype = ? AND imsi = ?" #define SELECT_RESTRICTION_ID "SELECT restriction_id FROM restrictions " \ - "WHERE binpath = ? AND iftype = ? AND imsi = ? AND send_limit=? " \ - "AND rcv_limit=? AND rcv_warn_limit=? AND send_warn_limit=? " \ - "AND rst_state=? AND roaming=? AND ifname=?" + " WHERE binpath = ? AND iftype = ? AND imsi = ? AND " \ + " data_limit = ? AND data_warn_limit = ? AND " \ + " rst_state = ? AND roaming = ? AND ifname = ?" /* UPDATE statement */ #define UPDATE_NET_RESTRICTIONS "UPDATE restrictions " \ - "SET binpath=?, rcv_limit=?, send_limit=?, iftype=?, rst_state=?, " \ - "roaming=?, ifname=?, imsi=?, rcv_warn_limit=?, send_warn_limit=? " \ - "WHERE restriction_id=?" + " SET binpath = ?, data_limit = ?, iftype = ?, rst_state = ?, " \ + " roaming = ?, ifname = ?, imsi = ?, data_warn_limit = ? " \ + " WHERE restriction_id = ?" /* INSERT statement */ #define INSERT_NET_RESTRICTIONS "INSERT INTO restrictions " \ - "(binpath, rcv_limit, send_limit, iftype, rst_state, " \ - "roaming, ifname, imsi, rcv_warn_limit, send_warn_limit) " \ - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" + " (binpath, data_limit, iftype, rst_state, " \ + " roaming, ifname, imsi, data_warn_limit) " \ + " VALUES (?, ?, ?, ?, ?, ?, ?, ?)" static void __finalize_delete(void); @@ -284,17 +284,15 @@ stc_error_e table_restrictions_per_app(const gchar* app_id, break; case SQLITE_ROW: data.app_id = (char *)sqlite3_column_text(stmt, 0); - data.rcv_limit = sqlite3_column_int64(stmt, 1); - data.send_limit = sqlite3_column_int64(stmt, 2); - data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 3); + data.data_limit = sqlite3_column_int64(stmt, 1); + data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2); data.rst_state = - (stc_restriction_state_e)sqlite3_column_int(stmt, 4); - data.roaming = sqlite3_column_int(stmt, 5); - data.ifname = (char *)sqlite3_column_text(stmt, 6); - data.imsi = (char *)sqlite3_column_text(stmt, 7); - data.rcv_warn_limit = sqlite3_column_int64(stmt, 8); - data.send_warn_limit = sqlite3_column_int64(stmt, 9); - data.restriction_id = sqlite3_column_int64(stmt, 10); + (stc_restriction_state_e)sqlite3_column_int(stmt, 3); + data.roaming = sqlite3_column_int(stmt, 4); + data.ifname = (char *)sqlite3_column_text(stmt, 5); + data.imsi = (char *)sqlite3_column_text(stmt, 6); + data.data_warn_limit = sqlite3_column_int64(stmt, 7); + data.restriction_id = sqlite3_column_int64(stmt, 8); if (restriction_cb(&data, user_data) == STC_CANCEL) rc = SQLITE_DONE; @@ -334,17 +332,15 @@ stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restrict break; case SQLITE_ROW: data.app_id = (char *)sqlite3_column_text(stmt, 0); - data.rcv_limit = sqlite3_column_int64(stmt, 1); - data.send_limit = sqlite3_column_int64(stmt, 2); - data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 3); + data.data_limit = sqlite3_column_int64(stmt, 1); + data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2); data.rst_state = - (stc_restriction_state_e)sqlite3_column_int(stmt, 4); - data.roaming = sqlite3_column_int(stmt, 5); - data.ifname = (char *)sqlite3_column_text(stmt, 6); - data.imsi = (char *)sqlite3_column_text(stmt, 7); - data.rcv_warn_limit = sqlite3_column_int64(stmt, 8); - data.send_warn_limit = sqlite3_column_int64(stmt, 9); - data.restriction_id = sqlite3_column_int64(stmt, 10); + (stc_restriction_state_e)sqlite3_column_int(stmt, 3); + data.roaming = sqlite3_column_int(stmt, 4); + data.ifname = (char *)sqlite3_column_text(stmt, 5); + data.imsi = (char *)sqlite3_column_text(stmt, 6); + data.data_warn_limit = sqlite3_column_int64(stmt, 7); + data.restriction_id = sqlite3_column_int64(stmt, 8); if (restriction_cb(&data, user_data) == STC_CANCEL) rc = SQLITE_DONE; @@ -480,14 +476,11 @@ stc_error_e __get_restriction_id(table_restrictions_info *info) DB_ACTION(sqlite3_bind_int(stmt, 2, info->iftype)); DB_ACTION(sqlite3_bind_text(stmt, 3, info->imsi ? info->imsi : "", -1, SQLITE_TRANSIENT)); - DB_ACTION(sqlite3_bind_int64(stmt, 4, info->send_limit)); - DB_ACTION(sqlite3_bind_int64(stmt, 5, info->rcv_limit)); - DB_ACTION(sqlite3_bind_int64(stmt, 6, info->rcv_warn_limit)); - DB_ACTION(sqlite3_bind_int64(stmt, 7, info->send_warn_limit)); - - DB_ACTION(sqlite3_bind_int(stmt, 8, info->rst_state)); - DB_ACTION(sqlite3_bind_int(stmt, 9, info->roaming)); - DB_ACTION(sqlite3_bind_text(stmt, 10, info->ifname ? info->ifname : "", + DB_ACTION(sqlite3_bind_int64(stmt, 4, info->data_limit)); + DB_ACTION(sqlite3_bind_int64(stmt, 5, info->data_warn_limit)); + DB_ACTION(sqlite3_bind_int(stmt, 6, info->rst_state)); + DB_ACTION(sqlite3_bind_int(stmt, 7, info->roaming)); + DB_ACTION(sqlite3_bind_text(stmt, 8, info->ifname ? info->ifname : "", -1, SQLITE_TRANSIENT)); rc = sqlite3_step(stmt); @@ -526,17 +519,15 @@ stc_error_e table_restrictions_update(table_restrictions_info *info) DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "", -1, SQLITE_TRANSIENT)); - DB_ACTION(sqlite3_bind_int64(stmt, 2, info->rcv_limit)); - DB_ACTION(sqlite3_bind_int64(stmt, 3, info->send_limit)); - DB_ACTION(sqlite3_bind_int(stmt, 4, info->iftype)); - DB_ACTION(sqlite3_bind_int(stmt, 5, info->rst_state)); - DB_ACTION(sqlite3_bind_int(stmt, 6, info->roaming)); - DB_ACTION(sqlite3_bind_text(stmt, 7, info->ifname ? info->ifname : "", + DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_limit)); + DB_ACTION(sqlite3_bind_int(stmt, 3, info->iftype)); + DB_ACTION(sqlite3_bind_int(stmt, 4, info->rst_state)); + DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming)); + DB_ACTION(sqlite3_bind_text(stmt, 6, info->ifname ? info->ifname : "", -1, SQLITE_TRANSIENT)); - DB_ACTION(sqlite3_bind_text(stmt, 8, info->imsi ? info->imsi : "", + DB_ACTION(sqlite3_bind_text(stmt, 7, info->imsi ? info->imsi : "", -1, SQLITE_TRANSIENT)); - DB_ACTION(sqlite3_bind_int64(stmt, 9, info->rcv_warn_limit)); - DB_ACTION(sqlite3_bind_int64(stmt, 10, info->send_warn_limit)); + DB_ACTION(sqlite3_bind_int64(stmt, 8, info->data_warn_limit)); if (info->restriction_id) DB_ACTION(sqlite3_bind_int64(stmt, 11, info->restriction_id)); diff --git a/src/monitor/include/stc-monitor.h b/src/monitor/include/stc-monitor.h index 07b4715..09871d5 100644 --- a/src/monitor/include/stc-monitor.h +++ b/src/monitor/include/stc-monitor.h @@ -80,11 +80,10 @@ typedef struct { uint64_t restriction_id; uint32_t classid; stc_restriction_state_e rst_state; - stc_data_counter_s limit; - stc_data_counter_s warn_limit; - stc_data_counter_s counter; - gboolean in_limit_reached; - gboolean out_limit_reached; + int64_t data_limit; + int64_t data_warn_limit; + int64_t data_counter; + gboolean data_limit_reached; gboolean warn_limit_crossed_notified; gboolean rstn_limit_crossed_notified; } stc_rstn_value_s; diff --git a/src/monitor/stc-monitor.c b/src/monitor/stc-monitor.c index 2674ae5..896e437 100755 --- a/src/monitor/stc-monitor.c +++ b/src/monitor/stc-monitor.c @@ -38,8 +38,7 @@ typedef struct { 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; @@ -333,17 +332,16 @@ 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 [ in (%lld), out (%lld)], " - "warn_limit [ in (%lld), out (%lld)], " - "counter [ in (%lld), out (%lld)], " + "limit [ (%lld) bytes], " + "warn_limit [ (%lld) bytes], " + "counter [ (%lld) bytes], " "roaming [%d], imsi [%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_value->data_limit, + rstn_value->data_warn_limit, + rstn_value->data_counter, rstn_key->roaming, rstn_key->imsi); } @@ -351,7 +349,7 @@ 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; @@ -373,10 +371,8 @@ static void __process_restriction(enum traffic_restriction_type rst_type, 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 */ @@ -385,50 +381,36 @@ static void __process_restriction(enum traffic_restriction_type rst_type, 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_warn_limit.in_bytes < 0) - effective_warn_limit.in_bytes = 0; + if (effective_data_warn_limit < 0) + effective_data_warn_limit = 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); + 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: 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; + rstn_value->data_limit_reached = FALSE; break; default: ;//Do Nothing @@ -533,28 +515,26 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, if (rstn_value->classid != context->counter->classid) goto try_next_callback; - if (rstn_value->in_limit_reached == TRUE) { - context->in_limit_reached = TRUE; - goto try_next_callback; - } - - if (rstn_value->out_limit_reached == TRUE) { - context->out_limit_reached = TRUE; + if (rstn_value->data_limit_reached == TRUE) { + context->data_limit_reached = TRUE; goto try_next_callback; } switch (context->counter->iotype) { case NFACCT_COUNTER_IN: - rstn_value->counter.in_bytes += context->bytes; + 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) { - if (rstn_value->counter.in_bytes >= rstn_value->warn_limit.in_bytes - && rstn_value->warn_limit_crossed_notified == FALSE) { gboolean rv = FALSE; 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, @@ -564,98 +544,46 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key, rstn_value->warn_limit_crossed_notified = TRUE; snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype); - snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.in_bytes); + 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); - } - - /* block immediately */ - if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) { - context->counter->intend = NFACCT_BLOCK; - __del_iptables_in(context->counter); - __add_iptables_in(context->counter); - context->counter->intend = NFACCT_COUNTER; - rstn_value->in_limit_reached = TRUE; - - if (rstn_value->rstn_limit_crossed_notified == FALSE) { - gboolean rv = FALSE; - int ret = 0; - 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"); - - rv = stc_manager_dbus_emit_signal(stc->connection, - STC_DBUS_SERVICE_RESTRICTION_PATH, - STC_DBUS_INTERFACE_RESTRICTION, - "RestrictionThresholdCrossed", - g_variant_new("(s)", rstn_key->app_id)); - 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->limit.in_bytes); - stc_send_restriction_message_to_net_popup("restriction threshold crossed", - "restriction_noti", rstn_key->app_id, iftype, byte); - } + "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) { - if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes - && rstn_value->warn_limit_crossed_notified == FALSE) { gboolean rv = FALSE; 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); + 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, - "WarnThresholdCrossed", + "RestrictionThresholdCrossed", g_variant_new("(s)", rstn_key->app_id)); if (rv == TRUE) - rstn_value->warn_limit_crossed_notified = 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->limit.out_bytes); - stc_send_warn_message_to_net_popup("warn threshold crossed", - "warning_noti", rstn_key->app_id, iftype, byte); - } - - /* block immediately */ - if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) { - context->counter->intend = NFACCT_BLOCK; - __del_iptables_out(context->counter); - __add_iptables_out(context->counter); - context->counter->intend = NFACCT_COUNTER; - rstn_value->out_limit_reached = TRUE; - - if (rstn_value->rstn_limit_crossed_notified == FALSE) { - gboolean rv = FALSE; - 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"); - - rv = stc_manager_dbus_emit_signal(stc->connection, - STC_DBUS_SERVICE_RESTRICTION_PATH, - STC_DBUS_INTERFACE_RESTRICTION, - "RestrictionThresholdCrossed", - g_variant_new("(s)", rstn_key->app_id)); - 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->limit.out_bytes); - stc_send_restriction_message_to_net_popup("restriction threshold crossed", - "restriction_noti", rstn_key->app_id, iftype, byte); - } + 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; @@ -694,8 +622,6 @@ static gboolean __update_app_statistics(gpointer key, gpointer value, 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.ground = STC_APP_STATE_UNKNOWN; @@ -735,8 +661,7 @@ static gboolean __update_counter_statistics(gpointer key, gpointer value, 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); @@ -812,8 +737,7 @@ static void __fill_nfacct_result(char *cnt_name, int64_t bytes, 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); @@ -1087,12 +1011,9 @@ static stc_error_e __rstn_tree_add(stc_rstn_key_s *key, 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; @@ -1129,10 +1050,8 @@ static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info, 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; @@ -1299,8 +1218,8 @@ stc_error_e stc_monitor_application_add(const stc_app_key_s app_key, 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, @@ -1492,10 +1411,8 @@ stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info) 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; ret = __rstn_tree_add(&key, &value, TRUE); diff --git a/src/stc-restriction.c b/src/stc-restriction.c index ba4e9ea..aa8ed4e 100755 --- a/src/stc-restriction.c +++ b/src/stc-restriction.c @@ -44,10 +44,8 @@ void __initialize_rstn_rule(table_restrictions_info *rule) rule->ifname = NULL; rule->iftype = STC_IFACE_ALL; rule->rst_state = STC_RESTRICTION_REMOVED; - rule->rcv_limit = 0; - rule->send_limit = 0; - rule->rcv_warn_limit = 0; - rule->send_warn_limit = 0; + rule->data_limit = 0; + rule->data_warn_limit = 0; rule->roaming = STC_ROAMING_DISABLE; rule->imsi = NULL; } @@ -114,17 +112,11 @@ void __stc_restriction_app_info_builder_add(GVariantBuilder *builder, g_variant_builder_add(builder, "{sv}", "rst_state", g_variant_new_uint16(info->rst_state)); - g_variant_builder_add(builder, "{sv}", "rcv_limit", - g_variant_new_int64(info->rcv_limit)); + g_variant_builder_add(builder, "{sv}", "data_limit", + g_variant_new_int64(info->data_limit)); - g_variant_builder_add(builder, "{sv}", "send_limit", - g_variant_new_int64(info->send_limit)); - - g_variant_builder_add(builder, "{sv}", "rcv_warn_limit", - g_variant_new_int64(info->rcv_warn_limit)); - - g_variant_builder_add(builder, "{sv}", "send_warn_limit", - g_variant_new_int64(info->send_warn_limit)); + g_variant_builder_add(builder, "{sv}", "data_warn_limit", + g_variant_new_int64(info->data_warn_limit)); g_variant_builder_add(builder, "{sv}", "roaming", g_variant_new_uint16(info->roaming)); @@ -202,21 +194,13 @@ static void __stc_extract_restriction_rule(const char *key, GVariant *value, rule->iftype = g_variant_get_uint16(value); STC_LOGD("iftype: [%u]", (unsigned int) rule->iftype); - } else if (!g_strcmp0(key, "rcv_limit")) { - rule->rcv_limit = g_variant_get_int64(value); - STC_LOGD("rcv_limit: [%lld]", rule->rcv_limit); - - } else if (!g_strcmp0(key, "send_limit")) { - rule->send_limit = g_variant_get_int64(value); - STC_LOGD("send_limit: [%lld]", rule->send_limit); - - } else if (!g_strcmp0(key, "rcv_warn_limit")) { - rule->rcv_warn_limit = g_variant_get_int64(value); - STC_LOGD("rcv_warn_limit: [%lld]", rule->rcv_warn_limit); + } else if (!g_strcmp0(key, "data_limit")) { + rule->data_limit = g_variant_get_int64(value); + STC_LOGD("data_limit: [%lld]", rule->data_limit); - } else if (!g_strcmp0(key, "send_warn_limit")) { - rule->send_warn_limit = g_variant_get_int64(value); - STC_LOGD("send_warn_limit: [%lld]", rule->send_warn_limit); + } else if (!g_strcmp0(key, "data_warn_limit")) { + rule->data_warn_limit = g_variant_get_int64(value); + STC_LOGD("data_warn_limit: [%lld]", rule->data_warn_limit); } else if (!g_strcmp0(key, "roaming")) { rule->roaming = g_variant_get_uint16(value); -- 2.7.4