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