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