2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <linux/netlink.h>
19 #include <vconf-keys.h>
21 #include "stc-default-connection.h"
22 #include "helper-nl.h"
23 #include "helper-nfacct-rule.h"
24 #include "helper-net-cls.h"
25 #include "helper-cgroup.h"
26 #include "helper-iptables.h"
28 #include "table-statistics.h"
29 #include "table-counters.h"
30 #include "stc-monitor.h"
32 #include "stc-manager-plugin-appstatus.h"
33 #include "stc-manager-plugin-exception.h"
35 #define GRANULARITY 10
36 #define MAX_INT_LENGTH 128
37 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
41 time_t month_start_ts;
45 } reset_time_limits_context_s;
48 stc_app_key_s *app_key;
49 stc_process_key_s *proc_key;
50 gboolean entry_removed;
51 } remove_pid_context_s;
54 struct nfacct_rule *counter;
56 gboolean data_limit_exceeded;
57 } classid_bytes_context_s;
59 static stc_system_s *g_system = NULL;
61 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
63 if (counter->intend == NFACCT_WARN)
64 return NFACCT_JUMP_ACCEPT;
65 else if (counter->intend == NFACCT_BLOCK)
66 return NFACCT_JUMP_REJECT;
67 else if (counter->intend == NFACCT_ALLOW)
68 return NFACCT_JUMP_ACCEPT;
70 return NFACCT_JUMP_UNKNOWN;
73 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
76 return STC_ERROR_INVALID_PARAMETER;
78 counter->action = NFACCT_ACTION_INSERT;
79 counter->iotype = NFACCT_COUNTER_IN;
80 counter->jump = __get_jump_by_intend(counter);
81 counter->iptype = NFACCT_TYPE_IPV4;
82 counter->send_limit = 0;
83 counter->rcv_limit = 0;
85 return produce_net_rule(counter);
88 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
91 return STC_ERROR_INVALID_PARAMETER;
93 counter->action = NFACCT_ACTION_INSERT;
94 counter->iotype = NFACCT_COUNTER_OUT;
95 counter->jump = __get_jump_by_intend(counter);
96 counter->iptype = NFACCT_TYPE_IPV4;
97 counter->send_limit = 0;
98 counter->rcv_limit = 0;
100 return produce_net_rule(counter);
103 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
106 return STC_ERROR_INVALID_PARAMETER;
108 counter->action = NFACCT_ACTION_DELETE;
109 counter->iotype = NFACCT_COUNTER_IN;
110 counter->jump = __get_jump_by_intend(counter);
111 counter->iptype = NFACCT_TYPE_IPV4;
112 counter->send_limit = 0;
113 counter->rcv_limit = 0;
115 return produce_net_rule(counter);
118 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
121 return STC_ERROR_INVALID_PARAMETER;
123 counter->action = NFACCT_ACTION_DELETE;
124 counter->iotype = NFACCT_COUNTER_OUT;
125 counter->jump = __get_jump_by_intend(counter);
126 counter->iptype = NFACCT_TYPE_IPV4;
127 counter->send_limit = 0;
128 counter->rcv_limit = 0;
130 return produce_net_rule(counter);
133 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
136 return STC_ERROR_INVALID_PARAMETER;
138 counter->action = NFACCT_ACTION_INSERT;
139 counter->iotype = NFACCT_COUNTER_IN;
140 counter->jump = __get_jump_by_intend(counter);
141 counter->iptype = NFACCT_TYPE_IPV6;
142 counter->send_limit = 0;
143 counter->rcv_limit = 0;
145 return produce_net_rule(counter);
148 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
151 return STC_ERROR_INVALID_PARAMETER;
153 counter->action = NFACCT_ACTION_INSERT;
154 counter->iotype = NFACCT_COUNTER_OUT;
155 counter->jump = __get_jump_by_intend(counter);
156 counter->iptype = NFACCT_TYPE_IPV6;
157 counter->send_limit = 0;
158 counter->rcv_limit = 0;
160 return produce_net_rule(counter);
163 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
166 return STC_ERROR_INVALID_PARAMETER;
168 counter->action = NFACCT_ACTION_DELETE;
169 counter->iotype = NFACCT_COUNTER_IN;
170 counter->jump = __get_jump_by_intend(counter);
171 counter->iptype = NFACCT_TYPE_IPV6;
172 counter->send_limit = 0;
173 counter->rcv_limit = 0;
175 return produce_net_rule(counter);
178 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
181 return STC_ERROR_INVALID_PARAMETER;
183 counter->action = NFACCT_ACTION_DELETE;
184 counter->iotype = NFACCT_COUNTER_OUT;
185 counter->jump = __get_jump_by_intend(counter);
186 counter->iptype = NFACCT_TYPE_IPV6;
187 counter->send_limit = 0;
188 counter->rcv_limit = 0;
190 return produce_net_rule(counter);
193 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
194 gpointer UNUSED user_data)
196 stc_process_key_s *key_a = (stc_process_key_s *)a;
197 stc_process_key_s *key_b = (stc_process_key_s *)b;
199 return key_a->pid - key_b->pid;
202 static void __processes_tree_value_free(gpointer data)
204 stc_process_value_s *value = (stc_process_value_s *)data;
209 static void __processes_tree_key_free(gpointer data)
211 stc_process_key_s *key = (stc_process_key_s *)data;
216 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
217 gpointer UNUSED user_data)
219 stc_app_key_s *key_a = (stc_app_key_s *)a;
220 stc_app_key_s *key_b = (stc_app_key_s *)b;
223 ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
227 return g_strcmp0(key_a->app_id, key_b->app_id);
230 static void __apps_tree_value_free(gpointer data)
232 stc_app_value_s *value = (stc_app_value_s *)data;
234 g_tree_destroy(value->processes);
235 value->processes = NULL;
240 static void __apps_tree_key_free(gpointer data)
242 stc_app_key_s *key = (stc_app_key_s *)data;
249 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
250 gpointer UNUSED user_data)
252 stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
253 stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
256 ret = g_strcmp0(key_a->app_id, key_b->app_id);
260 ret = g_strcmp0(key_a->ifname, key_b->ifname);
264 ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
268 ret = key_a->iftype - key_b->iftype;
272 ret = key_a->roaming - key_b->roaming;
279 static void __rstns_tree_value_free(gpointer data)
281 stc_rstn_value_s *value = (stc_rstn_value_s *)data;
286 static void __rstns_tree_key_free(gpointer data)
288 stc_rstn_key_s *key = (stc_rstn_key_s *)data;
292 FREE(key->subscriber_id);
298 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
301 stc_process_key_s *proc_key = (stc_process_key_s *)key;
302 stc_process_value_s *proc_value = (stc_process_value_s *)value;
304 STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
305 proc_key->pid, proc_value->ground);
309 static void __processes_tree_printall(GTree *processes)
311 g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
314 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
317 stc_app_key_s *app_key = (stc_app_key_s *)key;
318 stc_app_value_s *app_value = (stc_app_value_s *)value;
320 STC_LOGD("Application info => Pkg ID [\033[0;34m%s\033[0;m], "
321 "App ID [\033[0;32m%s\033[0;m], Type [%d], classid [%d],"
322 " counter [ in (%lld), out (%lld)]",
323 app_key->pkg_id, app_key->app_id,
324 app_value->type, app_value->classid,
325 app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
327 __processes_tree_printall(app_value->processes);
331 static void __apps_tree_printall(void)
333 g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
338 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
341 remove_pid_context_s *context = (remove_pid_context_s *)data;
342 stc_app_value_s *app_value = (stc_app_value_s *)value;
344 if (!g_tree_remove(app_value->processes, context->proc_key))
347 context->entry_removed = TRUE;
348 context->app_key = (stc_app_key_s *)key;
353 static stc_app_value_s * __application_lookup(GTree *apps,
354 const stc_app_key_s *key)
356 stc_app_value_s *lookup;
358 ret_value_msg_if(apps == NULL, NULL, "apps is null!");
360 lookup = g_tree_lookup(apps, key);
365 static stc_process_value_s * __process_lookup(GTree *processes,
366 const stc_process_key_s *key)
368 stc_process_value_s *lookup;
370 ret_value_msg_if(processes == NULL, NULL, "processes is null!");
372 lookup = g_tree_lookup(processes, key);
378 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
381 guint *pid_count = (guint *)data;
387 static gboolean __add_application_monitor(gpointer key, gpointer value,
390 stc_app_value_s *app_value = (stc_app_value_s *)value;
391 default_connection_s *connection = (default_connection_s *)data;
392 stc_s *stc = stc_get_manager();
394 if (app_value->classid == STC_TOTAL_DATACALL_CLASSID ||
395 app_value->classid == STC_TOTAL_WIFI_CLASSID ||
396 app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID)
399 if (stc && connection && connection->ifname) {
400 struct nfacct_rule counter;
403 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
404 if (stc->carg == NULL) //LCOV_EXCL_LINE
405 return FALSE; //LCOV_EXCL_LINE
407 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
410 memset(&counter, 0, sizeof(struct nfacct_rule));
412 counter.carg = stc->carg;
413 counter.classid = app_value->classid;
414 counter.intend = NFACCT_COUNTER;
416 if (connection->tether_state == TRUE &&
417 connection->tether_iface.ifname != NULL &&
418 app_value->classid == STC_TETHERING_APP_CLASSID) {
419 counter.iftype = connection->tether_iface.type;
420 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
422 counter.iftype = connection->type;
423 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
426 if (app_value->classid == STC_TOTAL_IPV4_CLASSID) {
427 __add_iptables_in(&counter);
428 __add_iptables_out(&counter);
429 } else if (app_value->classid == STC_TOTAL_IPV6_CLASSID) {
430 __add_ip6tables_in(&counter);
431 __add_ip6tables_out(&counter);
433 __add_iptables_in(&counter);
434 __add_iptables_out(&counter);
435 __add_ip6tables_in(&counter);
436 __add_ip6tables_out(&counter);
443 static gboolean __remove_application_monitor(gpointer key, gpointer value,
446 stc_app_value_s *app_value = (stc_app_value_s *)value;
447 default_connection_s *connection = (default_connection_s *)data;
448 stc_s *stc = stc_get_manager();
450 if (stc && connection && connection->ifname) {
451 struct nfacct_rule counter;
454 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
455 if (stc->carg == NULL) //LCOV_EXCL_LINE
456 return FALSE; //LCOV_EXCL_LINE
458 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
461 memset(&counter, 0, sizeof(struct nfacct_rule));
463 counter.carg = stc->carg;
464 counter.classid = app_value->classid;
465 counter.intend = NFACCT_COUNTER;
467 if (connection->tether_state == FALSE &&
468 connection->tether_iface.ifname != NULL &&
469 app_value->classid == STC_TETHERING_APP_CLASSID) {
470 counter.iftype = connection->tether_iface.type;
471 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
473 counter.iftype = connection->type;
474 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
477 __del_iptables_in(&counter);
478 __del_iptables_out(&counter);
479 __del_ip6tables_in(&counter);
480 __del_ip6tables_out(&counter);
486 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
488 STC_LOGI("rstn info => rstn_id [%llu], "
489 "app_id [%s], classid [%u], ifname [%s], "
490 "iftype [%d], rstn_state [%d], rstn_type [%d], "
491 "month_start_date [%d], limit [ (%lld) bytes], "
492 "warn_limit [ (%lld) bytes], "
493 "monthly_limit [ (%lld) bytes], "
494 "weekly_limit [ (%lld) bytes], "
495 "daily_limit [ (%lld) bytes], "
496 "data_counter [ (%lld) bytes], "
497 "warn_counter [ (%lld) bytes], "
498 "monthly_counter [ (%lld) bytes], "
499 "weekly_counter [ (%lld) bytes], "
500 "daily_counter [ (%lld) bytes], "
501 "roaming [%d], subscriber_id [%s]",
502 rstn_value->restriction_id,
503 rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
504 rstn_key->iftype, rstn_value->rstn_state, rstn_value->rstn_type,
505 rstn_value->month_start_date,
506 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA],
507 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN],
508 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY],
509 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY],
510 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY],
511 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
512 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
513 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
514 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
515 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY],
516 rstn_key->roaming, rstn_key->subscriber_id);
519 static void __add_iptables_rule(int64_t classid, nfacct_rule_intend intend,
520 stc_iface_type_e iftype)
522 char *default_ifname = stc_default_connection_get_ifname();
523 default_connection_s *connection = stc_get_default_connection();
524 struct nfacct_rule counter;
525 stc_s *stc = stc_get_manager();
527 g_free(default_ifname); //LCOV_EXCL_LINE
528 return; //LCOV_EXCL_LINE
532 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
533 if (stc->carg == NULL) { //LCOV_EXCL_LINE
534 g_free(default_ifname); //LCOV_EXCL_LINE
535 return; //LCOV_EXCL_LINE
538 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
541 counter.carg = stc->carg;
542 counter.classid = classid;
543 counter.intend = intend;
545 if (connection && connection->tether_iface.ifname != NULL &&
546 classid == STC_TETHERING_APP_CLASSID) {
547 counter.iftype = connection->tether_iface.type;
548 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
550 counter.iftype = iftype;
551 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
554 g_free(default_ifname);
557 __add_iptables_in(&counter);
558 __add_iptables_out(&counter);
561 __add_ip6tables_in(&counter);
562 __add_ip6tables_out(&counter);
565 static void __del_iptables_rule(int64_t classid, nfacct_rule_intend intend,
566 stc_iface_type_e iftype)
568 char *default_ifname = stc_default_connection_get_ifname();
569 default_connection_s *connection = stc_get_default_connection();
570 struct nfacct_rule counter;
571 stc_s *stc = stc_get_manager();
573 g_free(default_ifname); //LCOV_EXCL_LINE
574 return; //LCOV_EXCL_LINE
578 stc->carg = MALLOC0(counter_arg_s, 1); //LCOV_EXCL_LINE
579 if (stc->carg == NULL) { //LCOV_EXCL_LINE
580 g_free(default_ifname); //LCOV_EXCL_LINE
581 return; //LCOV_EXCL_LINE
584 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
587 counter.carg = stc->carg;
588 counter.classid = classid;
589 counter.intend = intend;
591 if (connection && connection->tether_iface.ifname != NULL &&
592 classid == STC_TETHERING_APP_CLASSID) {
593 counter.iftype = connection->tether_iface.type;
594 g_strlcpy(counter.ifname, connection->tether_iface.ifname, MAX_IFACE_LENGTH);
596 counter.iftype = iftype;
597 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
600 g_free(default_ifname);
603 __del_iptables_in(&counter);
604 __del_iptables_out(&counter);
607 __del_ip6tables_in(&counter);
608 __del_ip6tables_out(&counter);
612 time_t month_start_ts;
613 time_t week_start_ts;
615 int64_t monthly_stat;
620 static stc_cb_ret_e __statistics_info_cb(const table_statistics_info *info,
623 cumulative_data_s *stat = (cumulative_data_s *)user_data;
624 int64_t counters = 0;
626 counters = info->cnt.in_bytes + info->cnt.out_bytes;
628 stat->monthly_stat += counters;
629 if (stat->week_start_ts <= info->interval->from)
630 stat->weekly_stat += counters;
631 if (stat->day_start_ts <= info->interval->from)
632 stat->daily_stat += counters;
637 static void __process_restriction(enum traffic_restriction_type rstn_type,
638 stc_rstn_key_s *rstn_key,
639 stc_rstn_value_s *rstn_value, void *data)
641 default_connection_s *old_connection = (default_connection_s *)data;
642 default_connection_s *connection = NULL;
644 if (old_connection != NULL)
645 connection = old_connection;
647 connection = stc_get_default_connection();
649 /* no default ifname */
650 if (connection->ifname == NULL)
653 /* rstn not applicable for this interface */
654 if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
655 (g_strcmp0(connection->ifname, rstn_key->ifname) != 0) &&
656 (g_strcmp0(connection->tether_iface.ifname, rstn_key->ifname) != 0))
659 /* classid is invalid */
660 if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
667 table_counters_info info;
668 int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
670 memset(&info, 0, sizeof(table_counters_info));
671 rstn_value->limit_exceeded = 0;
673 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
674 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
675 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
676 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
677 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
678 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
679 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
680 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
681 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
682 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
683 table_counters_get(rstn_value->restriction_id, &info);
685 time_t current_time = 0;
686 cumulative_data_s stat;
687 table_statistics_select_rule rule;
689 memset(&stat, 0, sizeof(cumulative_data_s));
690 stat.month_start_ts = rstn_value->month_start_ts;
691 stat.week_start_ts = g_system->last_week_ts;
692 stat.day_start_ts = g_system->last_day_ts;
694 memset(&rule, 0, sizeof(table_statistics_select_rule));
695 rule.from = rstn_value->month_start_ts;
697 rule.to = current_time;
698 rule.iftype = rstn_key->iftype;
699 rule.granularity = GRANULARITY;
701 table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat);
703 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
704 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
705 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter + stat.monthly_stat;
706 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter + stat.weekly_stat;
707 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter + stat.daily_stat;
710 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
711 if (rstn_value->limit[i] >= 0) {
712 effective_limit[i] = rstn_value->limit[i] - rstn_value->counter[i];
714 if (effective_limit[i] < 0)
715 rstn_value->limit_exceeded |= (1 << i);
719 STC_LOGD("rstn_id [%llu], datausage [%llu] bytes",
720 rstn_value->restriction_id, info.data_counter);
722 if (rstn_value->limit_exceeded != 0 &&
723 rstn_value->limit_exceeded != (1 << STC_RSTN_LIMIT_TYPE_DATA_WARN)) {
724 __add_iptables_rule(rstn_value->classid, NFACCT_BLOCK, rstn_key->iftype);
727 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
731 __add_iptables_rule(rstn_value->classid, NFACCT_ALLOW,
734 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
735 rstn_value->limit_exceeded = 0;
736 rstn_value->limit_notified = 0;
742 if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
743 __del_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
746 __del_iptables_rule(rstn_value->classid, rstn_value->rstn_type,
749 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
750 rstn_value->limit_exceeded = 0;
751 rstn_value->limit_notified = 0;
753 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
754 if (rstn_value->limit[i] >= 0)
755 rstn_value->counter[i] = 0;
764 static gboolean __remove_rstns_foreach_application(gpointer key,
768 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
769 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
770 gchar *app_id = (gchar *)data;
772 /* rstn rule is not for applications */
773 if (rstn_key->app_id == NULL)
776 /* rstn rule is not for this application */
777 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
780 /* rstn rule is already removed */
781 if (rstn_value->rstn_state == STC_RSTN_STATE_DEACTIVATED)
784 /* remove restriction from system */
785 __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
787 __print_rstn(rstn_key, rstn_value);
793 static void __remove_rstns_for_application(gchar *app_id)
795 g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
799 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
801 stc_error_e ret = STC_ERROR_NONE;
803 stc_app_value_s *lookup;
805 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
807 lookup = __application_lookup(g_system->apps, app_key);
809 STC_LOGE("app_key not found"); //LCOV_EXCL_LINE
810 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
813 g_tree_foreach(lookup->processes, __processes_tree_check_empty,
817 /* remove nfacct rule for this classid */
818 __remove_application_monitor((gpointer) app_key, lookup,
819 stc_get_default_connection());
820 __remove_rstns_for_application(app_key->app_id);
823 if (!g_tree_remove(g_system->apps, app_key)) {
824 ret = STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
825 STC_LOGE("key not found"); //LCOV_EXCL_LINE
831 static stc_error_e __close_contr_sock(stc_system_s *system)
833 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
835 /* close netlink socket for updating kernel counters */
836 if (system->contr_sock != -1) {
837 close(system->contr_sock);
838 system->contr_sock = -1;
841 if (system->contr_gsource_id != 0) {
842 g_source_remove(system->contr_gsource_id);
843 system->contr_gsource_id = 0;
846 return STC_ERROR_NONE;
849 static gboolean __process_contr_reply(GIOChannel *source,
850 GIOCondition condition,
854 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
856 GIOChannel *gio = NULL;
857 ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
859 /* close netlink socket for updating kernel counters */
860 if (system->contr_sock != -1) {
861 close(system->contr_sock);
862 system->contr_sock = -1;
865 if (system->contr_gsource_id != 0) {
866 g_source_remove(system->contr_gsource_id);
867 system->contr_gsource_id = 0;
870 /* create netlink socket for updating kernel counters */
871 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
872 if (system->contr_sock < 0) {
873 STC_LOGE("failed to open socket");
875 return STC_ERROR_FAIL;
878 gio = g_io_channel_unix_new(system->contr_sock);
879 system->contr_gsource_id =
880 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
881 (GIOFunc) __process_contr_reply,
883 g_io_channel_unref(gio);
885 return STC_ERROR_NONE;
888 static void __action_when_rstn_limit_exceeded(stc_rstn_limit_type_e limit_type,
889 stc_rstn_key_s *rstn_key,
890 stc_rstn_value_s *rstn_value,
891 classid_bytes_context_s *context)
894 char iftype[MAX_INT_LENGTH] = { 0, };
895 char byte[MAX_INT_LENGTH] = { 0, };
896 const char *signal_name = NULL;
897 const char *net_popup_content = NULL;
898 const char *net_popup_type = NULL;
899 stc_s *stc = (stc_s *)stc_get_manager();
902 STC_LOGE("Failed to get stc data");
906 switch (limit_type) {
907 case STC_RSTN_LIMIT_TYPE_DATA_WARN:
909 signal_name = "WarnThresholdCrossed";
910 net_popup_content = "warn threshold crossed";
911 net_popup_type = "warning_noti";
914 case STC_RSTN_LIMIT_TYPE_DATA:
915 case STC_RSTN_LIMIT_TYPE_MONTHLY:
916 case STC_RSTN_LIMIT_TYPE_WEEKLY:
917 case STC_RSTN_LIMIT_TYPE_DAILY:
919 signal_name = "RestrictionThresholdCrossed";
920 net_popup_content = "restriction threshold crossed";
921 net_popup_type = "restriction_noti";
923 /* block immediately */
924 context->counter->intend = NFACCT_BLOCK;
925 __del_iptables_in(context->counter);
926 __del_iptables_out(context->counter);
927 __add_iptables_in(context->counter);
928 __add_iptables_out(context->counter);
930 __del_ip6tables_in(context->counter);
931 __del_ip6tables_out(context->counter);
932 __add_ip6tables_in(context->counter);
933 __add_ip6tables_out(context->counter);
934 context->counter->intend = NFACCT_COUNTER;
936 rstn_value->limit_exceeded |= (1 << limit_type);
943 if (signal_name == NULL) {
944 STC_LOGE("Invalid parameter: limit_type");
949 rv = stc_manager_dbus_emit_signal(stc->connection,
950 STC_DBUS_SERVICE_RESTRICTION_PATH,
951 STC_DBUS_INTERFACE_RESTRICTION,
953 g_variant_new("(si)",
958 rstn_value->limit_notified |= (1 << limit_type);
960 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
961 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit[limit_type]);
962 stc_plugin_appstatus_send_message(net_popup_content,
963 net_popup_type, rstn_key->app_id, iftype, byte);
966 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
967 stc_rstn_value_s *rstn_value,
968 classid_bytes_context_s *context)
971 switch (context->counter->iotype) {
972 case NFACCT_COUNTER_IN:
973 case NFACCT_COUNTER_OUT:
974 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
975 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
976 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
977 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
978 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
979 rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
980 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
981 rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
982 (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
983 rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
984 table_counters_info info;
985 memset(&info, 0, sizeof(table_counters_info));
986 table_counters_get(rstn_value->restriction_id, &info);
988 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
989 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
990 rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter;
991 rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter;
992 rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter;
995 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
996 if (rstn_value->limit[i] >= 0 &&
997 !(rstn_value->limit_notified & (1 << i))) {
998 rstn_value->counter[i] += context->bytes;
999 if (rstn_value->limit[i] <= rstn_value->counter[i])
1000 __action_when_rstn_limit_exceeded(i,
1007 g_system->rstns_tree_updated = TRUE;
1008 __print_rstn(rstn_key, rstn_value);
1011 STC_LOGE("unknown iotype");
1017 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
1018 stc_rstn_value_s *rstn_value,
1019 classid_bytes_context_s *context)
1021 if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1022 context->counter->iftype == STC_IFACE_DATACALL) ||
1023 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
1024 context->counter->iftype == STC_IFACE_WIFI) ||
1025 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1026 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1027 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1028 context->counter->iftype == STC_IFACE_WIFI) ||
1029 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1030 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1031 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1032 context->counter->iftype == STC_IFACE_USB) ||
1033 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1034 context->counter->iftype == STC_IFACE_P2P)) {
1035 context->counter->classid = rstn_value->classid;
1036 return __rstn_counter_update(rstn_key, rstn_value, context);
1042 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
1046 gboolean rv = FALSE;
1047 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1048 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1049 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1052 if (context->counter->intend != NFACCT_COUNTER)
1053 goto try_next_callback;
1055 if (rstn_value->limit_exceeded == TRUE) {
1056 context->data_limit_exceeded = TRUE; //LCOV_EXCL_LINE
1057 goto try_next_callback; //LCOV_EXCL_LINE
1060 classid = context->counter->classid;
1061 rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
1063 context->counter->classid = classid;
1064 if (rstn_value->classid != context->counter->classid)
1065 goto try_next_callback;
1067 rv = __rstn_counter_update(rstn_key, rstn_value, context);
1074 static gboolean __update_app_statistics(gpointer key, gpointer value,
1077 stc_app_key_s *app_key = (stc_app_key_s *)key;
1078 stc_app_value_s *app_value = (stc_app_value_s *)value;
1079 time_t *touch_time = (time_t *)data;
1080 stc_db_classid_iftype_key stat_key;
1081 stc_db_app_stats stat;
1082 default_connection_s *default_connection = stc_get_default_connection();
1084 memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
1085 memset(&stat, 0 , sizeof(stc_db_app_stats));
1087 /* Do not update statistics for Tethering
1088 * if tethering is in-active found */
1089 if (default_connection &&
1090 default_connection->tether_state == FALSE &&
1091 !strcmp(app_key->app_id, STC_TOTAL_TETHERING))
1094 /* Do not update statistics for Wi-Fi
1095 * if tethering is active on wlan0 iface */
1096 if (default_connection && default_connection->tether_state &&
1097 default_connection->tether_iface.type == STC_IFACE_WIFI &&
1098 !strcmp(app_key->app_id, STC_TOTAL_WIFI))
1101 stat_key.classid = app_value->classid;
1103 if (app_value->classid == STC_TETHERING_APP_CLASSID &&
1104 default_connection->tether_state == TRUE)
1105 stat_key.iftype = default_connection->tether_iface.type;
1107 stat_key.iftype = default_connection->type;
1109 if (STC_IFACE_DATACALL == stat_key.iftype)
1110 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
1112 stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
1114 if (app_value->classid == STC_TETHERING_APP_CLASSID &&
1115 default_connection->tether_state == TRUE)
1116 g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
1119 g_strlcpy(stat_key.ifname, default_connection->ifname,
1122 stat.app_id = g_strdup(app_key->app_id);
1123 stat.snd_count = app_value->counter.out_bytes;
1124 stat.rcv_count = app_value->counter.in_bytes;
1125 stat.is_roaming = default_connection->roaming;
1127 if (strstr(stat.app_id, "_BACKGROUND")) {
1128 stat.ground = STC_APP_STATE_BACKGROUND;
1130 if (strstr(stat.app_id, "TOTAL_"))
1131 stat.ground = STC_APP_STATE_UNKNOWN;
1133 stat.ground = STC_APP_STATE_FOREGROUND;
1136 table_statistics_insert(&stat_key, &stat, *touch_time);
1138 app_value->counter.out_bytes = 0;
1139 app_value->counter.in_bytes = 0;
1142 FREE(stat_key.subscriber_id);
1147 static gboolean __flush_apps_stats_to_database(gpointer user_data)
1149 time_t current_time = 0;
1150 stc_s *stc = stc_get_manager();
1152 if (stc && stc->carg)
1153 current_time = stc->carg->last_run_time;
1155 if (g_system->apps_tree_updated == FALSE)
1156 return G_SOURCE_REMOVE;
1158 g_system->apps_tree_updated = FALSE;
1161 g_tree_foreach(g_system->apps,
1162 __update_app_statistics,
1165 STC_LOGI("Flushed app stats to database");
1166 return G_SOURCE_REMOVE;
1170 static gboolean __update_counter_statistics(gpointer key, gpointer value,
1173 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1174 table_counters_info info = {
1175 .restriction_id = rstn_value->restriction_id,
1176 .data_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
1177 .warn_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
1178 .monthly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
1179 .weekly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
1180 .daily_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY]
1183 table_counters_update_counters(&info);
1188 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
1190 time_t current_time = 0;
1191 stc_s *stc = stc_get_manager();
1193 if (stc && stc->carg)
1194 current_time = stc->carg->last_run_time;
1196 if (g_system->rstns_tree_updated == FALSE)
1197 return G_SOURCE_REMOVE;
1199 g_system->rstns_tree_updated = FALSE;
1201 if (g_system->rstns)
1202 g_tree_foreach(g_system->rstns,
1203 __update_counter_statistics,
1206 STC_LOGI("Flushed rstns counters to database");
1207 return G_SOURCE_REMOVE;
1211 static void __app_counter_update(stc_app_key_s *app_key,
1212 stc_app_value_s *app_value,
1213 classid_bytes_context_s *context)
1215 switch (context->counter->iotype) {
1216 case NFACCT_COUNTER_IN:
1217 app_value->data_usage.in_bytes += context->bytes;
1218 app_value->counter.in_bytes = context->bytes;
1219 g_system->apps_tree_updated = TRUE;
1222 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1225 case NFACCT_COUNTER_OUT:
1226 app_value->data_usage.out_bytes += context->bytes;
1227 app_value->counter.out_bytes = context->bytes;
1228 g_system->apps_tree_updated = TRUE;
1231 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1235 STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
1239 static void __interface_counter_update(stc_app_key_s *app_key,
1240 stc_app_value_s *app_value,
1241 classid_bytes_context_s *context)
1243 if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1244 context->counter->iftype == STC_IFACE_DATACALL) ||
1245 (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1246 context->counter->iftype == STC_IFACE_WIFI) ||
1247 (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1248 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1249 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1250 context->counter->iftype == STC_IFACE_WIFI) ||
1251 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1252 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1253 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1254 context->counter->iftype == STC_IFACE_USB) ||
1255 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1256 context->counter->iftype == STC_IFACE_P2P))
1257 __app_counter_update(app_key, app_value, context);
1261 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1265 stc_app_key_s *app_key = (stc_app_key_s *)key;
1266 stc_app_value_s *app_value = (stc_app_value_s *)value;
1267 classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1269 if (context->counter->intend != NFACCT_COUNTER)
1270 goto try_next_callback;
1272 __interface_counter_update(app_key, app_value, context);
1274 if (app_value->classid != context->counter->classid)
1275 goto try_next_callback;
1277 __app_counter_update(app_key, app_value, context);
1283 static gboolean __reset_time_counter_foreach_rstn(gpointer key,
1287 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1288 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1289 reset_time_limits_context_s *context = (reset_time_limits_context_s *)data;
1291 time_t now_month_start_ts;
1293 if (rstn_value->month_start_date == 0) {
1294 table_counters_info info;
1295 memset(&info, 0, sizeof(table_counters_info));
1296 table_counters_get_timestamps(rstn_value->restriction_id, &info);
1298 if (info.month_start_date == 0)
1299 rstn_value->month_start_date = 1;
1301 rstn_value->month_start_date = info.month_start_date;
1302 rstn_value->month_start_ts = info.month_start_ts;
1305 now_month_start_ts =
1306 stc_time_get_month_start(context->now,
1307 rstn_value->month_start_date);
1309 if (rstn_value->month_start_ts != now_month_start_ts) {
1310 rstn_value->month_start_ts = now_month_start_ts;
1311 context->month_start_ts = now_month_start_ts;
1312 context->is_updated |= (1 << STC_RSTN_LIMIT_TYPE_MONTHLY);
1315 if (context->is_updated) {
1316 table_counters_info info;
1317 memset(&info, 0, sizeof(table_counters_info));
1319 info.restriction_id = rstn_value->restriction_id;
1320 info.month_start_date = rstn_value->month_start_date;
1321 info.month_start_ts = rstn_value->month_start_ts;
1322 info.week_start_ts = context->week_start_ts;
1323 info.day_start_ts = context->day_start_ts;
1325 table_counters_update_timestamps(&info);
1328 for (i = STC_RSTN_LIMIT_TYPE_MONTHLY; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1330 if ((context->is_updated) & (1 << i)) {
1332 rstn_value->counter[i] = 0;
1334 if (rstn_value->limit_exceeded & (1 << i)) {
1335 /* remove iptables rule */
1336 char *default_ifname = stc_default_connection_get_ifname();
1337 struct nfacct_rule counter;
1338 stc_s *stc = stc_get_manager();
1340 STC_LOGE("Can't get stc data");
1341 g_free(default_ifname);
1342 goto try_next_callback;
1346 stc->carg = MALLOC0(counter_arg_s, 1);
1347 if (stc->carg == NULL) {
1348 g_free(default_ifname);
1349 goto try_next_callback;
1353 stc_monitor_get_counter_socket();
1356 counter.carg = stc->carg;
1357 counter.classid = rstn_value->classid;
1358 counter.intend = NFACCT_BLOCK;
1359 counter.iftype = rstn_key->iftype;
1360 g_strlcpy(counter.ifname, default_ifname,
1363 g_free(default_ifname);
1366 __del_iptables_in(&counter);
1367 __del_iptables_out(&counter);
1369 /* ip6tables rule */
1370 __del_ip6tables_in(&counter);
1371 __del_ip6tables_out(&counter);
1373 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1374 rstn_value->limit_exceeded &= ~(1 << i);
1375 rstn_value->limit_notified &= ~(1 << i);
1384 static void __reset_time_counters_if_required(void)
1386 reset_time_limits_context_s context;
1388 if (g_system == NULL) {
1389 STC_LOGE("stc monitor not initialized!");
1393 context.now = time(NULL);
1394 context.week_start_ts = stc_time_get_week_start(context.now);
1395 context.day_start_ts = stc_time_get_day_start(context.now);
1396 context.is_updated = 0;
1398 if (g_system->last_week_ts != context.week_start_ts) {
1399 g_system->last_week_ts = context.week_start_ts;
1400 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_WEEKLY);
1403 if (g_system->last_day_ts != context.day_start_ts) {
1404 g_system->last_day_ts = context.day_start_ts;
1405 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_DAILY);
1408 if (g_system->rstns) {
1409 g_tree_foreach(g_system->rstns,
1410 __reset_time_counter_foreach_rstn,
1412 if (context.is_updated)
1413 STC_LOGD("Counter reset completed month_start [%ld], week_start [%ld], day_start [%ld]",
1414 context.month_start_ts, g_system->last_week_ts, g_system->last_day_ts);
1418 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1419 struct counter_arg *carg)
1421 __reset_time_counters_if_required();
1423 struct nfacct_rule counter = {
1430 classid_bytes_context_s context = {
1431 .counter = &counter,
1433 .data_limit_exceeded = FALSE,
1436 if (!recreate_counter_by_name(cnt_name, &counter)) {
1437 STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
1438 return; //LCOV_EXCL_LINE
1442 STC_LOGI("classid %u, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1443 context.counter->classid, context.counter->iftype,
1444 context.counter->iotype, context.counter->intend,
1445 context.counter->ifname, context.bytes);
1447 if (g_system->rstns)
1448 g_tree_foreach(g_system->rstns,
1449 __rstn_counter_update_foreach_classid,
1453 g_tree_foreach(g_system->apps,
1454 __apps_counter_update_foreach_classid,
1458 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1461 struct counter_arg *carg = user_data;
1462 char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1463 if (carg->initiate) {
1465 * TODO: this will be used when daemon starts to update existing
1466 * counter data if present.
1468 populate_counters(cnt_name, carg);
1472 (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1473 int bytes = be64toh(*bytes_p);
1475 ++carg->serialized_counters;
1476 __fill_nfacct_result(cnt_name, bytes, carg);
1483 static int __post_fill_counters(void *user_data)
1485 struct counter_arg *carg = user_data;
1493 static void __process_network_counter(struct genl *ans,
1494 struct counter_arg *carg)
1496 struct netlink_serialization_params ser_params = {
1499 .eval_attr = __fill_counters,
1500 .post_eval_attr = __post_fill_counters,
1503 netlink_serialization_command *netlink =
1504 netlink_create_command(&ser_params);
1506 STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
1507 return; //LCOV_EXCL_LINE
1510 netlink->deserialize_answer(&(netlink->params));
1513 static gboolean __process_contr_reply(GIOChannel *source,
1514 GIOCondition condition,
1517 int sock = g_io_channel_unix_get_fd(source);
1520 stc_s *stc = stc_get_manager();
1523 void __gcov_flush(void);
1527 if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1528 (condition & G_IO_NVAL)) {
1529 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1531 STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
1532 "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
1533 (condition & G_IO_ERR), (condition & G_IO_HUP),
1534 (condition & G_IO_NVAL));
1535 __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
1536 return FALSE; //LCOV_EXCL_LINE
1539 ans = MALLOC0(struct genl, 1);
1541 STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
1542 return TRUE; //LCOV_EXCL_LINE
1546 STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
1547 goto out; //LCOV_EXCL_LINE
1550 ret = read_netlink(sock, ans, sizeof(struct genl));
1551 /* STC_LOGD("Counter data received ret [%d]", ret); */
1555 stc->carg->ans_len = ret;
1556 stc->carg->last_run_time = time(NULL);
1558 __process_network_counter(ans, stc->carg);
1560 g_idle_add(__flush_apps_stats_to_database, NULL);
1561 g_idle_add(__flush_rstns_counter_to_database, NULL);
1568 static gboolean __update_contr_cb(void *user_data)
1570 /* Here we just sent command, answer we receive in another callback */
1571 stc_s *stc = stc_get_manager();
1572 ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1574 stc->carg = MALLOC0(counter_arg_s, 1);
1575 if (stc->carg == NULL)
1576 return TRUE; /* we need to continue the timer */
1578 stc->carg->sock = stc_monitor_get_counter_socket();
1582 void __gcov_flush(void);
1586 /* STC_LOGD("Get all counters"); */
1587 nfacct_send_get_all(stc->carg);
1589 /* we need to continue the timer */
1595 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1598 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1599 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1601 __print_rstn(rstn_key, rstn_value);
1605 static void __rstn_tree_printall(void)
1607 g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1612 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1613 const stc_rstn_key_s *key)
1615 stc_rstn_value_s *lookup;
1617 ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1619 lookup = g_tree_lookup(rstns_tree, key);
1624 static gboolean __remove_restriction(gpointer key, gpointer value,
1627 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1628 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1630 __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1631 __print_rstn(rstn_key, rstn_value);
1635 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1638 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1639 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1641 /* rstn rule is activated */
1642 if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1645 if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1646 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1648 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1650 __print_rstn(rstn_key, rstn_value);
1656 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1658 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1659 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1661 /* rstn rule is activated */
1662 if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1665 if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1666 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1668 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1674 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1676 stc_rstn_value_s *lookup_value;
1678 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1680 lookup_value = __rstn_lookup(g_system->rstns, key);
1681 if (!lookup_value) {
1682 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1683 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1686 /* remove counter also */
1687 table_counters_delete(lookup_value->restriction_id);
1688 __remove_restriction(key, lookup_value, NULL);
1690 if (!g_tree_remove(g_system->rstns, key)) {
1691 STC_LOGD("key not found"); //LCOV_EXCL_LINE
1692 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1695 return STC_ERROR_NONE;
1698 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1699 stc_rstn_value_s *value, gboolean debug)
1701 stc_rstn_key_s *rstn_key;
1702 stc_rstn_value_s *rstn_value;
1704 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1706 rstn_value = __rstn_lookup(g_system->rstns, key);
1708 __rstn_tree_remove(key);
1710 rstn_key = MALLOC0(stc_rstn_key_s, 1);
1712 STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
1713 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1716 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1718 STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
1719 FREE(rstn_key); //LCOV_EXCL_LINE
1720 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1723 rstn_key->app_id = g_strdup(key->app_id);
1724 rstn_key->ifname = g_strdup(key->ifname);
1725 rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1726 rstn_key->iftype = key->iftype;
1727 rstn_key->roaming = key->roaming;
1729 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1731 rstn_value->restriction_id = value->restriction_id;
1732 rstn_value->rstn_state = value->rstn_state;
1733 rstn_value->rstn_type = value->rstn_type;
1734 rstn_value->classid = value->classid;
1737 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1738 rstn_value->limit[i] = value->limit[i];
1739 rstn_value->counter[i] = 0;
1742 rstn_value->limit_exceeded = 0;
1743 rstn_value->limit_notified = 0;
1744 rstn_value->month_start_date = value->month_start_date;
1745 rstn_value->month_start_ts = value->month_start_ts;
1748 __add_restriction_debug(key, rstn_value, NULL);
1750 __add_restriction(key, rstn_value, NULL);
1752 return STC_ERROR_NONE;
1756 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1759 stc_cb_ret_e ret = STC_CONTINUE;
1762 stc_rstn_value_s value;
1764 memset(&key, 0, sizeof(stc_rstn_key_s));
1765 memset(&value, 0, sizeof(stc_rstn_value_s));
1767 key.app_id = g_strdup(info->app_id);
1768 key.ifname = g_strdup(info->ifname);
1769 key.subscriber_id = g_strdup(info->subscriber_id);
1770 key.iftype = info->iftype;
1771 key.roaming = info->roaming;
1773 value.rstn_type = info->rstn_type;
1774 value.rstn_state = STC_RSTN_STATE_UNKNOWN;
1775 value.restriction_id = info->restriction_id;
1778 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1780 value.classid = STC_UNKNOWN_CLASSID;
1782 value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
1783 value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
1784 value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
1785 value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
1786 value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
1788 if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1793 FREE(key.subscriber_id);
1797 static void __fill_restritions_list(void)
1799 table_restrictions_foreach(__insert_restriction_cb, NULL);
1801 /* __rstn_tree_printall(); */
1804 static gboolean __add_rstn_foreach_application(gpointer key,
1808 stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1809 stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1810 gchar *app_id = (gchar *)data;
1812 /* rstn rule is not for applications */
1813 if (rstn_key->app_id == NULL)
1816 /* rstn rule is not for this application */
1817 if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1820 /* rstn rule is already applied */
1821 if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1824 /* add restriction to system */
1825 if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1826 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
1828 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
1830 __print_rstn(rstn_key, rstn_value);
1836 static void __add_rstns_for_application(gchar *app_id)
1838 g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1842 static void __add_application_by_interface(const char *app_id)
1844 stc_app_key_s app_key;
1845 stc_app_value_s app_value;
1848 return; //LCOV_EXCL_LINE
1850 memset(&app_key, 0, sizeof(stc_app_key_s));
1851 memset(&app_value, 0, sizeof(stc_app_value_s));
1853 app_key.pkg_id = g_strdup(app_id);
1854 app_key.app_id = g_strdup(app_id);
1856 app_value.type = STC_APP_TYPE_NONE;
1857 app_value.processes = NULL;
1858 app_value.counter.in_bytes = 0;
1859 app_value.counter.out_bytes = 0;
1861 stc_monitor_application_add(app_key, app_value);
1863 FREE(app_key.pkg_id);
1864 FREE(app_key.app_id);
1867 static int __vconf_get_int(const char *key, int *value)
1871 ret = vconf_get_int(key, value);
1872 if (ret != VCONF_OK) {
1873 STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
1874 return -1; //LCOV_EXCL_LINE
1881 static int __vconf_set_int(const char *key, int value)
1885 ret = vconf_set_int(key, value);
1886 if (ret != VCONF_OK) {
1887 STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
1888 return -1; //LCOV_EXCL_LINE
1894 static guint __get_background_state(void)
1896 return g_system->background_state;;
1899 static void __set_background_state(guint state)
1901 g_system->background_state = state;
1904 static gboolean __processes_tree_foreach_background(gpointer key,
1908 stc_process_key_s *proc_key = (stc_process_key_s *)key;
1909 stc_app_key_s *app_key = (stc_app_key_s *)data;
1911 if (g_system->background_state)
1912 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1914 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1919 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1922 stc_app_key_s *app_key = (stc_app_key_s *)key;
1923 stc_app_value_s *app_value = (stc_app_value_s *)value;
1925 if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1926 g_tree_foreach(app_value->processes,
1927 __processes_tree_foreach_background, app_key);
1932 static stc_error_e __process_update_background(void)
1934 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1936 g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1938 return STC_ERROR_NONE;
1942 static void __fill_exceptions_list(void)
1944 stc_plugin_fill_exception_list();
1947 stc_error_e stc_monitor_init(void)
1949 stc_system_s *system = MALLOC0(stc_system_s, 1);
1950 GIOChannel *gio = NULL;
1952 ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1954 /* initializing current classid */
1955 init_current_classid();
1957 /* initializing cgroups */
1960 /* creating monitored application tree */
1961 system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1962 __apps_tree_key_free,
1963 __apps_tree_value_free);
1965 system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1966 __rstns_tree_key_free,
1967 __rstns_tree_value_free);
1969 /* create netlink socket for updating kernel counters */
1970 system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1971 if (system->contr_sock < 0) {
1972 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
1973 FREE(system); //LCOV_EXCL_LINE
1974 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1977 gio = g_io_channel_unix_new(system->contr_sock);
1978 system->contr_gsource_id =
1979 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1980 (GIOFunc) __process_contr_reply,
1982 g_io_channel_unref(gio);
1986 __add_application_by_interface(STC_TOTAL_DATACALL);
1987 __add_application_by_interface(STC_TOTAL_WIFI);
1988 __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1989 __add_application_by_interface(STC_TOTAL_IPV4);
1990 __add_application_by_interface(STC_TOTAL_IPV6);
1991 __add_application_by_interface(STC_TOTAL_TETHERING);
1993 /* creating restriction rules tree */
1994 __update_contr_cb(NULL);
1996 /* registering periodic kernel counters update callback */
1997 g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
2000 if (g_system->contr_timer_id == 0) {
2001 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
2002 __close_contr_sock(g_system); //LCOV_EXCL_LINE
2003 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2006 __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
2007 (int *)&g_system->background_state);
2009 __fill_exceptions_list();
2010 __fill_restritions_list();
2012 return STC_ERROR_NONE;
2015 stc_error_e stc_monitor_deinit(void)
2017 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2019 /* close netlink socket for updating kernel counters */
2020 __close_contr_sock(g_system);
2022 /* remove kernel counters update timer */
2023 if (g_system->contr_timer_id > 0) {
2024 g_source_remove(g_system->contr_timer_id);
2025 g_system->contr_timer_id = 0;
2028 /* destroy monitored application tree */
2029 g_tree_destroy(g_system->apps);
2030 g_system->apps = NULL;
2032 /* destroy restriction rules tree */
2033 g_tree_destroy(g_system->rstns);
2034 g_system->rstns = NULL;
2038 return STC_ERROR_NONE;
2041 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
2042 const stc_app_value_s app_value)
2044 stc_error_e ret = STC_ERROR_NONE;
2046 stc_app_value_s *value;
2047 stc_app_value_s *lookup;
2049 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2051 lookup = __application_lookup(g_system->apps, &app_key);
2053 return STC_ERROR_NONE; //LCOV_EXCL_LINE
2055 key = MALLOC0(stc_app_key_s, 1);
2057 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
2058 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2061 value = MALLOC0(stc_app_value_s, 1);
2063 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
2064 FREE(key); //LCOV_EXCL_LINE
2065 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2068 key->app_id = g_strdup(app_key.app_id);
2069 key->pkg_id = g_strdup(app_key.pkg_id);
2071 value->type = app_value.type;
2072 value->data_usage.in_bytes = app_value.data_usage.in_bytes;
2073 value->data_usage.out_bytes = app_value.data_usage.out_bytes;
2075 value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
2076 __processes_tree_key_free,
2077 __processes_tree_value_free);
2079 /* create cgroup and update classid */
2080 value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
2082 g_tree_insert(g_system->apps, key, value);
2084 /* add nfacct rule for this classid */
2085 __add_application_monitor(key, value, stc_get_default_connection());
2086 __add_rstns_for_application(app_key.app_id);
2091 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
2092 const stc_process_key_s proc_key,
2093 const stc_process_value_s proc_value)
2095 stc_error_e ret = STC_ERROR_NONE;
2096 stc_app_value_s *app_lookup;
2097 stc_process_key_s *key;
2098 stc_process_value_s *value;
2099 stc_process_value_s *proc_lookup;
2101 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2103 app_lookup = __application_lookup(g_system->apps, &app_key);
2106 STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
2107 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2110 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2112 return STC_ERROR_NONE; //LCOV_EXCL_LINE
2114 key = MALLOC0(stc_process_key_s, 1);
2116 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
2117 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2120 value = MALLOC0(stc_process_value_s, 1);
2122 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
2123 FREE(key); //LCOV_EXCL_LINE
2124 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2127 key->pid = proc_key.pid;
2129 value->ground = proc_value.ground;
2131 g_tree_insert(app_lookup->processes, key, value);
2133 /* add pid to application cgroup */
2134 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2137 __apps_tree_printall(); //LCOV_EXCL_LINE
2143 API stc_error_e stc_monitor_process_remove(pid_t pid)
2145 stc_error_e ret = STC_ERROR_NONE;
2146 stc_process_key_s proc_key = {
2150 remove_pid_context_s context = {
2152 .proc_key = &proc_key,
2153 .entry_removed = FALSE,
2156 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2158 g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
2161 if (context.entry_removed)
2162 __application_remove_if_empty(context.app_key);
2165 __apps_tree_printall(); //LCOV_EXCL_LINE
2172 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
2173 const stc_process_key_s proc_key,
2174 stc_app_state_e ground)
2176 stc_error_e ret = STC_ERROR_NONE;
2177 stc_app_value_s *app_lookup;
2178 stc_process_value_s *proc_lookup;
2180 ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2182 app_lookup = __application_lookup(g_system->apps, &app_key);
2184 STC_LOGD("app_key not found");
2185 return STC_ERROR_FAIL;
2188 proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2190 STC_LOGD("proc_key not found");
2191 return STC_ERROR_FAIL;
2194 if (proc_lookup->ground != ground)
2195 proc_lookup->ground = ground;
2197 if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
2198 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
2200 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2206 void stc_monitor_update_rstn_by_default_connection(void *data)
2208 static default_connection_s old_connection;
2209 default_connection_s *new_connection = (default_connection_s *)data;
2211 if (old_connection.path != NULL) {
2214 g_tree_foreach(g_system->apps,
2215 __remove_application_monitor,
2216 (gpointer)&old_connection);
2218 if (g_system->rstns)
2219 g_tree_foreach(g_system->rstns,
2220 __remove_restriction,
2221 (gpointer)&old_connection);
2223 iptables_flush_chains();
2227 FREE(old_connection.path);
2228 FREE(old_connection.ifname);
2229 FREE(old_connection.tether_iface.ifname);
2230 old_connection.type = 0;
2231 old_connection.roaming = 0;
2232 old_connection.tether_state = FALSE;
2233 old_connection.tether_iface.type = 0;
2235 if (new_connection != NULL && new_connection->path != NULL) {
2237 g_tree_foreach(g_system->apps,
2238 __add_application_monitor,
2239 (gpointer)new_connection);
2241 if (g_system->rstns)
2242 g_tree_foreach(g_system->rstns, __add_restriction,
2245 old_connection.path = g_strdup(new_connection->path);
2246 old_connection.ifname = g_strdup(new_connection->ifname);
2247 old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname);
2248 old_connection.type = new_connection->type;
2249 old_connection.roaming = new_connection->roaming;
2250 old_connection.tether_state = new_connection->tether_state;
2251 old_connection.tether_iface.type = new_connection->tether_iface.type;
2255 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
2260 stc_rstn_value_s value;
2262 memset(&key, 0, sizeof(stc_rstn_key_s));
2263 memset(&value, 0, sizeof(stc_rstn_value_s));
2265 key.app_id = g_strdup(info->app_id);
2266 key.ifname = g_strdup(info->ifname);
2267 key.subscriber_id = g_strdup(info->subscriber_id);
2268 key.iftype = info->iftype;
2269 key.roaming = info->roaming;
2271 value.rstn_type = info->rstn_type;
2272 value.rstn_state = STC_RSTN_STATE_UNKNOWN;
2273 value.restriction_id = info->restriction_id;
2276 value.classid = get_classid_by_app_id(info->app_id, TRUE);
2278 value.classid = STC_UNKNOWN_CLASSID;
2280 if (value.classid == STC_BACKGROUND_APP_CLASSID) {
2281 __set_background_state(TRUE); //LCOV_EXCL_LINE
2282 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
2283 __process_update_background(); //LCOV_EXCL_LINE
2286 value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
2287 value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
2288 value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
2289 value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
2290 value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
2291 value.month_start_date = info->month_start_date;
2292 value.month_start_ts = stc_time_get_month_start(time(NULL),
2293 info->month_start_date);
2295 ret = __rstn_tree_add(&key, &value, TRUE);
2299 FREE(key.subscriber_id);
2303 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
2307 stc_rstn_key_s key = {
2308 .app_id = g_strdup(info->app_id),
2309 .ifname = g_strdup(info->ifname),
2310 .subscriber_id = g_strdup(info->subscriber_id),
2311 .iftype = info->iftype,
2312 .roaming = info->roaming,
2315 if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
2316 __set_background_state(FALSE); //LCOV_EXCL_LINE
2317 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
2318 __process_update_background(); //LCOV_EXCL_LINE
2321 ret = __rstn_tree_remove(&key);
2325 FREE(key.subscriber_id);
2329 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
2331 return stc_plugin_check_exception_by_cmdline(cmdline);
2334 int stc_monitor_get_counter_socket(void)
2336 return g_system->contr_sock;