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