Initialize smart traffic control manager package
[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
19 #include "stc-default-connection.h"
20 #include "helper-nl.h"
21 #include "helper-nfacct-rule.h"
22 #include "helper-net-cls.h"
23 #include "counter.h"
24 #include "table-statistics.h"
25 #include "table-counters.h"
26 #include "stc-monitor.h"
27
28 typedef struct {
29         stc_app_key_s *app_key;
30         stc_process_key_s *proc_key;
31         gboolean entry_removed;
32 } remove_pid_context_s;
33
34 typedef struct {
35         struct nfacct_rule *counter;
36         uint64_t bytes;
37         gboolean in_limit_reached;
38         gboolean out_limit_reached;
39 } classid_bytes_context_s;
40
41 static stc_system_s *g_system = NULL;
42
43 static nfacct_rule_jump __get_jump_by_intend(struct nfacct_rule *counter)
44 {
45         if (counter->intend == NFACCT_WARN)
46                 return NFACCT_JUMP_ACCEPT;
47         else if (counter->intend == NFACCT_BLOCK)
48                 return NFACCT_JUMP_REJECT;
49
50         return NFACCT_JUMP_UNKNOWN;
51 }
52
53 static stc_error_e __add_iptables_in(struct nfacct_rule *counter)
54 {
55         return produce_net_rule(counter, 0, 0,
56                 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
57                 NFACCT_COUNTER_IN);
58 }
59
60 static stc_error_e __add_iptables_out(struct nfacct_rule *counter)
61 {
62         return produce_net_rule(counter, 0, 0,
63                 NFACCT_ACTION_INSERT, __get_jump_by_intend(counter),
64                 NFACCT_COUNTER_OUT);
65 }
66
67 static stc_error_e __del_iptables_in(struct nfacct_rule *counter)
68 {
69         return produce_net_rule(counter, 0, 0,
70                 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
71                 NFACCT_COUNTER_IN);
72 }
73
74 static stc_error_e __del_iptables_out(struct nfacct_rule *counter)
75 {
76         return produce_net_rule(counter, 0, 0,
77                 NFACCT_ACTION_DELETE, __get_jump_by_intend(counter),
78                 NFACCT_COUNTER_OUT);
79 }
80
81 static int __processes_tree_key_compare(gconstpointer a, gconstpointer b,
82                                         gpointer UNUSED user_data)
83 {
84         stc_process_key_s *key_a = (stc_process_key_s *)a;
85         stc_process_key_s *key_b = (stc_process_key_s *)b;
86
87         return key_a->pid - key_b->pid;
88 }
89
90 static void __processes_tree_value_free(gpointer data)
91 {
92         stc_process_value_s *value = (stc_process_value_s *)data;
93
94         FREE(value);
95 }
96
97 static void __processes_tree_key_free(gpointer data)
98 {
99         stc_process_key_s *key = (stc_process_key_s *)data;
100
101         FREE(key);
102 }
103
104 static int __apps_tree_key_compare(gconstpointer a, gconstpointer b,
105                                    gpointer UNUSED user_data)
106 {
107         stc_app_key_s *key_a = (stc_app_key_s *)a;
108         stc_app_key_s *key_b = (stc_app_key_s *)b;
109         gint ret;
110
111         ret = g_strcmp0(key_a->pkg_id, key_b->pkg_id);
112         if (ret)
113                 return ret;
114
115         return g_strcmp0(key_a->app_id, key_b->app_id);
116 }
117
118 static void __apps_tree_value_free(gpointer data)
119 {
120         stc_app_value_s *value = (stc_app_value_s *)data;
121
122         g_tree_destroy(value->processes);
123         value->processes = NULL;
124
125         FREE(value);
126 }
127
128 static void __apps_tree_key_free(gpointer data)
129 {
130         stc_app_key_s *key = (stc_app_key_s *)data;
131
132         g_free(key->pkg_id);
133         g_free(key->app_id);
134         FREE(key);
135 }
136
137 static int __rstns_tree_key_compare(gconstpointer a, gconstpointer b,
138                                     gpointer UNUSED user_data)
139 {
140         stc_rstn_key_s *key_a = (stc_rstn_key_s *)a;
141         stc_rstn_key_s *key_b = (stc_rstn_key_s *)b;
142         int ret;
143
144         ret = g_strcmp0(key_a->app_id, key_b->app_id);
145         if (ret != 0)
146                 return ret;
147
148         ret = g_strcmp0(key_a->ifname, key_b->ifname);
149         if (ret != 0)
150                 return ret;
151
152         ret = g_strcmp0(key_a->imsi, key_b->imsi);
153         if (ret != 0)
154                 return ret;
155
156         ret = key_a->iftype - key_b->iftype;
157         if (ret != 0)
158                 return ret;
159
160         return 0;
161 }
162
163 static void __rstns_tree_value_free(gpointer data)
164 {
165         stc_rstn_value_s *value = (stc_rstn_value_s *)data;
166
167         FREE(value);
168 }
169
170 static void __rstns_tree_key_free(gpointer data)
171 {
172         stc_rstn_key_s *key = (stc_rstn_key_s *)data;
173
174         FREE(key->app_id);
175         FREE(key->ifname);
176         FREE(key->imsi);
177         FREE(key);
178 }
179
180 static gboolean __processes_tree_foreach_print(gpointer key, gpointer value,
181                                                gpointer data)
182 {
183         stc_process_key_s *proc_key = (stc_process_key_s *)key;
184         stc_process_value_s *proc_value = (stc_process_value_s *)value;
185
186         STC_LOGD("Process entry => PID [%d], Ground state [%d]",
187                  proc_key->pid, proc_value->ground);
188         return FALSE;
189 }
190
191 static void __processes_tree_printall(GTree *processes)
192 {
193         g_tree_foreach(processes, __processes_tree_foreach_print, NULL);
194 }
195
196 static gboolean __apps_tree_foreach_print(gpointer key, gpointer value,
197                                           gpointer data)
198 {
199         stc_app_key_s *app_key = (stc_app_key_s *)key;
200         stc_app_value_s *app_value = (stc_app_value_s *)value;
201
202         STC_LOGD("Application info => Pkg ID [%s], App ID [%s],"
203                  " Type [%d], classid [%d],"
204                  " counter [ in (%llu), out (%llu)]",
205                  app_key->pkg_id, app_key->app_id,
206                  app_value->type, app_value->classid,
207                  app_value->data_usage.in_bytes, app_value->data_usage.out_bytes);
208
209         __processes_tree_printall(app_value->processes);
210         return FALSE;
211 }
212
213 static void __apps_tree_printall(void)
214 {
215         g_tree_foreach(g_system->apps, __apps_tree_foreach_print, NULL);
216 }
217
218 static gboolean __apps_tree_foreach_remove_pid(gpointer key, gpointer value,
219                                                gpointer data)
220 {
221         remove_pid_context_s *context = (remove_pid_context_s *)data;
222         stc_app_value_s *app_value = (stc_app_value_s *)value;
223
224         if (!g_tree_remove(app_value->processes, context->proc_key)) {
225                 STC_LOGD("key not found");
226                 return FALSE;
227         }
228
229         context->entry_removed = TRUE;
230         context->app_key = (stc_app_key_s *)key;
231
232         return TRUE;
233 }
234
235 static stc_app_value_s * __application_lookup(GTree *apps,
236                                               const stc_app_key_s *key)
237 {
238         stc_app_value_s *lookup;
239
240         ret_value_msg_if(apps == NULL, NULL, "apps is null!");
241
242         lookup = g_tree_lookup(apps, key);
243
244         return lookup;
245 }
246
247 static stc_process_value_s * __process_lookup(GTree *processes,
248                                               const stc_process_key_s *key)
249 {
250         stc_process_value_s *lookup;
251
252         ret_value_msg_if(processes == NULL, NULL, "processes is null!");
253
254         lookup = g_tree_lookup(processes, key);
255
256         return lookup;
257 }
258
259 static gboolean __processes_tree_check_empty(gpointer key, gpointer value,
260                                              gpointer data)
261 {
262         guint *pid_count = (guint *)data;
263         (*pid_count)++;
264         return TRUE;
265 }
266
267 static gboolean __add_application_monitor(gpointer key, gpointer value,
268                                           gpointer data)
269 {
270         stc_app_value_s *app_value = (stc_app_value_s *)value;
271         default_connection_s *connection = (default_connection_s *)data;
272         stc_s *stc = stc_get_manager();
273
274         if (stc && connection && connection->ifname) {
275                 struct nfacct_rule counter;
276
277                 if (!stc->carg) {
278                         stc->carg = MALLOC0(counter_arg_s, 1);
279                         stc->carg->sock = stc_monitor_get_counter_socket();
280                 }
281
282                 memset(&counter, 0, sizeof(struct nfacct_rule));
283
284                 counter.carg = stc->carg;
285                 counter.classid = app_value->classid;
286                 counter.intend = NFACCT_COUNTER;
287                 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
288
289                 __add_iptables_in(&counter);
290                 __add_iptables_out(&counter);
291         }
292
293         return FALSE;
294 }
295
296 static gboolean __remove_application_monitor(gpointer key, gpointer value,
297                                              gpointer data)
298 {
299         stc_app_value_s *app_value = (stc_app_value_s *)value;
300         default_connection_s *connection = (default_connection_s *)data;
301         stc_s *stc = stc_get_manager();
302
303         if (stc && connection && connection->ifname) {
304                 struct nfacct_rule counter;
305
306                 if (!stc->carg) {
307                         stc->carg = MALLOC0(counter_arg_s, 1);
308                         stc->carg->sock = stc_monitor_get_counter_socket();
309                 }
310
311                 memset(&counter, 0, sizeof(struct nfacct_rule));
312
313                 counter.carg = stc->carg;
314                 counter.classid = app_value->classid;
315                 counter.intend = NFACCT_COUNTER;
316                 g_strlcpy(counter.ifname, connection->ifname, MAX_IFACE_LENGTH);
317
318                 __del_iptables_in(&counter);
319                 __del_iptables_out(&counter);
320         }
321
322         return FALSE;
323 }
324
325 static void __print_rstn(stc_rstn_key_s *rstn_key, stc_rstn_value_s *rstn_value)
326 {
327         STC_LOGI("rstn info => rstn_id [%llu], "
328                  "app_id [%s], classid [%lu], ifname [%s], "
329                  "iftype [%d], rst_state [%d], "
330                  "limit [ in (%llu), out (%llu)], "
331                  "warn_limit [ in (%llu), out (%llu)], "
332                  "counter [ in (%llu), out (%llu)], "
333                  "roaming [%d], imsi [%s]",
334                  rstn_value->restriction_id,
335                  rstn_key->app_id, rstn_value->classid , rstn_key->ifname,
336                  rstn_key->iftype, rstn_value->rst_state,
337                  rstn_value->limit.in_bytes, rstn_value->limit.out_bytes,
338                  rstn_value->warn_limit.in_bytes,
339                  rstn_value->warn_limit.out_bytes,
340                  rstn_value->counter.in_bytes, rstn_value->counter.out_bytes,
341                  rstn_key->roaming, rstn_key->imsi);
342 }
343
344 static void __process_restriction(enum traffic_restriction_type rst_type,
345                                   stc_rstn_key_s *rstn_key,
346                                   stc_rstn_value_s *rstn_value, void *data)
347 {
348         stc_data_counter_s effective_limit, effective_warn_limit;
349         default_connection_s *old_connection = (default_connection_s *)data;
350         default_connection_s *connection = NULL;
351
352         if (old_connection != NULL)
353                 connection = old_connection;
354         else
355                 connection = stc_get_default_connection();
356
357         /* no default ifname */
358         if (connection->ifname == NULL)
359                 return;
360
361         /* rstn not applicable for this interface */
362         if (rstn_key->ifname != NULL && g_strcmp0("", rstn_key->ifname) != 0 &&
363             g_strcmp0(connection->ifname, rstn_key->ifname) != 0)
364                 return;
365
366         /* classid is invalid */
367         if (rstn_value->classid == STC_UNKNOWN_CLASSID)
368                 return;
369
370         effective_limit.out_bytes = rstn_value->limit.out_bytes;
371         effective_limit.in_bytes = rstn_value->limit.in_bytes;
372         effective_warn_limit.out_bytes = rstn_value->warn_limit.out_bytes;
373         effective_warn_limit.in_bytes = rstn_value->warn_limit.in_bytes;
374
375         if (rst_type == RST_SET) {
376                 /* TODO: Change this to runtime memory */
377                 table_counters_info info;
378
379                 memset(&info, 0, sizeof(table_counters_info));
380                 table_counters_get(rstn_value->restriction_id, &info);
381
382                 effective_limit.out_bytes -= info.sent_bytes;
383                 effective_limit.in_bytes -= info.rcv_bytes;
384                 effective_warn_limit.out_bytes -= info.sent_bytes;
385                 effective_warn_limit.in_bytes -= info.rcv_bytes;
386
387                 if (effective_limit.in_bytes < 0) {
388                         effective_limit.in_bytes = 0;
389                         rstn_value->in_limit_reached = TRUE;
390                 }
391
392                 if (effective_limit.out_bytes < 0) {
393                         effective_limit.out_bytes = 0;
394                         rstn_value->out_limit_reached = TRUE;
395                 }
396
397                 if (effective_warn_limit.in_bytes < 0)
398                         effective_warn_limit.in_bytes = 0;
399
400                 if (effective_warn_limit.out_bytes < 0)
401                         effective_warn_limit.out_bytes = 0;
402                  STC_LOGD("datausage [in: %llu, out: %llu]",
403                           info.rcv_bytes, info.sent_bytes);
404         }
405
406         STC_LOGD("rstn_id [%llu], effective limit [in: %llu, out: %llu], "
407                  "effective warn limit [in: %llu, out: %llu], "
408                  "datausage [in: %llu, out: %llu]",
409                  rstn_value->restriction_id,
410                  effective_limit.in_bytes, effective_limit.out_bytes,
411                  effective_warn_limit.in_bytes, effective_warn_limit.out_bytes);
412
413         send_net_restriction(rst_type,
414                              rstn_value->classid,
415                              0 /*quota_id*/,
416                              connection->type,
417                              effective_limit.out_bytes,
418                              effective_limit.in_bytes,
419                              effective_warn_limit.out_bytes,
420                              effective_warn_limit.in_bytes,
421                              connection->ifname);
422         switch (rst_type) {
423         case RST_SET:
424                 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
425                 rstn_value->in_limit_reached = FALSE;
426                 rstn_value->out_limit_reached = FALSE;
427                 break;
428         case RST_EXCLUDE:
429                 ;//Do Nothing
430                 break;
431         case RST_UNSET:
432                 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
433                 rstn_value->in_limit_reached = FALSE;
434                 rstn_value->out_limit_reached = FALSE;
435                 break;
436         default:
437                 ;//Do Nothing
438         }
439 }
440
441 static gboolean __remove_rstns_foreach_application(gpointer key,
442                                                    gpointer value,
443                                                    gpointer data)
444 {
445         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
446         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
447         gchar *app_id = (gchar *)data;
448
449         /* rstn rule is not for applications */
450         if (rstn_key->app_id == NULL)
451                 goto out;
452
453         /* rstn rule is not for this application */
454         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
455                 goto out;
456
457         /* rstn rule is already removed */
458         if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
459                 goto out;
460
461         /* remove restriction from system */
462         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
463
464         __print_rstn(rstn_key, rstn_value);
465 out:
466         return FALSE;
467 }
468
469 static void __remove_rstns_for_application(gchar *app_id)
470 {
471         g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
472                        app_id);
473 }
474
475 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
476 {
477         stc_error_e ret = STC_ERROR_NONE;
478         guint pid_count = 0;
479         stc_app_value_s *lookup;
480
481         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
482
483         lookup = __application_lookup(g_system->apps, app_key);
484         if (!lookup) {
485                 STC_LOGE("app_key not found");
486                 return STC_ERROR_NO_DATA;
487         }
488
489         g_tree_foreach(lookup->processes, __processes_tree_check_empty,
490                        &pid_count);
491
492         if (!pid_count) {
493                 /* remove nfacct rule for this classid */
494                 __remove_application_monitor((gpointer) app_key, lookup,
495                                              stc_get_default_connection());
496                 __remove_rstns_for_application(app_key->app_id);
497         }
498
499         if (!g_tree_remove(g_system->apps, app_key)) {
500                 ret = STC_ERROR_NO_DATA;
501                 STC_LOGE("key not found");
502         }
503
504         return ret;
505 }
506
507 static stc_error_e __close_contr_sock(stc_system_s *system)
508 {
509         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
510
511         /* close netlink socket for updating kernel counters */
512         if (g_system->contr_sock != -1) {
513                 close(g_system->contr_sock);
514                 g_system->contr_sock = -1;
515         }
516
517         if (g_system->contr_gsource_id != 0) {
518                 g_source_remove(g_system->contr_gsource_id);
519                 g_system->contr_gsource_id = 0;
520         }
521
522         return STC_ERROR_NONE;
523 }
524
525 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
526                                                       gpointer value,
527                                                       gpointer data)
528 {
529         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
530         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
531         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
532
533         if (context->counter->intend != NFACCT_COUNTER)
534                 goto try_next_callback;
535
536         if (rstn_value->classid != context->counter->classid)
537                 goto try_next_callback;
538
539         if (rstn_value->in_limit_reached == TRUE) {
540                 context->in_limit_reached = TRUE;
541                 goto try_next_callback;
542         }
543
544         if (rstn_value->out_limit_reached == TRUE) {
545                 context->out_limit_reached = TRUE;
546                 goto try_next_callback;
547         }
548
549         switch (context->counter->iotype) {
550         case NFACCT_COUNTER_IN:
551                 rstn_value->counter.in_bytes += context->bytes;
552
553                 /* block immediately */
554                 if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) {
555                         __del_iptables_in(context->counter);
556                         __add_iptables_in(context->counter);
557                         rstn_value->in_limit_reached = TRUE;
558                         //context->in_limit_reached = TRUE;
559                 }
560
561                 g_system->rstns_tree_updated = TRUE;
562                 __print_rstn(rstn_key, rstn_value);
563                 break;
564         case NFACCT_COUNTER_OUT:
565                 rstn_value->counter.out_bytes += context->bytes;
566
567                 /* block immediately */
568                 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) {
569                         __del_iptables_out(context->counter);
570                         __add_iptables_out(context->counter);
571                         rstn_value->out_limit_reached = TRUE;
572                         //context->out_limit_reached = TRUE;
573                 }
574
575                 g_system->rstns_tree_updated = TRUE;
576                 __print_rstn(rstn_key, rstn_value);
577                 break;
578         default:
579                 STC_LOGE("unknown iotype");
580         }
581
582
583 try_next_callback:
584         return FALSE;
585 }
586
587 static gboolean __update_app_statistics(gpointer key, gpointer value,
588                                         gpointer data)
589 {
590         stc_app_key_s *app_key = (stc_app_key_s *)key;
591         stc_app_value_s *app_value = (stc_app_value_s *)value;
592         time_t *touch_time = (time_t *)data;
593         stc_db_classid_iftype_key stat_key;
594         stc_db_app_stats stat;
595         char *default_ifname = stc_default_connection_get_ifname();
596
597         memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
598         memset(&stat, 0 , sizeof(stc_db_app_stats));
599
600         stat_key.classid = app_value->classid;
601         stat_key.iftype = stc_default_connection_get_type();
602         if (STC_IFACE_DATACALL == stat_key.iftype)
603                 stat_key.imsi = g_strdup("unknown");
604         else
605                 stat_key.imsi = g_strdup("noneimsi");
606         g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH);
607
608         stat.app_id = g_strdup(app_key->app_id);
609         stat.snd_count = app_value->counter.out_bytes;
610         stat.rcv_count = app_value->counter.in_bytes;
611         stat.delta_snd = 0;
612         stat.delta_rcv = 0;
613         stat.is_roaming = stc_default_connection_get_roaming();
614         stat.ground = STC_APP_STATE_UNKNOWN;
615
616         table_statistics_insert(&stat_key, &stat, *touch_time);
617
618         app_value->counter.out_bytes = 0;
619         app_value->counter.in_bytes = 0;
620
621         FREE(stat.app_id);
622         FREE(stat_key.imsi);
623         FREE(default_ifname);
624
625         return FALSE;
626 }
627
628 static gboolean __flush_apps_stats_to_database(gpointer user_data)
629 {
630         time_t current_time = time(0);
631
632         if (g_system->apps_tree_updated == FALSE)
633                 return G_SOURCE_REMOVE;
634
635         g_system->apps_tree_updated = FALSE;
636
637         if (g_system->apps)
638                 g_tree_foreach(g_system->apps,
639                                __update_app_statistics,
640                                &current_time);
641
642         STC_LOGI("Flushed app stats to database");
643         return G_SOURCE_REMOVE;
644 }
645
646 static gboolean __update_counter_statistics(gpointer key, gpointer value,
647                                             gpointer data)
648 {
649         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
650         table_counters_info info = {
651                 .restriction_id = rstn_value->restriction_id,
652                 .sent_bytes = rstn_value->counter.out_bytes,
653                 .rcv_bytes = rstn_value->counter.in_bytes
654         };
655
656         table_counters_update_counters(&info);
657
658         return FALSE;
659 }
660
661 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
662 {
663         time_t current_time = time(0);
664
665         if (g_system->rstns_tree_updated == FALSE)
666                 return G_SOURCE_REMOVE;
667
668         g_system->rstns_tree_updated = FALSE;
669
670         if (g_system->rstns)
671                 g_tree_foreach(g_system->rstns,
672                                __update_counter_statistics,
673                                &current_time);
674
675         STC_LOGI("Flushed rstns counters to database");
676         return G_SOURCE_REMOVE;
677 }
678
679 static gboolean __apps_counter_update_foreach_classid(gpointer key,
680                                                       gpointer value,
681                                                       gpointer data)
682 {
683         stc_app_key_s *app_key = (stc_app_key_s *)key;
684         stc_app_value_s *app_value = (stc_app_value_s *)value;
685         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
686
687         if (context->counter->intend != NFACCT_COUNTER)
688                 goto try_next_callback;
689
690         if (app_value->classid != context->counter->classid)
691                 goto try_next_callback;
692
693         switch (context->counter->iotype) {
694         case NFACCT_COUNTER_IN:
695                 app_value->data_usage.in_bytes += context->bytes;
696                 app_value->counter.in_bytes = context->bytes;
697                 g_system->apps_tree_updated = TRUE;
698
699                 __apps_tree_foreach_print(app_key, app_value, NULL);
700                 break;
701         case NFACCT_COUNTER_OUT:
702                 app_value->data_usage.out_bytes += context->bytes;
703                 app_value->counter.out_bytes = context->bytes;
704                 g_system->apps_tree_updated = TRUE;
705
706                 __apps_tree_foreach_print(app_key, app_value, NULL);
707                 break;
708         default:
709                 STC_LOGE("unknown iotype");
710         }
711
712 try_next_callback:
713         return FALSE;
714 }
715
716 static void __fill_nfacct_result(char *cnt_name, uint64_t bytes,
717                                  struct counter_arg *carg)
718 {
719         struct nfacct_rule counter = {
720                 .carg = carg,
721                 .name = {0},
722                 .ifname = {0},
723                 0
724         };
725
726         classid_bytes_context_s context = {
727                 .counter = &counter,
728                 .bytes = bytes,
729                 .in_limit_reached = FALSE,
730                 .out_limit_reached = FALSE
731         };
732
733         STC_LOGD("cnt_name %s", cnt_name);
734
735         if (!recreate_counter_by_name(cnt_name, &counter)) {
736                 STC_LOGE("Can't parse counter name %s", cnt_name);
737                 return;
738         }
739
740         STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %llu",
741                  context.counter->classid, context.counter->iftype,
742                  context.counter->iotype, context.counter->intend,
743                  context.counter->ifname, context.bytes);
744
745         if (g_system->rstns)
746                 g_tree_foreach(g_system->rstns,
747                                __rstn_counter_update_foreach_classid,
748                                &context);
749
750         if (g_system->apps)
751                 g_tree_foreach(g_system->apps,
752                                __apps_counter_update_foreach_classid,
753                                &context);
754 }
755
756 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
757                            void *user_data)
758 {
759         struct counter_arg *carg = user_data;
760         char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
761         if (carg->initiate) {
762                 /**
763                  * TODO: this will be used when daemon starts to update existing
764                  * counter data if present.
765                  *
766                  populate_counters(cnt_name, carg);
767                  */
768         } else {
769                 uint64_t *bytes_p =
770                         (uint64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
771                 int bytes = be64toh(*bytes_p);
772                 if (bytes) {
773                         ++carg->serialized_counters;
774                         __fill_nfacct_result(cnt_name, bytes, carg);
775                 }
776         }
777
778         return 0;
779 }
780
781 static int __post_fill_counters(void *user_data)
782 {
783         struct counter_arg *carg = user_data;
784
785         if (carg->initiate)
786                 carg->initiate = 0;
787
788         return 0;
789 }
790
791 static void __process_network_counter(struct genl *ans,
792                                       struct counter_arg *carg)
793 {
794         struct netlink_serialization_params ser_params = {
795                 .carg = carg,
796                 .ans = ans,
797                 .eval_attr = __fill_counters,
798                 .post_eval_attr = __post_fill_counters,
799         };
800
801         netlink_serialization_command *netlink =
802                 netlink_create_command(&ser_params);
803         if (!netlink) {
804                 STC_LOGE("Can not create command");
805                 return;
806         }
807
808         netlink->deserialize_answer(&(netlink->params));
809 }
810
811 static gboolean __process_contr_reply(GIOChannel *source,
812                                       GIOCondition condition,
813                                       gpointer user_data)
814 {
815         int sock = g_io_channel_unix_get_fd(source);
816         struct genl ans;
817         int ret;
818         stc_s *stc = stc_get_manager();
819
820         if ((condition & G_IO_ERR) ||
821             (condition & G_IO_HUP) ||
822             (condition & G_IO_NVAL)) {
823                 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
824
825                 __close_contr_sock(g_system);
826                 return FALSE;
827         }
828
829         if (stc == NULL) {
830                 STC_LOGE("Can't get stc data");
831                 goto out;
832         }
833
834         ret = read_netlink(sock,
835                            &ans, sizeof(struct genl));
836         STC_LOGD("Counter data received ret [%d]", ret);
837         if (ret == 0)
838                 goto out;
839
840         stc->carg->ans_len = ret;
841         __process_network_counter(&ans, stc->carg);
842
843         g_idle_add(__flush_apps_stats_to_database, NULL);
844         g_idle_add(__flush_rstns_counter_to_database, NULL);
845 out:
846         return TRUE;
847 }
848
849 static gboolean __update_contr_cb(void *user_data)
850 {
851         /* Here we just sent command, answer we receive in another callback */
852         stc_s *stc = stc_get_manager();
853         ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
854         if (!stc->carg) {
855                 stc->carg = MALLOC0(counter_arg_s, 1);
856                 stc->carg->sock = stc_monitor_get_counter_socket();
857         }
858
859         STC_LOGD("Get all counters");
860         nfacct_send_get_all(stc->carg);
861
862         /* we need to continue the timer */
863         return TRUE;
864 }
865 #if 0
866 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
867                                           gpointer data)
868 {
869         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
870         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
871
872         __print_rstn(rstn_key, rstn_value);
873         return FALSE;
874 }
875
876 static void __rstn_tree_printall(void)
877 {
878         g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
879 }
880 #endif
881 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
882                                         const stc_rstn_key_s *key)
883 {
884         stc_rstn_value_s *lookup;
885
886         ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
887
888         lookup = g_tree_lookup(rstns_tree, key);
889
890         return lookup;
891 }
892
893 static gboolean __remove_restriction(gpointer key, gpointer value,
894                                      gpointer data)
895 {
896         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
897         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
898
899         /* rstn rule is already removed */
900         if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
901                 return FALSE;
902
903         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
904         __print_rstn(rstn_key, rstn_value);
905         return FALSE;
906 }
907
908 static gboolean __add_restriction_debug(gpointer key, gpointer value,
909                                         gpointer data)
910 {
911         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
912         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
913
914         /* rstn rule is activated */
915         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
916                 return FALSE;
917
918         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
919                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
920         else
921                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
922
923         __print_rstn(rstn_key, rstn_value);
924
925         return FALSE;
926 }
927
928 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
929 {
930         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
931         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
932
933         /* rstn rule is activated */
934         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
935                 return FALSE;
936
937         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
938                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
939         else
940                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
941
942         return FALSE;
943 }
944
945 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
946 {
947         stc_rstn_value_s *lookup_value;
948
949         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
950
951         lookup_value = __rstn_lookup(g_system->rstns, key);
952         if (!lookup_value) {
953                 STC_LOGE("key not found");
954                 return STC_ERROR_NO_DATA;
955         }
956
957         __remove_restriction(key, lookup_value, NULL);
958
959         /* remove counter also */
960         table_counters_delete(lookup_value->restriction_id);
961
962         if (!g_tree_remove(g_system->rstns, key)) {
963                 STC_LOGD("key not found");
964                 return STC_ERROR_NO_DATA;
965         }
966
967         return STC_ERROR_NONE;
968 }
969
970 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
971                                    stc_rstn_value_s *value, gboolean debug)
972 {
973         stc_rstn_value_s *rstn_value;
974
975         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
976
977         rstn_value = __rstn_lookup(g_system->rstns, key);
978         if (!rstn_value) {
979                 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
980                 if (!rstn_key) {
981                         STC_LOGE("rstn_key allocation failed");
982                         return STC_ERROR_OUT_OF_MEMORY;
983                 }
984
985                 rstn_value = MALLOC0(stc_rstn_value_s, 1);
986                 if (!rstn_value) {
987                         STC_LOGE("rstn_value allocation failed");
988                         FREE(rstn_key);
989                         return STC_ERROR_OUT_OF_MEMORY;
990                 }
991
992                 rstn_key->app_id = g_strdup(key->app_id);
993                 rstn_key->ifname = g_strdup(key->ifname);
994                 rstn_key->imsi = g_strdup(key->imsi);
995                 rstn_key->iftype = key->iftype;
996                 rstn_key->roaming = key->roaming;
997
998                 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
999         }
1000
1001         rstn_value->restriction_id = value->restriction_id;
1002         rstn_value->rst_state = value->rst_state;
1003         rstn_value->classid = value->classid;
1004         rstn_value->limit.in_bytes = value->limit.in_bytes;
1005         rstn_value->limit.out_bytes = value->limit.out_bytes;
1006         rstn_value->warn_limit.in_bytes = value->warn_limit.in_bytes;
1007         rstn_value->warn_limit.out_bytes = value->warn_limit.out_bytes;
1008         rstn_value->counter.in_bytes = 0;
1009         rstn_value->counter.out_bytes = 0;
1010
1011         if (debug == TRUE)
1012                 __add_restriction_debug(key, rstn_value, NULL);
1013         else
1014                 __add_restriction(key, rstn_value, NULL);
1015
1016         return STC_ERROR_NONE;
1017 }
1018
1019 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1020                                             void *user_data)
1021 {
1022         stc_cb_ret_e ret = STC_CONTINUE;
1023
1024         stc_rstn_key_s key;
1025         stc_rstn_value_s value;
1026
1027         memset(&key, 0, sizeof(stc_rstn_key_s));
1028         memset(&value, 0, sizeof(stc_rstn_value_s));
1029
1030         key.app_id = g_strdup(info->app_id);
1031         key.ifname = g_strdup(info->ifname);
1032         key.imsi = g_strdup(info->imsi);
1033         key.iftype = info->iftype;
1034         key.roaming = info->roaming;
1035
1036         value.rst_state = info->rst_state;
1037         value.restriction_id = info->restriction_id;
1038
1039         if (value.rst_state != STC_RESTRICTION_EXCLUDED)
1040                 value.classid = get_classid_by_app_id(info->app_id ?
1041                                                       info->app_id :
1042                                                       STC_ALL_APP, TRUE);
1043         else
1044                 value.classid = STC_UNKNOWN_CLASSID;
1045
1046         value.limit.in_bytes = info->rcv_limit;
1047         value.limit.out_bytes = info->send_limit;
1048         value.warn_limit.in_bytes = info->rcv_warn_limit;
1049         value.warn_limit.out_bytes = info->send_warn_limit;
1050
1051         if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1052                 ret = STC_CANCEL;
1053
1054         FREE(key.app_id);
1055         FREE(key.ifname);
1056         FREE(key.imsi);
1057         return ret;
1058 }
1059
1060 static void __fill_restritions_list(void)
1061 {
1062         table_restrictions_foreach(__insert_restriction_cb, NULL);
1063         //__rstn_tree_printall();
1064 }
1065
1066 static gboolean __add_rstn_foreach_application(gpointer key,
1067                                                gpointer value,
1068                                                gpointer data)
1069 {
1070         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1071         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1072         gchar *app_id = (gchar *)data;
1073
1074         /* rstn rule is not for applications */
1075         if (rstn_key->app_id == NULL)
1076                 goto out;
1077
1078         /* rstn rule is not for this application */
1079         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1080                 goto out;
1081
1082         /* rstn rule is already applied */
1083         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1084                 goto out;
1085
1086         /* add restriction to system */
1087         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1088                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1089         else
1090                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1091
1092         __print_rstn(rstn_key, rstn_value);
1093 out:
1094         return FALSE;
1095 }
1096
1097 static void __add_rstns_for_application(gchar *app_id)
1098 {
1099         g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1100                        app_id);
1101 }
1102
1103 /* used only for (STC_ALL_APP/STC_BACKGROUND_APP_NAME) */
1104 static void __stc_monitor_add_application_by_app_id(const char *app_id)
1105 {
1106         stc_app_key_s app_key;
1107         stc_app_value_s app_value;
1108
1109         if (app_id == NULL)
1110                 return;
1111
1112         memset(&app_key, 0, sizeof(stc_app_key_s));
1113         memset(&app_value, 0, sizeof(stc_app_value_s));
1114
1115         app_key.pkg_id = g_strdup(app_id);
1116         app_key.app_id = g_strdup(app_id);
1117
1118         app_value.type = STC_APP_TYPE_NONE;
1119         app_value.processes = NULL;
1120         app_value.counter.in_bytes = 0;
1121         app_value.counter.out_bytes = 0;
1122
1123         stc_monitor_application_add(app_key, app_value);
1124
1125         FREE(app_key.pkg_id);
1126         FREE(app_key.app_id);
1127
1128         __apps_tree_printall();
1129 }
1130
1131 stc_error_e stc_monitor_init(void)
1132 {
1133         stc_system_s *system = MALLOC0(stc_system_s, 1);
1134         GIOChannel *gio = NULL;
1135
1136         ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1137
1138         /* creating monitored application tree */
1139         system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1140                                        __apps_tree_key_free,
1141                                        __apps_tree_value_free);
1142
1143         system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1144                                         __rstns_tree_key_free,
1145                                         __rstns_tree_value_free);
1146
1147         /* create netlink socket for updating kernel counters */
1148         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1149         if (!(system->contr_sock)) {
1150                 STC_LOGE("failed to open socket");
1151                 FREE(system);
1152                 return STC_ERROR_FAIL;
1153         }
1154
1155         gio = g_io_channel_unix_new(system->contr_sock);
1156         system->contr_gsource_id =
1157                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1158                                (GIOFunc) __process_contr_reply,
1159                                NULL);
1160         g_io_channel_unref(gio);
1161
1162         g_system = system;
1163
1164         /* create entry for STC_ALL_APP */
1165         __stc_monitor_add_application_by_app_id(STC_ALL_APP);
1166
1167         /* create entry for STC_BACKGROUND_APP_NAME */
1168         __stc_monitor_add_application_by_app_id(STC_BACKGROUND_APP_NAME);
1169
1170         /* create background cgroup */
1171         g_system->background_pids = NULL;
1172         create_net_background_cgroup(g_system->background_pids);
1173
1174         /* creating restriction rules tree */
1175         __update_contr_cb(NULL);
1176
1177         /* registering periodic kernel counters update callback */
1178         g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1179                                                          __update_contr_cb,
1180                                                          NULL);
1181         if (g_system->contr_timer_id == 0) {
1182                 STC_LOGE("Failed to register kernel counters update timer");
1183                 __close_contr_sock(g_system);
1184                 return STC_ERROR_FAIL;
1185         }
1186
1187         __fill_restritions_list();
1188
1189         return STC_ERROR_NONE;
1190 }
1191
1192 stc_error_e stc_monitor_deinit(void)
1193 {
1194         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1195
1196         /* close netlink socket for updating kernel counters */
1197         __close_contr_sock(g_system);
1198
1199         /* remove kernel counters update timer */
1200         if (g_system->contr_timer_id > 0) {
1201                 g_source_remove(g_system->contr_timer_id);
1202                 g_system->contr_timer_id = 0;
1203         }
1204
1205         /* free background pid list */
1206         g_slist_free(g_system->background_pids);
1207         g_system->background_pids = NULL;
1208
1209         /* destroy monitored application tree */
1210         g_tree_destroy(g_system->apps);
1211         g_system->apps = NULL;
1212
1213         /* destroy restriction rules tree */
1214         g_tree_destroy(g_system->rstns);
1215         g_system->rstns = NULL;
1216
1217         FREE(g_system);
1218
1219         return STC_ERROR_NONE;
1220 }
1221
1222 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1223                                         const stc_app_value_s app_value)
1224 {
1225         stc_error_e ret = STC_ERROR_NONE;
1226         stc_app_key_s *key;
1227         stc_app_value_s *value;
1228         stc_app_value_s *lookup;
1229
1230         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1231
1232         lookup = __application_lookup(g_system->apps, &app_key);
1233         if (lookup) {
1234                 STC_LOGD("app_key already present");
1235                 return STC_ERROR_NONE;
1236         }
1237
1238         key = MALLOC0(stc_app_key_s, 1);
1239         if (!key) {
1240                 STC_LOGE("key allocation failed");
1241                 return STC_ERROR_OUT_OF_MEMORY;
1242         }
1243
1244         value = MALLOC0(stc_app_value_s, 1);
1245         if (!value) {
1246                 STC_LOGE("value allocation failed");
1247                 FREE(key);
1248                 return STC_ERROR_OUT_OF_MEMORY;
1249         }
1250
1251         key->app_id = g_strdup(app_key.app_id);
1252         key->pkg_id = g_strdup(app_key.pkg_id);
1253
1254         value->type = app_value.type;
1255         value->counter.in_bytes = app_value.counter.in_bytes;
1256         value->counter.out_bytes = app_value.counter.out_bytes;
1257
1258         value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1259                                            __processes_tree_key_free,
1260                                            __processes_tree_value_free);
1261
1262         /* create cgroup and update classid */
1263         value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1264
1265         g_tree_insert(g_system->apps, key, value);
1266
1267         /* add nfacct rule for this classid */
1268         __add_application_monitor(key, value, stc_get_default_connection());
1269         __add_rstns_for_application(app_key.app_id);
1270
1271         return ret;
1272 }
1273
1274 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1275                                     const stc_process_key_s proc_key,
1276                                     const stc_process_value_s proc_value)
1277 {
1278         stc_error_e ret = STC_ERROR_NONE;
1279         stc_app_value_s *app_lookup;
1280         stc_process_key_s *key;
1281         stc_process_value_s *value;
1282         stc_process_value_s *proc_lookup;
1283
1284         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1285
1286         app_lookup = __application_lookup(g_system->apps, &app_key);
1287         if (!app_lookup) {
1288                 STC_LOGD("app_key not found");
1289                 return STC_ERROR_FAIL;
1290         }
1291
1292         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1293         if (proc_lookup) {
1294                 STC_LOGD("proc_key already present");
1295                 return STC_ERROR_NONE;
1296         }
1297
1298         key = MALLOC0(stc_process_key_s, 1);
1299         if (!key) {
1300                 STC_LOGE("key allocation failed");
1301                 return STC_ERROR_OUT_OF_MEMORY;
1302         }
1303
1304         value = MALLOC0(stc_process_value_s, 1);
1305         if (!value) {
1306                 STC_LOGE("value allocation failed");
1307                 FREE(key);
1308                 return STC_ERROR_OUT_OF_MEMORY;
1309         }
1310
1311         key->pid = proc_key.pid;
1312
1313         value->ground = proc_value.ground;
1314
1315         g_tree_insert(app_lookup->processes, key, value);
1316
1317         /* add pid to application cgroup */
1318         place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1319
1320         /* add pid to background cgroup if gound state is background */
1321         if (proc_value.ground == STC_APP_STATE_BACKGROUND)
1322                 add_pid_to_background_cgroup(proc_key.pid);
1323
1324         return ret;
1325 }
1326
1327 stc_error_e stc_monitor_process_remove(pid_t pid)
1328 {
1329         stc_error_e ret = STC_ERROR_NONE;
1330         stc_process_key_s proc_key = {
1331                 .pid = pid
1332         };
1333
1334         remove_pid_context_s context = {
1335                 .app_key = NULL,
1336                 .proc_key = &proc_key,
1337                 .entry_removed = FALSE,
1338         };
1339
1340         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1341
1342         /* remove pid from background cgroup */
1343         remove_pid_from_background_cgroup(pid);
1344
1345         g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1346                        &context);
1347
1348         if (context.entry_removed)
1349                 __application_remove_if_empty(context.app_key);
1350
1351         return ret;
1352 }
1353
1354 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1355                                               const stc_process_key_s proc_key,
1356                                               stc_app_state_e ground)
1357 {
1358         stc_error_e ret = STC_ERROR_NONE;
1359         stc_app_value_s *app_lookup;
1360         stc_process_value_s *proc_lookup;
1361
1362         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1363
1364         app_lookup = __application_lookup(g_system->apps, &app_key);
1365         if (!app_lookup) {
1366                 STC_LOGD("app_key not found");
1367                 return STC_ERROR_FAIL;
1368         }
1369
1370         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1371         if (!proc_lookup) {
1372                 STC_LOGD("proc_key not found");
1373                 return STC_ERROR_FAIL;
1374         }
1375
1376         if (proc_lookup->ground != ground)
1377                 proc_lookup->ground = ground;
1378
1379         if (ground == STC_APP_STATE_BACKGROUND)
1380                 add_pid_to_background_cgroup(proc_key.pid);
1381         else
1382                 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1383
1384         return ret;
1385 }
1386
1387 void stc_monitor_update_rstn_by_default_connection(void *data)
1388 {
1389         static default_connection_s old_connection;
1390         default_connection_s *new_connection = (default_connection_s *)data;
1391
1392         if (old_connection.path != NULL) {
1393                 if (g_system->apps)
1394                         g_tree_foreach(g_system->apps,
1395                                        __remove_application_monitor,
1396                                        (gpointer)&old_connection);
1397
1398                 if (g_system->rstns)
1399                         g_tree_foreach(g_system->rstns,
1400                                        __remove_restriction,
1401                                        (gpointer)&old_connection);
1402         }
1403
1404         FREE(old_connection.path);
1405         FREE(old_connection.ifname);
1406         old_connection.type = 0;
1407         old_connection.roaming = 0;
1408
1409         if (new_connection != NULL && new_connection->path != NULL) {
1410                 if (g_system->apps)
1411                         g_tree_foreach(g_system->apps,
1412                                        __add_application_monitor,
1413                                        (gpointer)new_connection);
1414
1415                 if (g_system->rstns)
1416                         g_tree_foreach(g_system->rstns, __add_restriction,
1417                                        NULL);
1418
1419                 old_connection.path = g_strdup(new_connection->path);
1420                 old_connection.ifname = g_strdup(new_connection->ifname);
1421                 old_connection.type = new_connection->type;
1422                 old_connection.roaming = new_connection->roaming;
1423         }
1424 }
1425
1426 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1427 {
1428         stc_error_e ret;
1429
1430         stc_rstn_key_s key;
1431         stc_rstn_value_s value;
1432
1433         memset(&key, 0, sizeof(stc_rstn_key_s));
1434         memset(&value, 0, sizeof(stc_rstn_value_s));
1435
1436         key.app_id = g_strdup(info->app_id);
1437         key.ifname = g_strdup(info->ifname);
1438         key.imsi = g_strdup(info->imsi);
1439         key.iftype = info->iftype;
1440         key.roaming = info->roaming;
1441
1442         value.rst_state = info->rst_state;
1443         value.restriction_id = info->restriction_id;
1444
1445         if (value.rst_state != STC_RESTRICTION_EXCLUDED)
1446                 value.classid = get_classid_by_app_id(info->app_id ?
1447                                                       info->app_id :
1448                                                       STC_ALL_APP, TRUE);
1449         else
1450                 value.classid = STC_UNKNOWN_CLASSID;
1451
1452         value.limit.in_bytes = info->rcv_limit;
1453         value.limit.out_bytes = info->send_limit;
1454         value.warn_limit.in_bytes = info->rcv_warn_limit;
1455         value.warn_limit.out_bytes = info->send_warn_limit;
1456
1457         ret = __rstn_tree_add(&key, &value, TRUE);
1458
1459         FREE(key.app_id);
1460         FREE(key.ifname);
1461         FREE(key.imsi);
1462         return ret;
1463 }
1464
1465 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1466 {
1467         stc_error_e ret;
1468
1469         stc_rstn_key_s key = {
1470                 .app_id = g_strdup(info->app_id),
1471                 .ifname = g_strdup(info->ifname),
1472                 .imsi = g_strdup(info->imsi),
1473                 .iftype = info->iftype,
1474                 .roaming = info->roaming,
1475         };
1476
1477         ret = __rstn_tree_remove(&key);
1478
1479         FREE(key.app_id);
1480         FREE(key.ifname);
1481         FREE(key.imsi);
1482         return ret;
1483 }
1484
1485 int stc_monitor_get_counter_socket(void)
1486 {
1487         return g_system->contr_sock;
1488 }