Added data limits monitoring for daily, weekly and monthly
[platform/core/connectivity/stc-manager.git] / src / monitor / stc-monitor.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <linux/netlink.h>
18 #include <vconf.h>
19 #include <vconf-keys.h>
20
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"
27 #include "counter.h"
28 #include "table-statistics.h"
29 #include "table-counters.h"
30 #include "stc-monitor.h"
31 #include "stc-time.h"
32 #include "stc-manager-plugin-appstatus.h"
33 #include "stc-manager-plugin-exception.h"
34
35 #define GRANULARITY 10
36 #define MAX_INT_LENGTH 128
37 #define VCONFKEY_STC_BACKGROUND_STATE "db/stc/background_state"
38
39 typedef struct {
40         time_t now;
41         time_t month_start_ts;
42         time_t week_start_ts;
43         time_t day_start_ts;
44         int is_updated;
45 } reset_time_limits_context_s;
46
47 typedef struct {
48         stc_app_key_s *app_key;
49         stc_process_key_s *proc_key;
50         gboolean entry_removed;
51 } remove_pid_context_s;
52
53 typedef struct {
54         struct nfacct_rule *counter;
55         int64_t bytes;
56         gboolean data_limit_exceeded;
57 } classid_bytes_context_s;
58
59 static stc_system_s *g_system = NULL;
60
61 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
62 {
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;
69
70         return NFACCT_JUMP_UNKNOWN;
71 }
72
73 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
74 {
75         if (counter == NULL)
76                 return STC_ERROR_INVALID_PARAMETER;
77
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;
84
85         return produce_net_rule(counter);
86 }
87
88 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
89 {
90         if (counter == NULL)
91                 return STC_ERROR_INVALID_PARAMETER;
92
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;
99
100         return produce_net_rule(counter);
101 }
102
103 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
104 {
105         if (counter == NULL)
106                 return STC_ERROR_INVALID_PARAMETER;
107
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;
114
115         return produce_net_rule(counter);
116 }
117
118 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
119 {
120         if (counter == NULL)
121                 return STC_ERROR_INVALID_PARAMETER;
122
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;
129
130         return produce_net_rule(counter);
131 }
132
133 static stc_error_e __add_ip6tables_in(struct nfacct_rule *counter)
134 {
135         if (counter == NULL)
136                 return STC_ERROR_INVALID_PARAMETER;
137
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;
144
145         return produce_net_rule(counter);
146 }
147
148 static stc_error_e __add_ip6tables_out(struct nfacct_rule *counter)
149 {
150         if (counter == NULL)
151                 return STC_ERROR_INVALID_PARAMETER;
152
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;
159
160         return produce_net_rule(counter);
161 }
162
163 static stc_error_e __del_ip6tables_in(struct nfacct_rule *counter)
164 {
165         if (counter == NULL)
166                 return STC_ERROR_INVALID_PARAMETER;
167
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;
174
175         return produce_net_rule(counter);
176 }
177
178 static stc_error_e __del_ip6tables_out(struct nfacct_rule *counter)
179 {
180         if (counter == NULL)
181                 return STC_ERROR_INVALID_PARAMETER;
182
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;
189
190         return produce_net_rule(counter);
191 }
192
193 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
194                                         gpointer UNUSED user_data)
195 {
196         stc_process_key_s *key_a = (stc_process_key_s *)a;
197         stc_process_key_s *key_b = (stc_process_key_s *)b;
198
199         return key_a->pid - key_b->pid;
200 }
201
202 static void __processes_tree_value_free(gpointer data)
203 {
204         stc_process_value_s *value = (stc_process_value_s *)data;
205
206         FREE(value);
207 }
208
209 static void __processes_tree_key_free(gpointer data)
210 {
211         stc_process_key_s *key = (stc_process_key_s *)data;
212
213         FREE(key);
214 }
215
216 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
217                                    gpointer UNUSED user_data)
218 {
219         stc_app_key_s *key_a = (stc_app_key_s *)a;
220         stc_app_key_s *key_b = (stc_app_key_s *)b;
221         gint ret;
222
223         ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
224         if (ret)
225                 return ret;
226
227         return g_strcmp0(key_a->app_id, key_b->app_id);
228 }
229
230 static void __apps_tree_value_free(gpointer data)
231 {
232         stc_app_value_s *value = (stc_app_value_s *)data;
233
234         g_tree_destroy(value->processes);
235         value->processes = NULL;
236
237         FREE(value);
238 }
239
240 static void __apps_tree_key_free(gpointer data)
241 {
242         stc_app_key_s *key = (stc_app_key_s *)data;
243
244         g_free(key->pkg_id);
245         g_free(key->app_id);
246         FREE(key);
247 }
248
249 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
250                                     gpointer UNUSED user_data)
251 {
252         stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
253         stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
254         int ret;
255
256         ret = g_strcmp0(key_a->app_id, key_b->app_id);
257         if (ret != 0)
258                 return ret;
259
260         ret = g_strcmp0(key_a->ifname, key_b->ifname);
261         if (ret != 0)
262                 return ret;
263
264         ret = g_strcmp0(key_a->subscriber_id, key_b->subscriber_id);
265         if (ret != 0)
266                 return ret;
267
268         ret = key_a->iftype - key_b->iftype;
269         if (ret != 0)
270                 return ret;
271
272         ret = key_a->roaming - key_b->roaming;
273         if (ret != 0)
274                 return ret;
275
276         return 0;
277 }
278
279 static void __rstns_tree_value_free(gpointer data)
280 {
281         stc_rstn_value_s *value = (stc_rstn_value_s *)data;
282
283         FREE(value);
284 }
285
286 static void __rstns_tree_key_free(gpointer data)
287 {
288         stc_rstn_key_s *key = (stc_rstn_key_s *)data;
289
290         FREE(key->app_id);
291         FREE(key->ifname);
292         FREE(key->subscriber_id);
293         FREE(key);
294 }
295
296 /*
297 //LCOV_EXCL_START
298 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
299                                                gpointer data)
300 {
301         stc_process_key_s *proc_key = (stc_process_key_s *)key;
302         stc_process_value_s *proc_value = (stc_process_value_s *)value;
303
304         STC_LOGD("Process entry => PID [\033[1;33m%d\033[0;m], Ground state [%d]",
305                  proc_key->pid, proc_value->ground);
306         return FALSE;
307 }
308
309 static void __processes_tree_printall(GTree *processes)
310 {
311         g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
312 }
313
314 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
315                                           gpointer data)
316 {
317         stc_app_key_s *app_key = (stc_app_key_s *)key;
318         stc_app_value_s *app_value = (stc_app_value_s *)value;
319
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);
326
327         __processes_tree_printall(app_value->processes);
328         return FALSE;
329 }
330
331 static void __apps_tree_printall(void)
332 {
333         g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
334 }
335 //LCOV_EXCL_STOP
336 */
337
338 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
339                                                gpointer data)
340 {
341         remove_pid_context_s *context = (remove_pid_context_s *)data;
342         stc_app_value_s *app_value = (stc_app_value_s *)value;
343
344         if (!g_tree_remove(app_value->processes, context->proc_key))
345                 return FALSE;
346
347         context->entry_removed = TRUE;
348         context->app_key = (stc_app_key_s *)key;
349
350         return TRUE;
351 }
352
353 static stc_app_value_s * __application_lookup(GTree *apps,
354                                               const stc_app_key_s *key)
355 {
356         stc_app_value_s *lookup;
357
358         ret_value_msg_if(apps == NULL, NULL, "apps is null!");
359
360         lookup = g_tree_lookup(apps, key);
361
362         return lookup;
363 }
364
365 static stc_process_value_s * __process_lookup(GTree *processes,
366                                               const stc_process_key_s *key)
367 {
368         stc_process_value_s *lookup;
369
370         ret_value_msg_if(processes == NULL, NULL, "processes is null!");
371
372         lookup = g_tree_lookup(processes, key);
373
374         return lookup;
375 }
376
377 //LCOV_EXCL_START
378 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
379                                              gpointer data)
380 {
381         guint *pid_count = (guint *)data;
382         (*pid_count)++;
383         return TRUE;
384 }
385 //LCOV_EXCL_STOP
386
387 static gboolean __add_application_monitor(gpointer key, gpointer value,
388                                           gpointer data)
389 {
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();
393
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)
397                 return FALSE;
398
399         if (stc && connection && connection->ifname) {
400                 struct nfacct_rule counter;
401
402                 if (!stc->carg) {
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
406
407                         stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
408                 }
409
410                 memset(&counter, 0, sizeof(struct nfacct_rule));
411
412                 counter.carg = stc->carg;
413                 counter.classid = app_value->classid;
414                 counter.intend = NFACCT_COUNTER;
415
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);
421                 } else {
422                         counter.iftype = connection->type;
423                         g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
424                 }
425
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);
432                 } else {
433                         __add_iptables_in(&counter);
434                         __add_iptables_out(&counter);
435                         __add_ip6tables_in(&counter);
436                         __add_ip6tables_out(&counter);
437                 }
438         }
439
440         return FALSE;
441 }
442
443 static gboolean __remove_application_monitor(gpointer key, gpointer value,
444                                              gpointer data)
445 {
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();
449
450         if (stc && connection && connection->ifname) {
451                 struct nfacct_rule counter;
452
453                 if (!stc->carg) {
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
457
458                         stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
459                 }
460
461                 memset(&counter, 0, sizeof(struct nfacct_rule));
462
463                 counter.carg = stc->carg;
464                 counter.classid = app_value->classid;
465                 counter.intend = NFACCT_COUNTER;
466
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);
472                 } else {
473                         counter.iftype = connection->type;
474                         g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
475                 }
476
477                 __del_iptables_in(&counter);
478                 __del_iptables_out(&counter);
479                 __del_ip6tables_in(&counter);
480                 __del_ip6tables_out(&counter);
481         }
482
483         return FALSE;
484 }
485
486 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
487 {
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);
517 }
518
519 static void __add_iptables_rule(int64_t classid, nfacct_rule_intend intend,
520                                 stc_iface_type_e iftype)
521 {
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();
526         if (!stc) {
527                 g_free(default_ifname); //LCOV_EXCL_LINE
528                 return; //LCOV_EXCL_LINE
529         }
530
531         if (!stc->carg) {
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
536                 }
537
538                 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
539         }
540
541         counter.carg = stc->carg;
542         counter.classid = classid;
543         counter.intend = intend;
544
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);
549         } else {
550                 counter.iftype = iftype;
551                 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
552         }
553
554         g_free(default_ifname);
555
556         /* iptables rule */
557         __add_iptables_in(&counter);
558         __add_iptables_out(&counter);
559
560         /* ip6tables rule */
561         __add_ip6tables_in(&counter);
562         __add_ip6tables_out(&counter);
563 }
564
565 static void __del_iptables_rule(int64_t classid, nfacct_rule_intend intend,
566                                 stc_iface_type_e iftype)
567 {
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();
572         if (!stc) {
573                 g_free(default_ifname); //LCOV_EXCL_LINE
574                 return; //LCOV_EXCL_LINE
575         }
576
577         if (!stc->carg) {
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
582                 }
583
584                 stc->carg->sock = stc_monitor_get_counter_socket(); //LCOV_EXCL_LINE
585         }
586
587         counter.carg = stc->carg;
588         counter.classid = classid;
589         counter.intend = intend;
590
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);
595         } else {
596                 counter.iftype = iftype;
597                 g_strlcpy(counter.ifname, default_ifname, MAX_IFACE_LENGTH);
598         }
599
600         g_free(default_ifname);
601
602         /* iptables rule */
603         __del_iptables_in(&counter);
604         __del_iptables_out(&counter);
605
606         /* ip6tables rule */
607         __del_ip6tables_in(&counter);
608         __del_ip6tables_out(&counter);
609 }
610
611 typedef struct {
612         time_t month_start_ts;
613         time_t week_start_ts;
614         time_t day_start_ts;
615         int64_t monthly_stat;
616         int64_t weekly_stat;
617         int64_t daily_stat;
618 } cumulative_data_s;
619
620 static stc_cb_ret_e __statistics_info_cb(const table_statistics_info *info,
621                                         void *user_data)
622 {
623         cumulative_data_s *stat = (cumulative_data_s *)user_data;
624         int64_t counters = 0;
625
626         counters = info->cnt.in_bytes + info->cnt.out_bytes;
627
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;
633
634         return STC_CONTINUE;
635 }
636
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)
640 {
641         default_connection_s *old_connection = (default_connection_s *)data;
642         default_connection_s *connection = NULL;
643
644         if (old_connection != NULL)
645                 connection = old_connection;
646         else
647                 connection = stc_get_default_connection();
648
649         /* no default ifname */
650         if (connection->ifname == NULL)
651                 return;
652
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))
657                 return;
658
659         /* classid is invalid */
660         if (rstn_value->classid <= STC_UNKNOWN_CLASSID)
661                 return;
662
663         switch (rstn_type) {
664         case RST_SET:
665         {
666                 int i;
667                 table_counters_info info;
668                 int64_t effective_limit[STC_RSTN_LIMIT_TYPE_MAX] = { 0, };
669
670                 memset(&info, 0, sizeof(table_counters_info));
671                 rstn_value->limit_exceeded = 0;
672
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);
684
685                         time_t current_time = 0;
686                         cumulative_data_s stat;
687                         table_statistics_select_rule rule;
688
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.week_start_ts = g_system->last_day_ts;
693
694                         memset(&rule, 0, sizeof(table_statistics_select_rule));
695                         rule.from = rstn_value->month_start_ts;
696                         time(&current_time);
697                         rule.to = current_time;
698                         rule.iftype = rstn_key->iftype;
699                         rule.granularity = GRANULARITY;
700
701                         table_statistics_per_app(rstn_key->app_id, &rule, __statistics_info_cb, &stat);
702
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;
708                 }
709
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];
713
714                                 if (effective_limit[i] < 0)
715                                         rstn_value->limit_exceeded |= (1 << i);
716                         }
717                 }
718
719                 STC_LOGD("rstn_id [%llu], datausage [%llu] bytes",
720                         rstn_value->restriction_id, info.data_counter);
721
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);
725                 }
726
727                 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
728         }
729         break;
730         case RST_EXCLUDE:
731                 __add_iptables_rule(rstn_value->classid, NFACCT_ALLOW,
732                                     rstn_key->iftype);
733
734                 rstn_value->rstn_state = STC_RSTN_STATE_ACTIVATED;
735                 rstn_value->limit_exceeded = 0;
736                 rstn_value->limit_notified = 0;
737                 break;
738         case RST_UNSET:
739         {
740                 int i;
741
742                 if (rstn_value->classid == STC_TETHERING_APP_CLASSID)
743                         __del_iptables_rule(rstn_value->classid, NFACCT_BLOCK,
744                                             rstn_key->iftype);
745                 else
746                         __del_iptables_rule(rstn_value->classid, rstn_value->rstn_type,
747                                             rstn_key->iftype);
748
749                 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
750                 rstn_value->limit_exceeded = 0;
751                 rstn_value->limit_notified = 0;
752
753                 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++)
754                         if (rstn_value->limit[i] >= 0)
755                                 rstn_value->counter[i] = 0;
756         }
757         break;
758         default:
759                 ;//Do Nothing
760         }
761 }
762
763 //LCOV_EXCL_START
764 static gboolean __remove_rstns_foreach_application(gpointer key,
765                                                    gpointer value,
766                                                    gpointer data)
767 {
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;
771
772         /* rstn rule is not for applications */
773         if (rstn_key->app_id == NULL)
774                 goto out;
775
776         /* rstn rule is not for this application */
777         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
778                 goto out;
779
780         /* rstn rule is already removed */
781         if (rstn_value->rstn_state == STC_RSTN_STATE_DEACTIVATED)
782                 goto out;
783
784         /* remove restriction from system */
785         __process_restriction(RST_UNSET, rstn_key, rstn_value, NULL);
786
787         __print_rstn(rstn_key, rstn_value);
788 out:
789         return FALSE;
790 }
791 //LCOV_EXCL_STOP
792
793 static void __remove_rstns_for_application(gchar *app_id)
794 {
795         g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
796                        app_id);
797 }
798
799 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
800 {
801         stc_error_e ret = STC_ERROR_NONE;
802         guint pid_count = 0;
803         stc_app_value_s *lookup;
804
805         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
806
807         lookup = __application_lookup(g_system->apps, app_key);
808         if (!lookup) {
809                 STC_LOGE("app_key not found"); //LCOV_EXCL_LINE
810                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
811         }
812
813         g_tree_foreach(lookup->processes, __processes_tree_check_empty,
814                        &pid_count);
815
816         if (!pid_count) {
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);
821         }
822
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
826         }
827
828         return ret;
829 }
830
831 static stc_error_e __close_contr_sock(stc_system_s *system)
832 {
833         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
834
835         /* close netlink socket for updating kernel counters */
836         if (system->contr_sock != -1) {
837                 close(system->contr_sock);
838                 system->contr_sock = -1;
839         }
840
841         if (system->contr_gsource_id != 0) {
842                 g_source_remove(system->contr_gsource_id);
843                 system->contr_gsource_id = 0;
844         }
845
846         return STC_ERROR_NONE;
847 }
848
849 static gboolean __process_contr_reply(GIOChannel *source,
850                                       GIOCondition condition,
851                                       gpointer user_data);
852
853 //LCOV_EXCL_START
854 static stc_error_e __close_and_reopen_contr_sock(stc_system_s *system)
855 {
856         GIOChannel *gio = NULL;
857         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
858
859         /* close netlink socket for updating kernel counters */
860         if (system->contr_sock != -1) {
861                 close(system->contr_sock);
862                 system->contr_sock = -1;
863         }
864
865         if (system->contr_gsource_id != 0) {
866                 g_source_remove(system->contr_gsource_id);
867                 system->contr_gsource_id = 0;
868         }
869
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");
874                 FREE(system);
875                 return STC_ERROR_FAIL;
876         }
877
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,
882                                NULL);
883         g_io_channel_unref(gio);
884
885         return STC_ERROR_NONE;
886 }
887
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)
892 {
893         gboolean rv;
894         char iftype[MAX_INT_LENGTH];
895         char byte[MAX_INT_LENGTH];
896         const char *signal_name;
897         stc_s *stc = (stc_s *)stc_get_manager();
898
899         if (stc == NULL) {
900                 STC_LOGE("Failed to get stc data");
901                 return;
902         }
903
904         switch (limit_type) {
905         case STC_RSTN_LIMIT_TYPE_DATA_WARN:
906         {
907                 signal_name = "WarnThresholdCrossed";
908
909                 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
910                 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit[limit_type]);
911                 stc_plugin_appstatus_send_warn_message("warn threshold crossed",
912                                                                                 "warning_noti",
913                                                                                 rstn_key->app_id,
914                                                                                 iftype, byte);
915         }
916         break;
917         case STC_RSTN_LIMIT_TYPE_DATA:
918         case STC_RSTN_LIMIT_TYPE_MONTHLY:
919         case STC_RSTN_LIMIT_TYPE_WEEKLY:
920         case STC_RSTN_LIMIT_TYPE_DAILY:
921         {
922                 signal_name = "RestrictionThresholdCrossed";
923
924                 /* block immediately */
925                 context->counter->intend = NFACCT_BLOCK;
926                 __del_iptables_in(context->counter);
927                 __del_iptables_out(context->counter);
928                 __add_iptables_in(context->counter);
929                 __add_iptables_out(context->counter);
930
931                 __del_ip6tables_in(context->counter);
932                 __del_ip6tables_out(context->counter);
933                 __add_ip6tables_in(context->counter);
934                 __add_ip6tables_out(context->counter);
935                 context->counter->intend = NFACCT_COUNTER;
936
937                 rstn_value->limit_exceeded |= (1 << limit_type);
938
939                 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
940                 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit[limit_type]);
941                 stc_plugin_appstatus_send_restriction_message("restriction threshold crossed",
942                                                                                 "restriction_noti",
943                                                                                 rstn_key->app_id,
944                                                                                 iftype, byte);
945         }
946         break;
947         default:
948                 break;
949         }
950
951         /* emit signal */
952         rv = stc_manager_dbus_emit_signal(stc->connection,
953                                                   STC_DBUS_SERVICE_RESTRICTION_PATH,
954                                                   STC_DBUS_INTERFACE_RESTRICTION,
955                                                   signal_name,
956                                                   g_variant_new("(si)",
957                                                   rstn_key->app_id,
958                                                   rstn_key->iftype));
959
960         if (rv == TRUE)
961                 rstn_value->limit_notified |= (1 << limit_type);
962 }
963
964 static gboolean __rstn_counter_update(stc_rstn_key_s *rstn_key,
965                                       stc_rstn_value_s *rstn_value,
966                                       classid_bytes_context_s *context)
967 {
968         int i;
969         switch (context->counter->iotype) {
970         case NFACCT_COUNTER_IN:
971         case NFACCT_COUNTER_OUT:
972                 if ((rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] == 0 &&
973                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA] >= 0) ||
974                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] == 0 &&
975                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] >= 0) ||
976                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] == 0 &&
977                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_MONTHLY] >= 0) ||
978                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] == 0 &&
979                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_WEEKLY] >= 0) ||
980                         (rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] == 0 &&
981                         rstn_value->limit[STC_RSTN_LIMIT_TYPE_DAILY] >= 0)) {
982                         table_counters_info info;
983                         memset(&info, 0, sizeof(table_counters_info));
984                         table_counters_get(rstn_value->restriction_id, &info);
985
986                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA] = info.data_counter;
987                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info.warn_counter;
988                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY] = info.monthly_counter;
989                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY] = info.weekly_counter;
990                         rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY] = info.daily_counter;
991                 }
992
993                 for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
994                         if (rstn_value->limit[i] >= 0 &&
995                                 !(rstn_value->limit_notified & (1 << i))) {
996                                 rstn_value->counter[i] += context->bytes;
997                                 if (rstn_value->limit[i] <= rstn_value->counter[i])
998                                         __action_when_rstn_limit_exceeded(i,
999                                                                                 rstn_key,
1000                                                                                 rstn_value,
1001                                                                                 context);
1002                         }
1003                 }
1004
1005                 g_system->rstns_tree_updated = TRUE;
1006                 __print_rstn(rstn_key, rstn_value);
1007                 break;
1008         default:
1009                 STC_LOGE("unknown iotype");
1010         }
1011
1012         return FALSE;
1013 }
1014
1015 static gboolean __interface_rstn_counter_update(stc_rstn_key_s *rstn_key,
1016                                                 stc_rstn_value_s *rstn_value,
1017                                                 classid_bytes_context_s *context)
1018 {
1019         if ((rstn_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1020                 context->counter->iftype == STC_IFACE_DATACALL) ||
1021                 (rstn_value->classid == STC_TOTAL_WIFI_CLASSID &&
1022                 context->counter->iftype == STC_IFACE_WIFI) ||
1023                 (rstn_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1024                 context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1025                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1026                  context->counter->iftype == STC_IFACE_WIFI) ||
1027                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1028                  context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1029                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1030                  context->counter->iftype == STC_IFACE_USB) ||
1031                 (rstn_value->classid == STC_TETHERING_APP_CLASSID &&
1032                  context->counter->iftype == STC_IFACE_P2P)) {
1033                 context->counter->classid = rstn_value->classid;
1034                 return __rstn_counter_update(rstn_key, rstn_value, context);
1035         }
1036
1037         return FALSE;
1038 }
1039
1040 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
1041                                                       gpointer value,
1042                                                       gpointer data)
1043 {
1044         gboolean rv = FALSE;
1045         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1046         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1047         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1048         uint32_t classid;
1049
1050         if (context->counter->intend != NFACCT_COUNTER)
1051                 goto try_next_callback;
1052
1053         if (rstn_value->limit_exceeded == TRUE) {
1054                 context->data_limit_exceeded = TRUE; //LCOV_EXCL_LINE
1055                 goto try_next_callback; //LCOV_EXCL_LINE
1056         }
1057
1058         classid = context->counter->classid;
1059         rv = __interface_rstn_counter_update(rstn_key, rstn_value, context);
1060
1061         context->counter->classid = classid;
1062         if (rstn_value->classid != context->counter->classid)
1063                 goto try_next_callback;
1064
1065         rv = __rstn_counter_update(rstn_key, rstn_value, context);
1066
1067 try_next_callback:
1068         return rv;
1069 }
1070 //LCOV_EXCL_STOP
1071
1072 static gboolean __update_app_statistics(gpointer key, gpointer value,
1073                                         gpointer data)
1074 {
1075         stc_app_key_s *app_key = (stc_app_key_s *)key;
1076         stc_app_value_s *app_value = (stc_app_value_s *)value;
1077         time_t *touch_time = (time_t *)data;
1078         stc_db_classid_iftype_key stat_key;
1079         stc_db_app_stats stat;
1080         default_connection_s *default_connection = stc_get_default_connection();
1081
1082         memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
1083         memset(&stat, 0 , sizeof(stc_db_app_stats));
1084
1085         /* Do not update statistics for Tethering
1086          * if tethering is in-active found */
1087         if (default_connection &&
1088                 default_connection->tether_state == FALSE &&
1089                 !strcmp(app_key->app_id, STC_TOTAL_TETHERING))
1090                 return FALSE;
1091
1092         /* Do not update statistics for Wi-Fi
1093          * if tethering is active on wlan0 iface */
1094         if (default_connection && default_connection->tether_state &&
1095                 default_connection->tether_iface.type == STC_IFACE_WIFI &&
1096                 !strcmp(app_key->app_id, STC_TOTAL_WIFI))
1097                 return FALSE;
1098
1099         stat_key.classid = app_value->classid;
1100
1101         if (app_value->classid == STC_TETHERING_APP_CLASSID &&
1102                 default_connection->tether_state == TRUE)
1103                 stat_key.iftype = default_connection->tether_iface.type;
1104         else
1105                 stat_key.iftype = default_connection->type;
1106
1107         if (STC_IFACE_DATACALL == stat_key.iftype)
1108                 stat_key.subscriber_id = g_strdup(default_connection->subscriber_id);
1109         else
1110                 stat_key.subscriber_id = g_strdup("none_subid"); //LCOV_EXCL_LINE
1111
1112         if (app_value->classid == STC_TETHERING_APP_CLASSID &&
1113                 default_connection->tether_state == TRUE)
1114                 g_strlcpy(stat_key.ifname, default_connection->tether_iface.ifname,
1115                           MAX_IFACE_LENGTH);
1116         else
1117                 g_strlcpy(stat_key.ifname, default_connection->ifname,
1118                           MAX_IFACE_LENGTH);
1119
1120         stat.app_id = g_strdup(app_key->app_id);
1121         stat.snd_count = app_value->counter.out_bytes;
1122         stat.rcv_count = app_value->counter.in_bytes;
1123         stat.is_roaming = default_connection->roaming;
1124
1125         if (strstr(stat.app_id, "_BACKGROUND")) {
1126                 stat.ground = STC_APP_STATE_BACKGROUND;
1127         } else {
1128                 if (strstr(stat.app_id, "TOTAL_"))
1129                         stat.ground = STC_APP_STATE_UNKNOWN;
1130                 else
1131                         stat.ground = STC_APP_STATE_FOREGROUND;
1132         }
1133
1134         table_statistics_insert(&stat_key, &stat, *touch_time);
1135
1136         app_value->counter.out_bytes = 0;
1137         app_value->counter.in_bytes = 0;
1138
1139         FREE(stat.app_id);
1140         FREE(stat_key.subscriber_id);
1141
1142         return FALSE;
1143 }
1144
1145 static gboolean __flush_apps_stats_to_database(gpointer user_data)
1146 {
1147         time_t current_time = 0;
1148         stc_s *stc = stc_get_manager();
1149
1150         if (stc && stc->carg)
1151                 current_time = stc->carg->last_run_time;
1152
1153         if (g_system->apps_tree_updated == FALSE)
1154                 return G_SOURCE_REMOVE;
1155
1156         g_system->apps_tree_updated = FALSE;
1157
1158         if (g_system->apps)
1159                 g_tree_foreach(g_system->apps,
1160                                __update_app_statistics,
1161                                &current_time);
1162
1163         STC_LOGI("Flushed app stats to database");
1164         return G_SOURCE_REMOVE;
1165 }
1166
1167 //LCOV_EXCL_START
1168 static gboolean __update_counter_statistics(gpointer key, gpointer value,
1169                                             gpointer data)
1170 {
1171         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1172         table_counters_info info = {
1173                 .restriction_id = rstn_value->restriction_id,
1174                 .data_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA],
1175                 .warn_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DATA_WARN],
1176                 .monthly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_MONTHLY],
1177                 .weekly_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_WEEKLY],
1178                 .daily_counter = rstn_value->counter[STC_RSTN_LIMIT_TYPE_DAILY]
1179         };
1180
1181         table_counters_update_counters(&info);
1182
1183         return FALSE;
1184 }
1185
1186 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
1187 {
1188         time_t current_time = 0;
1189         stc_s *stc = stc_get_manager();
1190
1191         if (stc && stc->carg)
1192                 current_time = stc->carg->last_run_time;
1193
1194         if (g_system->rstns_tree_updated == FALSE)
1195                 return G_SOURCE_REMOVE;
1196
1197         g_system->rstns_tree_updated = FALSE;
1198
1199         if (g_system->rstns)
1200                 g_tree_foreach(g_system->rstns,
1201                                __update_counter_statistics,
1202                                &current_time);
1203
1204         STC_LOGI("Flushed rstns counters to database");
1205         return G_SOURCE_REMOVE;
1206 }
1207 //LCOV_EXCL_STOP
1208
1209 static void __app_counter_update(stc_app_key_s *app_key,
1210                                  stc_app_value_s *app_value,
1211                                  classid_bytes_context_s *context)
1212 {
1213         switch (context->counter->iotype) {
1214         case NFACCT_COUNTER_IN:
1215                 app_value->data_usage.in_bytes += context->bytes;
1216                 app_value->counter.in_bytes = context->bytes;
1217                 g_system->apps_tree_updated = TRUE;
1218
1219                 /*
1220                 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1221                 */
1222                 break;
1223         case NFACCT_COUNTER_OUT:
1224                 app_value->data_usage.out_bytes += context->bytes;
1225                 app_value->counter.out_bytes = context->bytes;
1226                 g_system->apps_tree_updated = TRUE;
1227
1228                 /*
1229                 __apps_tree_foreach_print(app_key, app_value, NULL); //LCOV_EXCL_LINE
1230                 */
1231                 break;
1232         default:
1233                 STC_LOGE("unknown iotype"); //LCOV_EXCL_LINE
1234         }
1235 }
1236
1237 static void __interface_counter_update(stc_app_key_s *app_key,
1238                                        stc_app_value_s *app_value,
1239                                        classid_bytes_context_s *context)
1240 {
1241         if ((app_value->classid == STC_TOTAL_DATACALL_CLASSID &&
1242              context->counter->iftype == STC_IFACE_DATACALL) ||
1243             (app_value->classid == STC_TOTAL_WIFI_CLASSID &&
1244              context->counter->iftype == STC_IFACE_WIFI) ||
1245             (app_value->classid == STC_TOTAL_BLUETOOTH_CLASSID &&
1246              context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1247                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1248                  context->counter->iftype == STC_IFACE_WIFI) ||
1249                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1250                  context->counter->iftype == STC_IFACE_BLUETOOTH) ||
1251                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1252                  context->counter->iftype == STC_IFACE_USB) ||
1253                 (app_value->classid == STC_TETHERING_APP_CLASSID &&
1254                  context->counter->iftype == STC_IFACE_P2P))
1255                  __app_counter_update(app_key, app_value, context);
1256 }
1257
1258
1259 static gboolean __apps_counter_update_foreach_classid(gpointer key,
1260                                                       gpointer value,
1261                                                       gpointer data)
1262 {
1263         stc_app_key_s *app_key = (stc_app_key_s *)key;
1264         stc_app_value_s *app_value = (stc_app_value_s *)value;
1265         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
1266
1267         if (context->counter->intend != NFACCT_COUNTER)
1268                 goto try_next_callback;
1269
1270         __interface_counter_update(app_key, app_value, context);
1271
1272         if (app_value->classid != context->counter->classid)
1273                 goto try_next_callback;
1274
1275         __app_counter_update(app_key, app_value, context);
1276
1277 try_next_callback:
1278         return FALSE;
1279 }
1280
1281 static gboolean __reset_time_counter_foreach_rstn(gpointer key,
1282                                                   gpointer value,
1283                                                   gpointer data)
1284 {
1285         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1286         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1287         reset_time_limits_context_s *context = (reset_time_limits_context_s *)data;
1288         int i;
1289         time_t now_month_start_ts;
1290
1291         if (rstn_value->month_start_date == 0) {
1292                 table_counters_info info;
1293                 memset(&info, 0, sizeof(table_counters_info));
1294                 table_counters_get_timestamps(rstn_value->restriction_id, &info);
1295
1296                 if (info.month_start_date == 0)
1297                         rstn_value->month_start_date = 1;
1298                 else
1299                         rstn_value->month_start_date = info.month_start_date;
1300                 rstn_value->month_start_ts = info.month_start_ts;
1301         }
1302
1303         now_month_start_ts =
1304                 stc_time_get_month_start(context->now,
1305                                          rstn_value->month_start_date);
1306
1307         if (rstn_value->month_start_ts != now_month_start_ts) {
1308                 rstn_value->month_start_ts = now_month_start_ts;
1309                 context->month_start_ts = now_month_start_ts;
1310                 context->is_updated |= (1 << STC_RSTN_LIMIT_TYPE_MONTHLY);
1311         }
1312
1313         if (context->is_updated) {
1314                 table_counters_info info;
1315                 memset(&info, 0, sizeof(table_counters_info));
1316
1317                 info.restriction_id = rstn_value->restriction_id;
1318                 info.month_start_date = rstn_value->month_start_date;
1319                 info.month_start_ts = rstn_value->month_start_ts;
1320                 info.week_start_ts = context->week_start_ts;
1321                 info.day_start_ts = context->day_start_ts;
1322
1323                 table_counters_update_timestamps(&info);
1324         }
1325
1326         for (i = STC_RSTN_LIMIT_TYPE_MONTHLY; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1327
1328                 if ((context->is_updated) & (1 << i)) {
1329                         /* reset limit */
1330                         rstn_value->counter[i] = 0;
1331
1332                         if (rstn_value->limit_exceeded & (1 << i)) {
1333                                 /* remove iptables rule */
1334                                 char *default_ifname = stc_default_connection_get_ifname();
1335                                 struct nfacct_rule counter;
1336                                 stc_s *stc = stc_get_manager();
1337                                 if (stc == NULL) {
1338                                         STC_LOGE("Can't get stc data");
1339                                         g_free(default_ifname);
1340                                         goto try_next_callback;
1341                                 }
1342
1343                                 if (!stc->carg) {
1344                                         stc->carg = MALLOC0(counter_arg_s, 1);
1345                                         if (stc->carg == NULL) {
1346                                                 g_free(default_ifname);
1347                                                 goto try_next_callback;
1348                                         }
1349
1350                                         stc->carg->sock =
1351                                                 stc_monitor_get_counter_socket();
1352                                 }
1353
1354                                 counter.carg = stc->carg;
1355                                 counter.classid = rstn_value->classid;
1356                                 counter.intend = NFACCT_BLOCK;
1357                                 counter.iftype = rstn_key->iftype;
1358                                 g_strlcpy(counter.ifname, default_ifname,
1359                                           MAX_IFACE_LENGTH);
1360
1361                                 g_free(default_ifname);
1362
1363                                 /* iptables rule */
1364                                 __del_iptables_in(&counter);
1365                                 __del_iptables_out(&counter);
1366
1367                                 /* ip6tables rule */
1368                                 __del_ip6tables_in(&counter);
1369                                 __del_ip6tables_out(&counter);
1370
1371                                 rstn_value->rstn_state = STC_RSTN_STATE_DEACTIVATED;
1372                                 rstn_value->limit_exceeded &= ~(1 << i);
1373                                 rstn_value->limit_notified &= ~(1 << i);
1374                         }
1375                 }
1376         }
1377
1378 try_next_callback:
1379         return FALSE;
1380 }
1381
1382 static void __reset_time_counters_if_required(void)
1383 {
1384         reset_time_limits_context_s context;
1385
1386         if (g_system == NULL) {
1387                 STC_LOGE("stc monitor not initialized!");
1388                 return;
1389         }
1390
1391         context.now = time(NULL);
1392         context.week_start_ts = stc_time_get_week_start(context.now);
1393         context.day_start_ts = stc_time_get_day_start(context.now);
1394         context.is_updated = 0;
1395
1396         if (g_system->last_week_ts != context.week_start_ts) {
1397                 g_system->last_week_ts = context.week_start_ts;
1398                 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_WEEKLY);
1399         }
1400
1401         if (g_system->last_day_ts != context.day_start_ts) {
1402                 g_system->last_day_ts = context.day_start_ts;
1403                 context.is_updated |= (1 << STC_RSTN_LIMIT_TYPE_DAILY);
1404         }
1405
1406         if (g_system->rstns) {
1407                 g_tree_foreach(g_system->rstns,
1408                                __reset_time_counter_foreach_rstn,
1409                                &context);
1410                 if (context.is_updated)
1411                         STC_LOGD("Counter reset completed month_start [%ld], week_start [%ld], day_start [%ld]",
1412                                  context.month_start_ts, g_system->last_week_ts, g_system->last_day_ts);
1413         }
1414 }
1415
1416 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
1417                                  struct counter_arg *carg)
1418 {
1419         __reset_time_counters_if_required();
1420
1421         struct nfacct_rule counter = {
1422                 .carg = carg,
1423                 .name = {0},
1424                 .ifname = {0},
1425                 0
1426         };
1427
1428         classid_bytes_context_s context = {
1429                 .counter = &counter,
1430                 .bytes = bytes,
1431                 .data_limit_exceeded = FALSE,
1432         };
1433
1434         if (!recreate_counter_by_name(cnt_name, &counter)) {
1435                 STC_LOGE("Can't parse counter name %s", cnt_name); //LCOV_EXCL_LINE
1436                 return; //LCOV_EXCL_LINE
1437         }
1438
1439         if (STC_DEBUG_LOG)
1440                 STC_LOGI("classid %u, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
1441                         context.counter->classid, context.counter->iftype,
1442                         context.counter->iotype, context.counter->intend,
1443                         context.counter->ifname, context.bytes);
1444
1445         if (g_system->rstns)
1446                 g_tree_foreach(g_system->rstns,
1447                                __rstn_counter_update_foreach_classid,
1448                                &context);
1449
1450         if (g_system->apps)
1451                 g_tree_foreach(g_system->apps,
1452                                __apps_counter_update_foreach_classid,
1453                                &context);
1454 }
1455
1456 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
1457                            void *user_data)
1458 {
1459         struct counter_arg *carg = user_data;
1460         char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
1461         if (carg->initiate) {
1462                 /**
1463                  * TODO: this will be used when daemon starts to update existing
1464                  * counter data if present.
1465                  *
1466                  populate_counters(cnt_name, carg);
1467                  */
1468         } else {
1469                 int64_t *bytes_p =
1470                         (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
1471                 int bytes = be64toh(*bytes_p);
1472                 if (bytes) {
1473                         ++carg->serialized_counters;
1474                         __fill_nfacct_result(cnt_name, bytes, carg);
1475                 }
1476         }
1477
1478         return 0;
1479 }
1480
1481 static int __post_fill_counters(void *user_data)
1482 {
1483         struct counter_arg *carg = user_data;
1484
1485         if (carg->initiate)
1486                 carg->initiate = 0;
1487
1488         return 0;
1489 }
1490
1491 static void __process_network_counter(struct genl *ans,
1492                                       struct counter_arg *carg)
1493 {
1494         struct netlink_serialization_params ser_params = {
1495                 .carg = carg,
1496                 .ans = ans,
1497                 .eval_attr = __fill_counters,
1498                 .post_eval_attr = __post_fill_counters,
1499         };
1500
1501         netlink_serialization_command *netlink =
1502                 netlink_create_command(&ser_params);
1503         if (!netlink) {
1504                 STC_LOGE("Can not create command"); //LCOV_EXCL_LINE
1505                 return; //LCOV_EXCL_LINE
1506         }
1507
1508         netlink->deserialize_answer(&(netlink->params));
1509 }
1510
1511 static gboolean __process_contr_reply(GIOChannel *source,
1512                                       GIOCondition condition,
1513                                       gpointer user_data)
1514 {
1515         int sock = g_io_channel_unix_get_fd(source);
1516         struct genl *ans;
1517         int ret;
1518         stc_s *stc = stc_get_manager();
1519
1520 #ifdef TIZEN_GTESTS
1521         void __gcov_flush(void);
1522         __gcov_flush();
1523 #endif
1524
1525         if ((condition & G_IO_ERR) || (condition & G_IO_HUP) ||
1526             (condition & G_IO_NVAL)) {
1527                 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
1528
1529                 STC_LOGE("Counter socket received G_IO event, closing socket." //LCOV_EXCL_LINE
1530                          "G_IO_ERR [%u], G_IO_HUP [%u], G_IO_NVAL [%u]",
1531                          (condition & G_IO_ERR), (condition & G_IO_HUP),
1532                          (condition & G_IO_NVAL));
1533                 __close_and_reopen_contr_sock(g_system); //LCOV_EXCL_LINE
1534                 return FALSE; //LCOV_EXCL_LINE
1535         }
1536
1537         ans = MALLOC0(struct genl, 1);
1538         if (ans == NULL) {
1539                 STC_LOGE("Failed allocate memory to genl reply message"); //LCOV_EXCL_LINE
1540                 return TRUE; //LCOV_EXCL_LINE
1541         }
1542
1543         if (stc == NULL) {
1544                 STC_LOGE("Can't get stc data"); //LCOV_EXCL_LINE
1545                 goto out; //LCOV_EXCL_LINE
1546         }
1547
1548         ret = read_netlink(sock, ans, sizeof(struct genl));
1549         /* STC_LOGD("Counter data received ret [%d]", ret); */
1550         if (ret == 0)
1551                 goto out;
1552
1553         stc->carg->ans_len = ret;
1554         stc->carg->last_run_time = time(NULL);
1555
1556         __process_network_counter(ans, stc->carg);
1557
1558         g_idle_add(__flush_apps_stats_to_database, NULL);
1559         g_idle_add(__flush_rstns_counter_to_database, NULL);
1560 out:
1561
1562         FREE(ans);
1563         return TRUE;
1564 }
1565
1566 static gboolean __update_contr_cb(void *user_data)
1567 {
1568         /* Here we just sent command, answer we receive in another callback */
1569         stc_s *stc = stc_get_manager();
1570         ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
1571         if (!stc->carg) {
1572                 stc->carg = MALLOC0(counter_arg_s, 1);
1573                 if (stc->carg == NULL)
1574                         return TRUE;  /* we need to continue the timer */
1575
1576                 stc->carg->sock = stc_monitor_get_counter_socket();
1577         }
1578
1579 #ifdef TIZEN_GTESTS
1580         void __gcov_flush(void);
1581         __gcov_flush();
1582 #endif
1583
1584         /* STC_LOGD("Get all counters"); */
1585         nfacct_send_get_all(stc->carg);
1586
1587         /* we need to continue the timer */
1588         return TRUE;
1589 }
1590
1591 /*
1592 //LCOV_EXCL_START
1593 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
1594                                           gpointer data)
1595 {
1596         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1597         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1598
1599         __print_rstn(rstn_key, rstn_value);
1600         return FALSE;
1601 }
1602
1603 static void __rstn_tree_printall(void)
1604 {
1605         g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
1606 }
1607 //LCOV_EXCL_STOP
1608 */
1609
1610 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
1611                                         const stc_rstn_key_s *key)
1612 {
1613         stc_rstn_value_s *lookup;
1614
1615         ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
1616
1617         lookup = g_tree_lookup(rstns_tree, key);
1618
1619         return lookup;
1620 }
1621
1622 static gboolean __remove_restriction(gpointer key, gpointer value,
1623                                      gpointer data)
1624 {
1625         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1626         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1627
1628         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
1629         __print_rstn(rstn_key, rstn_value);
1630         return FALSE;
1631 }
1632
1633 static gboolean __add_restriction_debug(gpointer key, gpointer value,
1634                                         gpointer data)
1635 {
1636         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1637         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1638
1639         /* rstn rule is activated */
1640         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1641                 return FALSE;
1642
1643         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1644                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1645         else
1646                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1647
1648         __print_rstn(rstn_key, rstn_value);
1649
1650         return FALSE;
1651 }
1652
1653 //LCOV_EXCL_START
1654 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1655 {
1656         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1657         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1658
1659         /* rstn rule is activated */
1660         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1661                 return FALSE;
1662
1663         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1664                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1665         else
1666                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1667
1668         return FALSE;
1669 }
1670 //LCOV_EXCL_STOP
1671
1672 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1673 {
1674         stc_rstn_value_s *lookup_value;
1675
1676         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1677
1678         lookup_value = __rstn_lookup(g_system->rstns, key);
1679         if (!lookup_value) {
1680                 STC_LOGE("key not found"); //LCOV_EXCL_LINE
1681                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1682         }
1683
1684         /* remove counter also */
1685         table_counters_delete(lookup_value->restriction_id);
1686         __remove_restriction(key, lookup_value, NULL);
1687
1688         if (!g_tree_remove(g_system->rstns, key)) {
1689                 STC_LOGD("key not found"); //LCOV_EXCL_LINE
1690                 return STC_ERROR_NO_DATA; //LCOV_EXCL_LINE
1691         }
1692
1693         return STC_ERROR_NONE;
1694 }
1695
1696 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1697                                    stc_rstn_value_s *value, gboolean debug)
1698 {
1699         stc_rstn_key_s *rstn_key;
1700         stc_rstn_value_s *rstn_value;
1701
1702         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1703
1704         rstn_value = __rstn_lookup(g_system->rstns, key);
1705         if (rstn_value)
1706                 __rstn_tree_remove(key);
1707
1708         rstn_key = MALLOC0(stc_rstn_key_s, 1);
1709         if (!rstn_key) {
1710                 STC_LOGE("rstn_key allocation failed"); //LCOV_EXCL_LINE
1711                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1712         }
1713
1714         rstn_value = MALLOC0(stc_rstn_value_s, 1);
1715         if (!rstn_value) {
1716                 STC_LOGE("rstn_value allocation failed"); //LCOV_EXCL_LINE
1717                 FREE(rstn_key); //LCOV_EXCL_LINE
1718                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
1719         }
1720
1721         rstn_key->app_id = g_strdup(key->app_id);
1722         rstn_key->ifname = g_strdup(key->ifname);
1723         rstn_key->subscriber_id = g_strdup(key->subscriber_id);
1724         rstn_key->iftype = key->iftype;
1725         rstn_key->roaming = key->roaming;
1726
1727         g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1728
1729         rstn_value->restriction_id = value->restriction_id;
1730         rstn_value->rstn_state = value->rstn_state;
1731         rstn_value->rstn_type = value->rstn_type;
1732         rstn_value->classid = value->classid;
1733
1734         int i;
1735         for (i = 0; i < STC_RSTN_LIMIT_TYPE_MAX; i++) {
1736                 rstn_value->limit[i] = value->limit[i];
1737                 rstn_value->counter[i] = 0;
1738         }
1739
1740         rstn_value->limit_exceeded = 0;
1741         rstn_value->limit_notified = 0;
1742         rstn_value->month_start_date = value->month_start_date;
1743         rstn_value->month_start_ts = value->month_start_ts;
1744
1745         if (debug == TRUE)
1746                 __add_restriction_debug(key, rstn_value, NULL);
1747         else
1748                 __add_restriction(key, rstn_value, NULL);
1749
1750         return STC_ERROR_NONE;
1751 }
1752
1753 //LCOV_EXCL_START
1754 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1755                                             void *user_data)
1756 {
1757         stc_cb_ret_e ret = STC_CONTINUE;
1758
1759         stc_rstn_key_s key;
1760         stc_rstn_value_s value;
1761
1762         memset(&key, 0, sizeof(stc_rstn_key_s));
1763         memset(&value, 0, sizeof(stc_rstn_value_s));
1764
1765         key.app_id = g_strdup(info->app_id);
1766         key.ifname = g_strdup(info->ifname);
1767         key.subscriber_id = g_strdup(info->subscriber_id);
1768         key.iftype = info->iftype;
1769         key.roaming = info->roaming;
1770
1771         value.rstn_type = info->rstn_type;
1772         value.rstn_state = STC_RSTN_STATE_UNKNOWN;
1773         value.restriction_id = info->restriction_id;
1774
1775         if (info->app_id)
1776                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1777         else
1778                 value.classid = STC_UNKNOWN_CLASSID;
1779
1780         value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
1781         value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
1782         value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
1783         value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
1784         value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
1785
1786         if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1787                 ret = STC_CANCEL;
1788
1789         FREE(key.app_id);
1790         FREE(key.ifname);
1791         FREE(key.subscriber_id);
1792         return ret;
1793 }
1794
1795 static void __fill_restritions_list(void)
1796 {
1797         table_restrictions_foreach(__insert_restriction_cb, NULL);
1798
1799         /* __rstn_tree_printall(); */
1800 }
1801
1802 static gboolean __add_rstn_foreach_application(gpointer key,
1803                                                gpointer value,
1804                                                gpointer data)
1805 {
1806         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1807         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1808         gchar *app_id = (gchar *)data;
1809
1810         /* rstn rule is not for applications */
1811         if (rstn_key->app_id == NULL)
1812                 goto out;
1813
1814         /* rstn rule is not for this application */
1815         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1816                 goto out;
1817
1818         /* rstn rule is already applied */
1819         if (rstn_value->rstn_state == STC_RSTN_STATE_ACTIVATED)
1820                 goto out;
1821
1822         /* add restriction to system */
1823         if (rstn_value->rstn_type == STC_RSTN_TYPE_ACCEPT)
1824                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, NULL);
1825         else
1826                 __process_restriction(RST_SET, rstn_key, rstn_value, NULL);
1827
1828         __print_rstn(rstn_key, rstn_value);
1829 out:
1830         return FALSE;
1831 }
1832 //LCOV_EXCL_STOP
1833
1834 static void __add_rstns_for_application(gchar *app_id)
1835 {
1836         g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1837                        app_id);
1838 }
1839
1840 static void __add_application_by_interface(const char *app_id)
1841 {
1842         stc_app_key_s app_key;
1843         stc_app_value_s app_value;
1844
1845         if (app_id == NULL)
1846                 return; //LCOV_EXCL_LINE
1847
1848         memset(&app_key, 0, sizeof(stc_app_key_s));
1849         memset(&app_value, 0, sizeof(stc_app_value_s));
1850
1851         app_key.pkg_id = g_strdup(app_id);
1852         app_key.app_id = g_strdup(app_id);
1853
1854         app_value.type = STC_APP_TYPE_NONE;
1855         app_value.processes = NULL;
1856         app_value.counter.in_bytes = 0;
1857         app_value.counter.out_bytes = 0;
1858
1859         stc_monitor_application_add(app_key, app_value);
1860
1861         FREE(app_key.pkg_id);
1862         FREE(app_key.app_id);
1863 }
1864
1865 static int __vconf_get_int(const char *key, int *value)
1866 {
1867         int ret = 0;
1868
1869         ret = vconf_get_int(key, value);
1870         if (ret != VCONF_OK) {
1871                 STC_LOGE("Failed to get vconfkey [%s] value", key); //LCOV_EXCL_LINE
1872                 return -1; //LCOV_EXCL_LINE
1873         }
1874
1875         return 0;
1876 }
1877
1878 //LCOV_EXCL_START
1879 static int __vconf_set_int(const char *key, int value)
1880 {
1881         int ret = 0;
1882
1883         ret = vconf_set_int(key, value);
1884         if (ret != VCONF_OK) {
1885                 STC_LOGE("Failed to set vconfkey [%s] value", key); //LCOV_EXCL_LINE
1886                 return -1; //LCOV_EXCL_LINE
1887         }
1888
1889         return 0;
1890 }
1891
1892 static guint __get_background_state(void)
1893 {
1894         return g_system->background_state;;
1895 }
1896
1897 static void __set_background_state(guint state)
1898 {
1899         g_system->background_state = state;
1900 }
1901
1902 static gboolean __processes_tree_foreach_background(gpointer key,
1903                                                     gpointer value,
1904                                                     gpointer data)
1905 {
1906         stc_process_key_s *proc_key = (stc_process_key_s *)key;
1907         stc_app_key_s *app_key = (stc_app_key_s *)data;
1908
1909         if (g_system->background_state)
1910                 place_pids_to_net_cgroup(proc_key->pid, STC_BACKGROUND_APP_ID);
1911         else
1912                 place_pids_to_net_cgroup(proc_key->pid, app_key->app_id);
1913
1914         return FALSE;
1915 }
1916
1917 static gboolean __apps_tree_foreach_background(gpointer key, gpointer value,
1918                                         gpointer data)
1919 {
1920         stc_app_key_s *app_key = (stc_app_key_s *)key;
1921         stc_app_value_s *app_value = (stc_app_value_s *)value;
1922
1923         if (strstr(app_key->app_id, STC_BACKGROUND_APP_SUFFIX))
1924                 g_tree_foreach(app_value->processes,
1925                                __processes_tree_foreach_background, app_key);
1926
1927         return FALSE;
1928 }
1929
1930 static stc_error_e __process_update_background(void)
1931 {
1932         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1933
1934         g_tree_foreach(g_system->apps, __apps_tree_foreach_background, NULL);
1935
1936         return STC_ERROR_NONE;
1937 }
1938 //LCOV_EXCL_STOP
1939
1940 static void __fill_exceptions_list(void)
1941 {
1942         stc_plugin_fill_exception_list();
1943 }
1944
1945 stc_error_e stc_monitor_init(void)
1946 {
1947         stc_system_s *system = MALLOC0(stc_system_s, 1);
1948         GIOChannel *gio = NULL;
1949
1950         ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1951
1952         /* initializing current classid */
1953         init_current_classid();
1954
1955         /* initializing cgroups */
1956         cgroup_init();
1957
1958         /* creating monitored application tree */
1959         system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1960                                        __apps_tree_key_free,
1961                                        __apps_tree_value_free);
1962
1963         system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1964                                         __rstns_tree_key_free,
1965                                         __rstns_tree_value_free);
1966
1967         /* create netlink socket for updating kernel counters */
1968         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1969         if (system->contr_sock < 0) {
1970                 STC_LOGE("failed to open socket"); //LCOV_EXCL_LINE
1971                 FREE(system); //LCOV_EXCL_LINE
1972                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
1973         }
1974
1975         gio = g_io_channel_unix_new(system->contr_sock);
1976         system->contr_gsource_id =
1977                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1978                                (GIOFunc) __process_contr_reply,
1979                                NULL);
1980         g_io_channel_unref(gio);
1981
1982         g_system = system;
1983
1984         __add_application_by_interface(STC_TOTAL_DATACALL);
1985         __add_application_by_interface(STC_TOTAL_WIFI);
1986         __add_application_by_interface(STC_TOTAL_BLUETOOTH);
1987         __add_application_by_interface(STC_TOTAL_IPV4);
1988         __add_application_by_interface(STC_TOTAL_IPV6);
1989         __add_application_by_interface(STC_TOTAL_TETHERING);
1990
1991         /* creating restriction rules tree */
1992         __update_contr_cb(NULL);
1993
1994         /* registering periodic kernel counters update callback */
1995         g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1996                                                          __update_contr_cb,
1997                                                          NULL);
1998         if (g_system->contr_timer_id == 0) {
1999                 STC_LOGE("Failed to register kernel counters update timer"); //LCOV_EXCL_LINE
2000                 __close_contr_sock(g_system); //LCOV_EXCL_LINE
2001                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2002         }
2003
2004         __vconf_get_int(VCONFKEY_STC_BACKGROUND_STATE,
2005                         (int *)&g_system->background_state);
2006
2007         __fill_exceptions_list();
2008         __fill_restritions_list();
2009
2010         return STC_ERROR_NONE;
2011 }
2012
2013 stc_error_e stc_monitor_deinit(void)
2014 {
2015         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2016
2017         /* close netlink socket for updating kernel counters */
2018         __close_contr_sock(g_system);
2019
2020         /* remove kernel counters update timer */
2021         if (g_system->contr_timer_id > 0) {
2022                 g_source_remove(g_system->contr_timer_id);
2023                 g_system->contr_timer_id = 0;
2024         }
2025
2026         /* destroy monitored application tree */
2027         g_tree_destroy(g_system->apps);
2028         g_system->apps = NULL;
2029
2030         /* destroy restriction rules tree */
2031         g_tree_destroy(g_system->rstns);
2032         g_system->rstns = NULL;
2033
2034         FREE(g_system);
2035
2036         return STC_ERROR_NONE;
2037 }
2038
2039 API stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
2040                                         const stc_app_value_s app_value)
2041 {
2042         stc_error_e ret = STC_ERROR_NONE;
2043         stc_app_key_s *key;
2044         stc_app_value_s *value;
2045         stc_app_value_s *lookup;
2046
2047         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2048
2049         lookup = __application_lookup(g_system->apps, &app_key);
2050         if (lookup)
2051                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
2052
2053         key = MALLOC0(stc_app_key_s, 1);
2054         if (!key) {
2055                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
2056                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2057         }
2058
2059         value = MALLOC0(stc_app_value_s, 1);
2060         if (!value) {
2061                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
2062                 FREE(key); //LCOV_EXCL_LINE
2063                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2064         }
2065
2066         key->app_id = g_strdup(app_key.app_id);
2067         key->pkg_id = g_strdup(app_key.pkg_id);
2068
2069         value->type = app_value.type;
2070         value->data_usage.in_bytes = app_value.data_usage.in_bytes;
2071         value->data_usage.out_bytes = app_value.data_usage.out_bytes;
2072
2073         value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
2074                                            __processes_tree_key_free,
2075                                            __processes_tree_value_free);
2076
2077         /* create cgroup and update classid */
2078         value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
2079
2080         g_tree_insert(g_system->apps, key, value);
2081
2082         /* add nfacct rule for this classid */
2083         __add_application_monitor(key, value, stc_get_default_connection());
2084         __add_rstns_for_application(app_key.app_id);
2085
2086         return ret;
2087 }
2088
2089 API stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
2090                                     const stc_process_key_s proc_key,
2091                                     const stc_process_value_s proc_value)
2092 {
2093         stc_error_e ret = STC_ERROR_NONE;
2094         stc_app_value_s *app_lookup;
2095         stc_process_key_s *key;
2096         stc_process_value_s *value;
2097         stc_process_value_s *proc_lookup;
2098
2099         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2100
2101         app_lookup = __application_lookup(g_system->apps, &app_key);
2102         if (!app_lookup) {
2103                 if (STC_DEBUG_LOG)
2104                         STC_LOGD("app_key not found"); //LCOV_EXCL_LINE
2105                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
2106         }
2107
2108         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2109         if (proc_lookup)
2110                 return STC_ERROR_NONE; //LCOV_EXCL_LINE
2111
2112         key = MALLOC0(stc_process_key_s, 1);
2113         if (!key) {
2114                 STC_LOGE("key allocation failed"); //LCOV_EXCL_LINE
2115                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2116         }
2117
2118         value = MALLOC0(stc_process_value_s, 1);
2119         if (!value) {
2120                 STC_LOGE("value allocation failed"); //LCOV_EXCL_LINE
2121                 FREE(key); //LCOV_EXCL_LINE
2122                 return STC_ERROR_OUT_OF_MEMORY; //LCOV_EXCL_LINE
2123         }
2124
2125         key->pid = proc_key.pid;
2126
2127         value->ground = proc_value.ground;
2128
2129         g_tree_insert(app_lookup->processes, key, value);
2130
2131         /* add pid to application cgroup */
2132         place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2133
2134         /*
2135         __apps_tree_printall(); //LCOV_EXCL_LINE
2136         */
2137
2138         return ret;
2139 }
2140
2141 API stc_error_e stc_monitor_process_remove(pid_t pid)
2142 {
2143         stc_error_e ret = STC_ERROR_NONE;
2144         stc_process_key_s proc_key = {
2145                 .pid = pid
2146         };
2147
2148         remove_pid_context_s context = {
2149                 .app_key = NULL,
2150                 .proc_key = &proc_key,
2151                 .entry_removed = FALSE,
2152         };
2153
2154         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2155
2156         g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
2157                        &context);
2158
2159         if (context.entry_removed)
2160                 __application_remove_if_empty(context.app_key);
2161
2162         /*
2163         __apps_tree_printall(); //LCOV_EXCL_LINE
2164         */
2165
2166         return ret;
2167 }
2168
2169 //LCOV_EXCL_START
2170 API stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
2171                                               const stc_process_key_s proc_key,
2172                                               stc_app_state_e ground)
2173 {
2174         stc_error_e ret = STC_ERROR_NONE;
2175         stc_app_value_s *app_lookup;
2176         stc_process_value_s *proc_lookup;
2177
2178         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
2179
2180         app_lookup = __application_lookup(g_system->apps, &app_key);
2181         if (!app_lookup) {
2182                 STC_LOGD("app_key not found");
2183                 return STC_ERROR_FAIL;
2184         }
2185
2186         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
2187         if (!proc_lookup) {
2188                 STC_LOGD("proc_key not found");
2189                 return STC_ERROR_FAIL;
2190         }
2191
2192         if (proc_lookup->ground != ground)
2193                 proc_lookup->ground = ground;
2194
2195         if (ground == STC_APP_STATE_BACKGROUND && __get_background_state())
2196                 place_pids_to_net_cgroup(proc_key.pid, STC_BACKGROUND_APP_ID);
2197         else
2198                 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
2199
2200         return ret;
2201 }
2202 //LCOV_EXCL_STOP
2203
2204 void stc_monitor_update_rstn_by_default_connection(void *data)
2205 {
2206         static default_connection_s old_connection;
2207         default_connection_s *new_connection = (default_connection_s *)data;
2208
2209         if (old_connection.path != NULL) {
2210                 //LCOV_EXCL_START
2211                 if (g_system->apps)
2212                         g_tree_foreach(g_system->apps,
2213                                        __remove_application_monitor,
2214                                        (gpointer)&old_connection);
2215
2216                 if (g_system->rstns)
2217                         g_tree_foreach(g_system->rstns,
2218                                        __remove_restriction,
2219                                        (gpointer)&old_connection);
2220
2221                 iptables_flush_chains();
2222                 //LCOV_EXCL_STOP
2223         }
2224
2225         FREE(old_connection.path);
2226         FREE(old_connection.ifname);
2227         FREE(old_connection.tether_iface.ifname);
2228         old_connection.type = 0;
2229         old_connection.roaming = 0;
2230         old_connection.tether_state = FALSE;
2231         old_connection.tether_iface.type = 0;
2232
2233         if (new_connection != NULL && new_connection->path != NULL) {
2234                 if (g_system->apps)
2235                         g_tree_foreach(g_system->apps,
2236                                        __add_application_monitor,
2237                                        (gpointer)new_connection);
2238
2239                 if (g_system->rstns)
2240                         g_tree_foreach(g_system->rstns, __add_restriction,
2241                                        NULL);
2242
2243                 old_connection.path = g_strdup(new_connection->path);
2244                 old_connection.ifname = g_strdup(new_connection->ifname);
2245                 old_connection.tether_iface.ifname = g_strdup(new_connection->tether_iface.ifname);
2246                 old_connection.type = new_connection->type;
2247                 old_connection.roaming = new_connection->roaming;
2248                 old_connection.tether_state = new_connection->tether_state;
2249                 old_connection.tether_iface.type = new_connection->tether_iface.type;
2250         }
2251 }
2252
2253 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
2254 {
2255         stc_error_e ret;
2256
2257         stc_rstn_key_s key;
2258         stc_rstn_value_s value;
2259
2260         memset(&key, 0, sizeof(stc_rstn_key_s));
2261         memset(&value, 0, sizeof(stc_rstn_value_s));
2262
2263         key.app_id = g_strdup(info->app_id);
2264         key.ifname = g_strdup(info->ifname);
2265         key.subscriber_id = g_strdup(info->subscriber_id);
2266         key.iftype = info->iftype;
2267         key.roaming = info->roaming;
2268
2269         value.rstn_type = info->rstn_type;
2270         value.rstn_state = STC_RSTN_STATE_UNKNOWN;
2271         value.restriction_id = info->restriction_id;
2272
2273         if (info->app_id)
2274                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
2275         else
2276                 value.classid = STC_UNKNOWN_CLASSID;
2277
2278         if (value.classid == STC_BACKGROUND_APP_CLASSID) {
2279                 __set_background_state(TRUE); //LCOV_EXCL_LINE
2280                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
2281                 __process_update_background(); //LCOV_EXCL_LINE
2282         }
2283
2284         value.limit[STC_RSTN_LIMIT_TYPE_DATA] = info->data_limit;
2285         value.limit[STC_RSTN_LIMIT_TYPE_DATA_WARN] = info->data_warn_limit;
2286         value.limit[STC_RSTN_LIMIT_TYPE_MONTHLY] = info->monthly_limit;
2287         value.limit[STC_RSTN_LIMIT_TYPE_WEEKLY] = info->weekly_limit;
2288         value.limit[STC_RSTN_LIMIT_TYPE_DAILY] = info->daily_limit;
2289         value.month_start_date = info->month_start_date;
2290         value.month_start_ts = stc_time_get_month_start(time(NULL),
2291                                                                                 info->month_start_date);
2292
2293         ret = __rstn_tree_add(&key, &value, TRUE);
2294
2295         FREE(key.app_id);
2296         FREE(key.ifname);
2297         FREE(key.subscriber_id);
2298         return ret;
2299 }
2300
2301 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
2302 {
2303         stc_error_e ret;
2304
2305         stc_rstn_key_s key = {
2306                 .app_id = g_strdup(info->app_id),
2307                 .ifname = g_strdup(info->ifname),
2308                 .subscriber_id = g_strdup(info->subscriber_id),
2309                 .iftype = info->iftype,
2310                 .roaming = info->roaming,
2311         };
2312
2313         if (!strcmp(key.app_id, STC_BACKGROUND_APP_ID)) {
2314                 __set_background_state(FALSE); //LCOV_EXCL_LINE
2315                 __vconf_set_int(VCONFKEY_STC_BACKGROUND_STATE, g_system->background_state); //LCOV_EXCL_LINE
2316                 __process_update_background(); //LCOV_EXCL_LINE
2317         }
2318
2319         ret = __rstn_tree_remove(&key);
2320
2321         FREE(key.app_id);
2322         FREE(key.ifname);
2323         FREE(key.subscriber_id);
2324         return ret;
2325 }
2326
2327 API stc_error_e stc_monitor_check_excn_by_cmdline(char *cmdline)
2328 {
2329         return stc_plugin_check_exception_by_cmdline(cmdline);
2330 }
2331
2332 int stc_monitor_get_counter_socket(void)
2333 {
2334         return g_system->contr_sock;
2335 }