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