Merge "Removed unwanted function send_net_restriction() and its related code." into...
[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         switch (rst_type) {
420         case RST_SET:
421                 rstn_value->rst_state = STC_RESTRICTION_ACTIVATED;
422                 rstn_value->in_limit_reached = FALSE;
423                 rstn_value->out_limit_reached = FALSE;
424                 break;
425         case RST_EXCLUDE:
426                 ;//Do Nothing
427                 break;
428         case RST_UNSET:
429                 rstn_value->rst_state = STC_RESTRICTION_REMOVED;
430                 rstn_value->in_limit_reached = FALSE;
431                 rstn_value->out_limit_reached = FALSE;
432                 break;
433         default:
434                 ;//Do Nothing
435         }
436 }
437
438 static gboolean __remove_rstns_foreach_application(gpointer key,
439                                                    gpointer value,
440                                                    gpointer data)
441 {
442         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
443         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
444         gchar *app_id = (gchar *)data;
445
446         /* rstn rule is not for applications */
447         if (rstn_key->app_id == NULL)
448                 goto out;
449
450         /* rstn rule is not for this application */
451         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
452                 goto out;
453
454         /* rstn rule is already removed */
455         if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
456                 goto out;
457
458         /* remove restriction from system */
459         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
460
461         __print_rstn(rstn_key, rstn_value);
462 out:
463         return FALSE;
464 }
465
466 static void __remove_rstns_for_application(gchar *app_id)
467 {
468         g_tree_foreach(g_system->rstns, __remove_rstns_foreach_application,
469                        app_id);
470 }
471
472 static stc_error_e __application_remove_if_empty(const stc_app_key_s *app_key)
473 {
474         stc_error_e ret = STC_ERROR_NONE;
475         guint pid_count = 0;
476         stc_app_value_s *lookup;
477
478         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
479
480         lookup = __application_lookup(g_system->apps, app_key);
481         if (!lookup) {
482                 STC_LOGE("app_key not found");
483                 return STC_ERROR_NO_DATA;
484         }
485
486         g_tree_foreach(lookup->processes, __processes_tree_check_empty,
487                        &pid_count);
488
489         if (!pid_count) {
490                 /* remove nfacct rule for this classid */
491                 __remove_application_monitor((gpointer) app_key, lookup,
492                                              stc_get_default_connection());
493                 __remove_rstns_for_application(app_key->app_id);
494         }
495
496         if (!g_tree_remove(g_system->apps, app_key)) {
497                 ret = STC_ERROR_NO_DATA;
498                 STC_LOGE("key not found");
499         }
500
501         return ret;
502 }
503
504 static stc_error_e __close_contr_sock(stc_system_s *system)
505 {
506         ret_value_msg_if(system == NULL, STC_ERROR_INVALID_PARAMETER, "invalid parameter");
507
508         /* close netlink socket for updating kernel counters */
509         if (g_system->contr_sock != -1) {
510                 close(g_system->contr_sock);
511                 g_system->contr_sock = -1;
512         }
513
514         if (g_system->contr_gsource_id != 0) {
515                 g_source_remove(g_system->contr_gsource_id);
516                 g_system->contr_gsource_id = 0;
517         }
518
519         return STC_ERROR_NONE;
520 }
521
522 static gboolean __rstn_counter_update_foreach_classid(gpointer key,
523                                                       gpointer value,
524                                                       gpointer data)
525 {
526         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
527         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
528         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
529
530         if (context->counter->intend != NFACCT_COUNTER)
531                 goto try_next_callback;
532
533         if (rstn_value->classid != context->counter->classid)
534                 goto try_next_callback;
535
536         if (rstn_value->in_limit_reached == TRUE) {
537                 context->in_limit_reached = TRUE;
538                 goto try_next_callback;
539         }
540
541         if (rstn_value->out_limit_reached == TRUE) {
542                 context->out_limit_reached = TRUE;
543                 goto try_next_callback;
544         }
545
546         switch (context->counter->iotype) {
547         case NFACCT_COUNTER_IN:
548                 rstn_value->counter.in_bytes += context->bytes;
549
550                 if (rstn_value->counter.in_bytes >= rstn_value->warn_limit.in_bytes
551                     && rstn_value->warn_limit_crossed_notified == FALSE) {
552                         gboolean rv = FALSE;
553                         char iftype[MAX_INT_LENGTH];
554                         char byte[MAX_INT_LENGTH];
555                         stc_s *stc = (stc_s *)stc_get_manager();
556                         ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
557
558                         rv = stc_manager_dbus_emit_signal(stc->connection,
559                                                           STC_DBUS_SERVICE_RESTRICTION_PATH,
560                                                           STC_DBUS_INTERFACE_RESTRICTION,
561                                                           "WarnThresholdCrossed",
562                                                           g_variant_new("(s)", rstn_key->app_id));
563                         if (rv == TRUE)
564                                 rstn_value->warn_limit_crossed_notified = TRUE;
565
566                         snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
567                         snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.in_bytes);
568                         stc_send_warn_message_to_net_popup("warn threshold crossed",
569                                 "warning_noti", rstn_key->app_id, iftype, byte);
570                 }
571
572                 /* block immediately */
573                 if (rstn_value->counter.in_bytes >= rstn_value->limit.in_bytes) {
574                         context->counter->intend = NFACCT_BLOCK;
575                         __del_iptables_in(context->counter);
576                         __add_iptables_in(context->counter);
577                         context->counter->intend = NFACCT_COUNTER;
578                         rstn_value->in_limit_reached = TRUE;
579
580                         if (rstn_value->rstn_limit_crossed_notified == FALSE) {
581                                 gboolean rv = FALSE;
582                                 int ret = 0;
583                                 char iftype[MAX_INT_LENGTH];
584                                 char byte[MAX_INT_LENGTH];
585                                 stc_s *stc = (stc_s *)stc_get_manager();
586                                 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
587
588                                 rv = stc_manager_dbus_emit_signal(stc->connection,
589                                                                   STC_DBUS_SERVICE_RESTRICTION_PATH,
590                                                                   STC_DBUS_INTERFACE_RESTRICTION,
591                                                                   "RestrictionThresholdCrossed",
592                                                                   g_variant_new("(s)", rstn_key->app_id));
593                                 if (rv == TRUE)
594                                         rstn_value->rstn_limit_crossed_notified = TRUE;
595
596                                 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
597                                 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.in_bytes);
598                                 stc_send_restriction_message_to_net_popup("restriction threshold crossed",
599                                         "restriction_noti", rstn_key->app_id, iftype, byte);
600                         }
601                 }
602
603                 g_system->rstns_tree_updated = TRUE;
604                 __print_rstn(rstn_key, rstn_value);
605                 break;
606         case NFACCT_COUNTER_OUT:
607                 rstn_value->counter.out_bytes += context->bytes;
608
609                 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes
610                     && rstn_value->warn_limit_crossed_notified == FALSE) {
611                         gboolean rv = FALSE;
612                         char iftype[MAX_INT_LENGTH];
613                         char byte[MAX_INT_LENGTH];
614                         stc_s *stc = (stc_s *)stc_get_manager();
615                         ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
616
617                         rv = stc_manager_dbus_emit_signal(stc->connection,
618                                                           STC_DBUS_SERVICE_RESTRICTION_PATH,
619                                                           STC_DBUS_INTERFACE_RESTRICTION,
620                                                           "WarnThresholdCrossed",
621                                                           g_variant_new("(s)", rstn_key->app_id));
622                         if (rv == TRUE)
623                                 rstn_value->warn_limit_crossed_notified = TRUE;
624
625                         snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
626                         snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.out_bytes);
627                         stc_send_warn_message_to_net_popup("warn threshold crossed",
628                                 "warning_noti", rstn_key->app_id, iftype, byte);
629                 }
630
631                 /* block immediately */
632                 if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes) {
633                         context->counter->intend = NFACCT_BLOCK;
634                         __del_iptables_out(context->counter);
635                         __add_iptables_out(context->counter);
636                         context->counter->intend = NFACCT_COUNTER;
637                         rstn_value->out_limit_reached = TRUE;
638
639                         if (rstn_value->rstn_limit_crossed_notified == FALSE) {
640                                 gboolean rv = FALSE;
641                                 char iftype[MAX_INT_LENGTH];
642                                 char byte[MAX_INT_LENGTH];
643                                 stc_s *stc = (stc_s *)stc_get_manager();
644                                 ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
645
646                                 rv = stc_manager_dbus_emit_signal(stc->connection,
647                                                                   STC_DBUS_SERVICE_RESTRICTION_PATH,
648                                                                   STC_DBUS_INTERFACE_RESTRICTION,
649                                                                   "RestrictionThresholdCrossed",
650                                                                   g_variant_new("(s)", rstn_key->app_id));
651                                 if (rv == TRUE)
652                                         rstn_value->rstn_limit_crossed_notified = TRUE;
653
654                                 snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
655                                 snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.out_bytes);
656                                 stc_send_restriction_message_to_net_popup("restriction threshold crossed",
657                                         "restriction_noti", rstn_key->app_id, iftype, byte);
658                         }
659                 }
660
661                 g_system->rstns_tree_updated = TRUE;
662                 __print_rstn(rstn_key, rstn_value);
663                 break;
664         default:
665                 STC_LOGE("unknown iotype");
666         }
667
668
669 try_next_callback:
670         return FALSE;
671 }
672
673 static gboolean __update_app_statistics(gpointer key, gpointer value,
674                                         gpointer data)
675 {
676         stc_app_key_s *app_key = (stc_app_key_s *)key;
677         stc_app_value_s *app_value = (stc_app_value_s *)value;
678         time_t *touch_time = (time_t *)data;
679         stc_db_classid_iftype_key stat_key;
680         stc_db_app_stats stat;
681         char *default_ifname = stc_default_connection_get_ifname();
682
683         memset(&stat_key, 0, sizeof(stc_db_classid_iftype_key));
684         memset(&stat, 0 , sizeof(stc_db_app_stats));
685
686         stat_key.classid = app_value->classid;
687         stat_key.iftype = stc_default_connection_get_type();
688         if (STC_IFACE_DATACALL == stat_key.iftype)
689                 stat_key.imsi = g_strdup("unknown");
690         else
691                 stat_key.imsi = g_strdup("noneimsi");
692         g_strlcpy(stat_key.ifname, default_ifname, MAX_IFACE_LENGTH);
693
694         stat.app_id = g_strdup(app_key->app_id);
695         stat.snd_count = app_value->counter.out_bytes;
696         stat.rcv_count = app_value->counter.in_bytes;
697         stat.delta_snd = 0;
698         stat.delta_rcv = 0;
699         stat.is_roaming = stc_default_connection_get_roaming();
700         stat.ground = STC_APP_STATE_UNKNOWN;
701
702         table_statistics_insert(&stat_key, &stat, *touch_time);
703
704         app_value->counter.out_bytes = 0;
705         app_value->counter.in_bytes = 0;
706
707         FREE(stat.app_id);
708         FREE(stat_key.imsi);
709         FREE(default_ifname);
710
711         return FALSE;
712 }
713
714 static gboolean __flush_apps_stats_to_database(gpointer user_data)
715 {
716         time_t current_time = time(0);
717
718         if (g_system->apps_tree_updated == FALSE)
719                 return G_SOURCE_REMOVE;
720
721         g_system->apps_tree_updated = FALSE;
722
723         if (g_system->apps)
724                 g_tree_foreach(g_system->apps,
725                                __update_app_statistics,
726                                &current_time);
727
728         STC_LOGI("Flushed app stats to database");
729         return G_SOURCE_REMOVE;
730 }
731
732 static gboolean __update_counter_statistics(gpointer key, gpointer value,
733                                             gpointer data)
734 {
735         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
736         table_counters_info info = {
737                 .restriction_id = rstn_value->restriction_id,
738                 .sent_bytes = rstn_value->counter.out_bytes,
739                 .rcv_bytes = rstn_value->counter.in_bytes
740         };
741
742         table_counters_update_counters(&info);
743
744         return FALSE;
745 }
746
747 static gboolean __flush_rstns_counter_to_database(gpointer user_data)
748 {
749         time_t current_time = time(0);
750
751         if (g_system->rstns_tree_updated == FALSE)
752                 return G_SOURCE_REMOVE;
753
754         g_system->rstns_tree_updated = FALSE;
755
756         if (g_system->rstns)
757                 g_tree_foreach(g_system->rstns,
758                                __update_counter_statistics,
759                                &current_time);
760
761         STC_LOGI("Flushed rstns counters to database");
762         return G_SOURCE_REMOVE;
763 }
764
765 static gboolean __apps_counter_update_foreach_classid(gpointer key,
766                                                       gpointer value,
767                                                       gpointer data)
768 {
769         stc_app_key_s *app_key = (stc_app_key_s *)key;
770         stc_app_value_s *app_value = (stc_app_value_s *)value;
771         classid_bytes_context_s *context = (classid_bytes_context_s *)data;
772
773         if (context->counter->intend != NFACCT_COUNTER)
774                 goto try_next_callback;
775
776         if (app_value->classid != context->counter->classid)
777                 goto try_next_callback;
778
779         switch (context->counter->iotype) {
780         case NFACCT_COUNTER_IN:
781                 app_value->data_usage.in_bytes += context->bytes;
782                 app_value->counter.in_bytes = context->bytes;
783                 g_system->apps_tree_updated = TRUE;
784
785                 __apps_tree_foreach_print(app_key, app_value, NULL);
786                 break;
787         case NFACCT_COUNTER_OUT:
788                 app_value->data_usage.out_bytes += context->bytes;
789                 app_value->counter.out_bytes = context->bytes;
790                 g_system->apps_tree_updated = TRUE;
791
792                 __apps_tree_foreach_print(app_key, app_value, NULL);
793                 break;
794         default:
795                 STC_LOGE("unknown iotype");
796         }
797
798 try_next_callback:
799         return FALSE;
800 }
801
802 static void __fill_nfacct_result(char *cnt_name, int64_t bytes,
803                                  struct counter_arg *carg)
804 {
805         struct nfacct_rule counter = {
806                 .carg = carg,
807                 .name = {0},
808                 .ifname = {0},
809                 0
810         };
811
812         classid_bytes_context_s context = {
813                 .counter = &counter,
814                 .bytes = bytes,
815                 .in_limit_reached = FALSE,
816                 .out_limit_reached = FALSE
817         };
818
819         STC_LOGD("cnt_name %s", cnt_name);
820
821         if (!recreate_counter_by_name(cnt_name, &counter)) {
822                 STC_LOGE("Can't parse counter name %s", cnt_name);
823                 return;
824         }
825
826         STC_LOGI("classid %lu, iftype %u, iotype %d, intend %d, ifname %s, bytes %lld",
827                  context.counter->classid, context.counter->iftype,
828                  context.counter->iotype, context.counter->intend,
829                  context.counter->ifname, context.bytes);
830
831         if (g_system->rstns)
832                 g_tree_foreach(g_system->rstns,
833                                __rstn_counter_update_foreach_classid,
834                                &context);
835
836         if (g_system->apps)
837                 g_tree_foreach(g_system->apps,
838                                __apps_counter_update_foreach_classid,
839                                &context);
840 }
841
842 static int __fill_counters(struct rtattr *attr_list[__NFACCT_MAX],
843                            void *user_data)
844 {
845         struct counter_arg *carg = user_data;
846         char *cnt_name = (char *)RTA_DATA(attr_list[NFACCT_NAME]);
847         if (carg->initiate) {
848                 /**
849                  * TODO: this will be used when daemon starts to update existing
850                  * counter data if present.
851                  *
852                  populate_counters(cnt_name, carg);
853                  */
854         } else {
855                 int64_t *bytes_p =
856                         (int64_t *)RTA_DATA(attr_list[NFACCT_BYTES]);
857                 int bytes = be64toh(*bytes_p);
858                 if (bytes) {
859                         ++carg->serialized_counters;
860                         __fill_nfacct_result(cnt_name, bytes, carg);
861                 }
862         }
863
864         return 0;
865 }
866
867 static int __post_fill_counters(void *user_data)
868 {
869         struct counter_arg *carg = user_data;
870
871         if (carg->initiate)
872                 carg->initiate = 0;
873
874         return 0;
875 }
876
877 static void __process_network_counter(struct genl *ans,
878                                       struct counter_arg *carg)
879 {
880         struct netlink_serialization_params ser_params = {
881                 .carg = carg,
882                 .ans = ans,
883                 .eval_attr = __fill_counters,
884                 .post_eval_attr = __post_fill_counters,
885         };
886
887         netlink_serialization_command *netlink =
888                 netlink_create_command(&ser_params);
889         if (!netlink) {
890                 STC_LOGE("Can not create command");
891                 return;
892         }
893
894         netlink->deserialize_answer(&(netlink->params));
895 }
896
897 static gboolean __process_contr_reply(GIOChannel *source,
898                                       GIOCondition condition,
899                                       gpointer user_data)
900 {
901         int sock = g_io_channel_unix_get_fd(source);
902         struct genl ans;
903         int ret;
904         stc_s *stc = stc_get_manager();
905
906         if ((condition & G_IO_ERR) ||
907             (condition & G_IO_HUP) ||
908             (condition & G_IO_NVAL)) {
909                 /* G_IO_ERR/G_IO_HUP/G_IO_NVAL received */
910
911                 __close_contr_sock(g_system);
912                 return FALSE;
913         }
914
915         if (stc == NULL) {
916                 STC_LOGE("Can't get stc data");
917                 goto out;
918         }
919
920         ret = read_netlink(sock,
921                            &ans, sizeof(struct genl));
922         STC_LOGD("Counter data received ret [%d]", ret);
923         if (ret == 0)
924                 goto out;
925
926         stc->carg->ans_len = ret;
927         __process_network_counter(&ans, stc->carg);
928
929         g_idle_add(__flush_apps_stats_to_database, NULL);
930         g_idle_add(__flush_rstns_counter_to_database, NULL);
931 out:
932         return TRUE;
933 }
934
935 static gboolean __update_contr_cb(void *user_data)
936 {
937         /* Here we just sent command, answer we receive in another callback */
938         stc_s *stc = stc_get_manager();
939         ret_value_msg_if(stc == NULL, STC_ERROR_FAIL, "Can't get stc data");
940         if (!stc->carg) {
941                 stc->carg = MALLOC0(counter_arg_s, 1);
942                 stc->carg->sock = stc_monitor_get_counter_socket();
943         }
944
945         STC_LOGD("Get all counters");
946         nfacct_send_get_all(stc->carg);
947
948         /* we need to continue the timer */
949         return TRUE;
950 }
951 #if 0
952 static gboolean __rstn_tree_foreach_print(gpointer key, gpointer value,
953                                           gpointer data)
954 {
955         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
956         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
957
958         __print_rstn(rstn_key, rstn_value);
959         return FALSE;
960 }
961
962 static void __rstn_tree_printall(void)
963 {
964         g_tree_foreach(g_system->rstns, __rstn_tree_foreach_print, NULL);
965 }
966 #endif
967 static stc_rstn_value_s * __rstn_lookup(GTree *rstns_tree,
968                                         const stc_rstn_key_s *key)
969 {
970         stc_rstn_value_s *lookup;
971
972         ret_value_msg_if(rstns_tree == NULL, NULL, "rstns_tree is null!");
973
974         lookup = g_tree_lookup(rstns_tree, key);
975
976         return lookup;
977 }
978
979 static gboolean __remove_restriction(gpointer key, gpointer value,
980                                      gpointer data)
981 {
982         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
983         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
984
985         /* rstn rule is already removed */
986         if (rstn_value->rst_state == STC_RESTRICTION_REMOVED)
987                 return FALSE;
988
989         __process_restriction(RST_UNSET, rstn_key, rstn_value, data);
990         __print_rstn(rstn_key, rstn_value);
991         return FALSE;
992 }
993
994 static gboolean __add_restriction_debug(gpointer key, gpointer value,
995                                         gpointer data)
996 {
997         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
998         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
999
1000         /* rstn rule is activated */
1001         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1002                 return FALSE;
1003
1004         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1005                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1006         else
1007                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1008
1009         __print_rstn(rstn_key, rstn_value);
1010
1011         return FALSE;
1012 }
1013
1014 static gboolean __add_restriction(gpointer key, gpointer value, gpointer data)
1015 {
1016         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1017         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1018
1019         /* rstn rule is activated */
1020         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1021                 return FALSE;
1022
1023         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1024                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1025         else
1026                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1027
1028         return FALSE;
1029 }
1030
1031 static stc_error_e __rstn_tree_remove(stc_rstn_key_s *key)
1032 {
1033         stc_rstn_value_s *lookup_value;
1034
1035         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1036
1037         lookup_value = __rstn_lookup(g_system->rstns, key);
1038         if (!lookup_value) {
1039                 STC_LOGE("key not found");
1040                 return STC_ERROR_NO_DATA;
1041         }
1042
1043         __remove_restriction(key, lookup_value, NULL);
1044
1045         /* remove counter also */
1046         table_counters_delete(lookup_value->restriction_id);
1047
1048         if (!g_tree_remove(g_system->rstns, key)) {
1049                 STC_LOGD("key not found");
1050                 return STC_ERROR_NO_DATA;
1051         }
1052
1053         return STC_ERROR_NONE;
1054 }
1055
1056 static stc_error_e __rstn_tree_add(stc_rstn_key_s *key,
1057                                    stc_rstn_value_s *value, gboolean debug)
1058 {
1059         stc_rstn_value_s *rstn_value;
1060
1061         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1062
1063         rstn_value = __rstn_lookup(g_system->rstns, key);
1064         if (!rstn_value) {
1065                 stc_rstn_key_s *rstn_key = MALLOC0(stc_rstn_key_s, 1);
1066                 if (!rstn_key) {
1067                         STC_LOGE("rstn_key allocation failed");
1068                         return STC_ERROR_OUT_OF_MEMORY;
1069                 }
1070
1071                 rstn_value = MALLOC0(stc_rstn_value_s, 1);
1072                 if (!rstn_value) {
1073                         STC_LOGE("rstn_value allocation failed");
1074                         FREE(rstn_key);
1075                         return STC_ERROR_OUT_OF_MEMORY;
1076                 }
1077
1078                 rstn_key->app_id = g_strdup(key->app_id);
1079                 rstn_key->ifname = g_strdup(key->ifname);
1080                 rstn_key->imsi = g_strdup(key->imsi);
1081                 rstn_key->iftype = key->iftype;
1082                 rstn_key->roaming = key->roaming;
1083
1084                 g_tree_insert(g_system->rstns, rstn_key, rstn_value);
1085         }
1086
1087         rstn_value->restriction_id = value->restriction_id;
1088         rstn_value->rst_state = value->rst_state;
1089         rstn_value->classid = value->classid;
1090         rstn_value->limit.in_bytes = value->limit.in_bytes;
1091         rstn_value->limit.out_bytes = value->limit.out_bytes;
1092         rstn_value->warn_limit.in_bytes = value->warn_limit.in_bytes;
1093         rstn_value->warn_limit.out_bytes = value->warn_limit.out_bytes;
1094         rstn_value->counter.in_bytes = 0;
1095         rstn_value->counter.out_bytes = 0;
1096         rstn_value->warn_limit_crossed_notified = FALSE;
1097         rstn_value->rstn_limit_crossed_notified = FALSE;
1098
1099         if (debug == TRUE)
1100                 __add_restriction_debug(key, rstn_value, NULL);
1101         else
1102                 __add_restriction(key, rstn_value, NULL);
1103
1104         return STC_ERROR_NONE;
1105 }
1106
1107 static stc_cb_ret_e __insert_restriction_cb(const table_restrictions_info *info,
1108                                             void *user_data)
1109 {
1110         stc_cb_ret_e ret = STC_CONTINUE;
1111
1112         stc_rstn_key_s key;
1113         stc_rstn_value_s value;
1114
1115         memset(&key, 0, sizeof(stc_rstn_key_s));
1116         memset(&value, 0, sizeof(stc_rstn_value_s));
1117
1118         key.app_id = g_strdup(info->app_id);
1119         key.ifname = g_strdup(info->ifname);
1120         key.imsi = g_strdup(info->imsi);
1121         key.iftype = info->iftype;
1122         key.roaming = info->roaming;
1123
1124         value.rst_state = info->rst_state;
1125         value.restriction_id = info->restriction_id;
1126
1127         if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1128                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1129         else
1130                 value.classid = STC_UNKNOWN_CLASSID;
1131
1132         value.limit.in_bytes = info->rcv_limit;
1133         value.limit.out_bytes = info->send_limit;
1134         value.warn_limit.in_bytes = info->rcv_warn_limit;
1135         value.warn_limit.out_bytes = info->send_warn_limit;
1136
1137         if (__rstn_tree_add(&key, &value, FALSE) != STC_ERROR_NONE)
1138                 ret = STC_CANCEL;
1139
1140         FREE(key.app_id);
1141         FREE(key.ifname);
1142         FREE(key.imsi);
1143         return ret;
1144 }
1145
1146 static void __fill_restritions_list(void)
1147 {
1148         table_restrictions_foreach(__insert_restriction_cb, NULL);
1149         //__rstn_tree_printall();
1150 }
1151
1152 static gboolean __add_rstn_foreach_application(gpointer key,
1153                                                gpointer value,
1154                                                gpointer data)
1155 {
1156         stc_rstn_key_s *rstn_key = (stc_rstn_key_s *)key;
1157         stc_rstn_value_s *rstn_value = (stc_rstn_value_s *)value;
1158         gchar *app_id = (gchar *)data;
1159
1160         /* rstn rule is not for applications */
1161         if (rstn_key->app_id == NULL)
1162                 goto out;
1163
1164         /* rstn rule is not for this application */
1165         if (g_strcmp0(rstn_key->app_id, app_id) != 0)
1166                 goto out;
1167
1168         /* rstn rule is already applied */
1169         if (rstn_value->rst_state == STC_RESTRICTION_ACTIVATED)
1170                 goto out;
1171
1172         /* add restriction to system */
1173         if (rstn_value->rst_state == STC_RESTRICTION_EXCLUDED)
1174                 __process_restriction(RST_EXCLUDE, rstn_key, rstn_value, data);
1175         else
1176                 __process_restriction(RST_SET, rstn_key, rstn_value, data);
1177
1178         __print_rstn(rstn_key, rstn_value);
1179 out:
1180         return FALSE;
1181 }
1182
1183 static void __add_rstns_for_application(gchar *app_id)
1184 {
1185         g_tree_foreach(g_system->rstns, __add_rstn_foreach_application,
1186                        app_id);
1187 }
1188
1189 stc_error_e stc_monitor_init(void)
1190 {
1191         stc_system_s *system = MALLOC0(stc_system_s, 1);
1192         GIOChannel *gio = NULL;
1193
1194         ret_value_msg_if(system == NULL, STC_ERROR_OUT_OF_MEMORY, "stc_system_s malloc fail!");
1195
1196         /* initializing cgroups */
1197         cgroup_init();
1198
1199         /* creating monitored application tree */
1200         system->apps = g_tree_new_full(__apps_tree_key_compare, NULL,
1201                                        __apps_tree_key_free,
1202                                        __apps_tree_value_free);
1203
1204         system->rstns = g_tree_new_full(__rstns_tree_key_compare, NULL,
1205                                         __rstns_tree_key_free,
1206                                         __rstns_tree_value_free);
1207
1208         /* create netlink socket for updating kernel counters */
1209         system->contr_sock = create_netlink(NETLINK_NETFILTER, 0);
1210         if (!(system->contr_sock)) {
1211                 STC_LOGE("failed to open socket");
1212                 FREE(system);
1213                 return STC_ERROR_FAIL;
1214         }
1215
1216         gio = g_io_channel_unix_new(system->contr_sock);
1217         system->contr_gsource_id =
1218                 g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP,
1219                                (GIOFunc) __process_contr_reply,
1220                                NULL);
1221         g_io_channel_unref(gio);
1222
1223         g_system = system;
1224
1225         /* creating restriction rules tree */
1226         __update_contr_cb(NULL);
1227
1228         /* registering periodic kernel counters update callback */
1229         g_system->contr_timer_id = g_timeout_add_seconds(CONTR_TIMER_INTERVAL,
1230                                                          __update_contr_cb,
1231                                                          NULL);
1232         if (g_system->contr_timer_id == 0) {
1233                 STC_LOGE("Failed to register kernel counters update timer");
1234                 __close_contr_sock(g_system);
1235                 return STC_ERROR_FAIL;
1236         }
1237
1238         __fill_restritions_list();
1239
1240         return STC_ERROR_NONE;
1241 }
1242
1243 stc_error_e stc_monitor_deinit(void)
1244 {
1245         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1246
1247         /* close netlink socket for updating kernel counters */
1248         __close_contr_sock(g_system);
1249
1250         /* remove kernel counters update timer */
1251         if (g_system->contr_timer_id > 0) {
1252                 g_source_remove(g_system->contr_timer_id);
1253                 g_system->contr_timer_id = 0;
1254         }
1255
1256         /* destroy monitored application tree */
1257         g_tree_destroy(g_system->apps);
1258         g_system->apps = NULL;
1259
1260         /* destroy restriction rules tree */
1261         g_tree_destroy(g_system->rstns);
1262         g_system->rstns = NULL;
1263
1264         FREE(g_system);
1265
1266         return STC_ERROR_NONE;
1267 }
1268
1269 stc_error_e stc_monitor_application_add(const stc_app_key_s app_key,
1270                                         const stc_app_value_s app_value)
1271 {
1272         stc_error_e ret = STC_ERROR_NONE;
1273         stc_app_key_s *key;
1274         stc_app_value_s *value;
1275         stc_app_value_s *lookup;
1276
1277         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1278
1279         lookup = __application_lookup(g_system->apps, &app_key);
1280         if (lookup) {
1281                 STC_LOGD("app_key already present");
1282                 return STC_ERROR_NONE;
1283         }
1284
1285         key = MALLOC0(stc_app_key_s, 1);
1286         if (!key) {
1287                 STC_LOGE("key allocation failed");
1288                 return STC_ERROR_OUT_OF_MEMORY;
1289         }
1290
1291         value = MALLOC0(stc_app_value_s, 1);
1292         if (!value) {
1293                 STC_LOGE("value allocation failed");
1294                 FREE(key);
1295                 return STC_ERROR_OUT_OF_MEMORY;
1296         }
1297
1298         key->app_id = g_strdup(app_key.app_id);
1299         key->pkg_id = g_strdup(app_key.pkg_id);
1300
1301         value->type = app_value.type;
1302         value->counter.in_bytes = app_value.counter.in_bytes;
1303         value->counter.out_bytes = app_value.counter.out_bytes;
1304
1305         value->processes = g_tree_new_full(__processes_tree_key_compare, NULL,
1306                                            __processes_tree_key_free,
1307                                            __processes_tree_value_free);
1308
1309         /* create cgroup and update classid */
1310         value->classid = get_classid_by_app_id(app_key.app_id, TRUE);
1311
1312         g_tree_insert(g_system->apps, key, value);
1313
1314         /* add nfacct rule for this classid */
1315         __add_application_monitor(key, value, stc_get_default_connection());
1316         __add_rstns_for_application(app_key.app_id);
1317
1318         return ret;
1319 }
1320
1321 stc_error_e stc_monitor_process_add(const stc_app_key_s app_key,
1322                                     const stc_process_key_s proc_key,
1323                                     const stc_process_value_s proc_value)
1324 {
1325         stc_error_e ret = STC_ERROR_NONE;
1326         stc_app_value_s *app_lookup;
1327         stc_process_key_s *key;
1328         stc_process_value_s *value;
1329         stc_process_value_s *proc_lookup;
1330
1331         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1332
1333         app_lookup = __application_lookup(g_system->apps, &app_key);
1334         if (!app_lookup) {
1335                 STC_LOGD("app_key not found");
1336                 return STC_ERROR_FAIL;
1337         }
1338
1339         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1340         if (proc_lookup) {
1341                 STC_LOGD("proc_key already present");
1342                 return STC_ERROR_NONE;
1343         }
1344
1345         key = MALLOC0(stc_process_key_s, 1);
1346         if (!key) {
1347                 STC_LOGE("key allocation failed");
1348                 return STC_ERROR_OUT_OF_MEMORY;
1349         }
1350
1351         value = MALLOC0(stc_process_value_s, 1);
1352         if (!value) {
1353                 STC_LOGE("value allocation failed");
1354                 FREE(key);
1355                 return STC_ERROR_OUT_OF_MEMORY;
1356         }
1357
1358         key->pid = proc_key.pid;
1359
1360         value->ground = proc_value.ground;
1361
1362         g_tree_insert(app_lookup->processes, key, value);
1363
1364         /* add pid to application cgroup */
1365         place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1366
1367         return ret;
1368 }
1369
1370 stc_error_e stc_monitor_process_remove(pid_t pid)
1371 {
1372         stc_error_e ret = STC_ERROR_NONE;
1373         stc_process_key_s proc_key = {
1374                 .pid = pid
1375         };
1376
1377         remove_pid_context_s context = {
1378                 .app_key = NULL,
1379                 .proc_key = &proc_key,
1380                 .entry_removed = FALSE,
1381         };
1382
1383         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1384
1385         g_tree_foreach(g_system->apps, __apps_tree_foreach_remove_pid,
1386                        &context);
1387
1388         if (context.entry_removed)
1389                 __application_remove_if_empty(context.app_key);
1390
1391         return ret;
1392 }
1393
1394 stc_error_e stc_monitor_process_update_ground(const stc_app_key_s app_key,
1395                                               const stc_process_key_s proc_key,
1396                                               stc_app_state_e ground)
1397 {
1398         stc_error_e ret = STC_ERROR_NONE;
1399         stc_app_value_s *app_lookup;
1400         stc_process_value_s *proc_lookup;
1401
1402         ret_value_msg_if(g_system == NULL, STC_ERROR_FAIL, "stc monitor not initialized!");
1403
1404         app_lookup = __application_lookup(g_system->apps, &app_key);
1405         if (!app_lookup) {
1406                 STC_LOGD("app_key not found");
1407                 return STC_ERROR_FAIL;
1408         }
1409
1410         proc_lookup = __process_lookup(app_lookup->processes, &proc_key);
1411         if (!proc_lookup) {
1412                 STC_LOGD("proc_key not found");
1413                 return STC_ERROR_FAIL;
1414         }
1415
1416         if (proc_lookup->ground != ground)
1417                 proc_lookup->ground = ground;
1418
1419         if (ground == STC_APP_STATE_BACKGROUND) {
1420                 char *background_app_id = g_strconcat(app_key.app_id,
1421                                                      STC_BACKGROUND_APP_SUFFIX,
1422                                                      NULL);
1423                 place_pids_to_net_cgroup(proc_key.pid, background_app_id);
1424                 g_free(background_app_id);
1425         } else {
1426                 place_pids_to_net_cgroup(proc_key.pid, app_key.app_id);
1427         }
1428
1429         return ret;
1430 }
1431
1432 void stc_monitor_update_rstn_by_default_connection(void *data)
1433 {
1434         static default_connection_s old_connection;
1435         default_connection_s *new_connection = (default_connection_s *)data;
1436
1437         if (old_connection.path != NULL) {
1438                 if (g_system->apps)
1439                         g_tree_foreach(g_system->apps,
1440                                        __remove_application_monitor,
1441                                        (gpointer)&old_connection);
1442
1443                 if (g_system->rstns)
1444                         g_tree_foreach(g_system->rstns,
1445                                        __remove_restriction,
1446                                        (gpointer)&old_connection);
1447         }
1448
1449         FREE(old_connection.path);
1450         FREE(old_connection.ifname);
1451         old_connection.type = 0;
1452         old_connection.roaming = 0;
1453
1454         if (new_connection != NULL && new_connection->path != NULL) {
1455                 if (g_system->apps)
1456                         g_tree_foreach(g_system->apps,
1457                                        __add_application_monitor,
1458                                        (gpointer)new_connection);
1459
1460                 if (g_system->rstns)
1461                         g_tree_foreach(g_system->rstns, __add_restriction,
1462                                        NULL);
1463
1464                 old_connection.path = g_strdup(new_connection->path);
1465                 old_connection.ifname = g_strdup(new_connection->ifname);
1466                 old_connection.type = new_connection->type;
1467                 old_connection.roaming = new_connection->roaming;
1468         }
1469 }
1470
1471 stc_error_e stc_monitor_rstns_tree_add(const table_restrictions_info *info)
1472 {
1473         stc_error_e ret;
1474
1475         stc_rstn_key_s key;
1476         stc_rstn_value_s value;
1477
1478         memset(&key, 0, sizeof(stc_rstn_key_s));
1479         memset(&value, 0, sizeof(stc_rstn_value_s));
1480
1481         key.app_id = g_strdup(info->app_id);
1482         key.ifname = g_strdup(info->ifname);
1483         key.imsi = g_strdup(info->imsi);
1484         key.iftype = info->iftype;
1485         key.roaming = info->roaming;
1486
1487         value.rst_state = info->rst_state;
1488         value.restriction_id = info->restriction_id;
1489
1490         if (value.rst_state != STC_RESTRICTION_EXCLUDED && info->app_id)
1491                 value.classid = get_classid_by_app_id(info->app_id, TRUE);
1492         else
1493                 value.classid = STC_UNKNOWN_CLASSID;
1494
1495         value.limit.in_bytes = info->rcv_limit;
1496         value.limit.out_bytes = info->send_limit;
1497         value.warn_limit.in_bytes = info->rcv_warn_limit;
1498         value.warn_limit.out_bytes = info->send_warn_limit;
1499
1500         ret = __rstn_tree_add(&key, &value, TRUE);
1501
1502         FREE(key.app_id);
1503         FREE(key.ifname);
1504         FREE(key.imsi);
1505         return ret;
1506 }
1507
1508 stc_error_e stc_monitor_rstns_tree_remove(const table_restrictions_info *info)
1509 {
1510         stc_error_e ret;
1511
1512         stc_rstn_key_s key = {
1513                 .app_id = g_strdup(info->app_id),
1514                 .ifname = g_strdup(info->ifname),
1515                 .imsi = g_strdup(info->imsi),
1516                 .iftype = info->iftype,
1517                 .roaming = info->roaming,
1518         };
1519
1520         ret = __rstn_tree_remove(&key);
1521
1522         FREE(key.app_id);
1523         FREE(key.ifname);
1524         FREE(key.imsi);
1525         return ret;
1526 }
1527
1528 int stc_monitor_get_counter_socket(void)
1529 {
1530         return g_system->contr_sock;
1531 }