Fix VPN issue when setting up host route
[framework/connectivity/connman.git] / src / counter.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <gdbus.h>
27
28 #include "connman.h"
29
30 static DBusConnection *connection;
31
32 static GHashTable *stats_table;
33 static GHashTable *counter_table;
34 static GHashTable *owner_mapping;
35
36 struct connman_stats {
37         char *interface;
38         unsigned int rx_bytes;
39         unsigned int tx_bytes;
40 };
41
42 struct connman_counter {
43         char *owner;
44         char *path;
45         guint timeout;
46         guint watch;
47 };
48
49 static void remove_stats(gpointer user_data)
50 {
51         struct connman_stats *stats = user_data;
52
53         g_free(stats->interface);
54         g_free(stats);
55 }
56
57 static void remove_counter(gpointer user_data)
58 {
59         struct connman_counter *counter = user_data;
60
61         DBG("owner %s path %s", counter->owner, counter->path);
62
63         if (counter->watch > 0)
64                 g_dbus_remove_watch(connection, counter->watch);
65
66         if (counter->timeout > 0)
67                 g_source_remove(counter->timeout);
68
69         g_free(counter->owner);
70         g_free(counter->path);
71         g_free(counter);
72 }
73
74 static void owner_disconnect(DBusConnection *connection, void *user_data)
75 {
76         struct connman_counter *counter = user_data;
77
78         DBG("owner %s path %s", counter->owner, counter->path);
79
80         g_hash_table_remove(owner_mapping, counter->owner);
81         g_hash_table_remove(counter_table, counter->path);
82 }
83
84 static gboolean counter_timeout(gpointer user_data)
85 {
86         struct connman_counter *counter = user_data;
87
88         DBG("owner %s path %s", counter->owner, counter->path);
89
90         __connman_rtnl_request_update();
91
92         return TRUE;
93 }
94
95 int __connman_counter_register(const char *owner, const char *path,
96                                                 unsigned int interval)
97 {
98         struct connman_counter *counter;
99
100         DBG("owner %s path %s interval %u", owner, path, interval);
101
102         counter = g_hash_table_lookup(counter_table, path);
103         if (counter != NULL)
104                 return -EEXIST;
105
106         counter = g_try_new0(struct connman_counter, 1);
107         if (counter == NULL)
108                 return -ENOMEM;
109
110         counter->owner = g_strdup(owner);
111         counter->path = g_strdup(path);
112
113         g_hash_table_replace(counter_table, counter->path, counter);
114         g_hash_table_replace(owner_mapping, counter->owner, counter);
115
116         if (interval > 0)
117                 counter->timeout = g_timeout_add_seconds(interval,
118                                                 counter_timeout, counter);
119
120         counter->watch = g_dbus_add_disconnect_watch(connection, owner,
121                                         owner_disconnect, counter, NULL);
122
123         __connman_rtnl_request_update();
124
125         return 0;
126 }
127
128 int __connman_counter_unregister(const char *owner, const char *path)
129 {
130         struct connman_counter *counter;
131
132         DBG("owner %s path %s", owner, path);
133
134         counter = g_hash_table_lookup(counter_table, path);
135         if (counter == NULL)
136                 return -ESRCH;
137
138         if (g_strcmp0(owner, counter->owner) != 0)
139                 return -EACCES;
140
141         g_hash_table_remove(owner_mapping, counter->owner);
142         g_hash_table_remove(counter_table, counter->path);
143
144         return 0;
145 }
146
147 static void send_usage(struct connman_counter *counter,
148                                         struct connman_stats *stats)
149 {
150         DBusMessage *message;
151         DBusMessageIter array, dict;
152
153         message = dbus_message_new_method_call(counter->owner, counter->path,
154                                         CONNMAN_COUNTER_INTERFACE, "Usage");
155         if (message == NULL)
156                 return;
157
158         dbus_message_set_no_reply(message, TRUE);
159
160         dbus_message_iter_init_append(message, &array);
161
162         connman_dbus_dict_open(&array, &dict);
163
164         connman_dbus_dict_append_basic(&dict, "Interface",
165                                         DBUS_TYPE_STRING, &stats->interface);
166         connman_dbus_dict_append_basic(&dict, "RX.Bytes",
167                                         DBUS_TYPE_UINT32, &stats->rx_bytes);
168         connman_dbus_dict_append_basic(&dict, "TX.Bytes",
169                                         DBUS_TYPE_UINT32, &stats->tx_bytes);
170
171         connman_dbus_dict_close(&array, &dict);
172
173         g_dbus_send_message(connection, message);
174 }
175
176 void __connman_counter_notify(const char *interface,
177                                 unsigned int rx_bytes, unsigned int tx_bytes)
178 {
179         struct connman_stats *stats;
180         GHashTableIter iter;
181         gpointer key, value;
182
183         stats = g_hash_table_lookup(stats_table, interface);
184         if (stats != NULL)
185                 goto update;
186
187         stats = g_try_new0(struct connman_stats, 1);
188         if (stats == NULL)
189                 return;
190
191         stats->interface = g_strdup(interface);
192
193         g_hash_table_replace(stats_table, stats->interface, stats);
194
195 update:
196         if (stats->rx_bytes == rx_bytes && stats->tx_bytes == tx_bytes)
197                 return;
198
199         stats->rx_bytes = rx_bytes;
200         stats->tx_bytes = tx_bytes;
201
202         g_hash_table_iter_init(&iter, counter_table);
203
204         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
205                 struct connman_counter *counter = value;
206
207                 send_usage(counter, stats);
208         }
209 }
210
211 static void release_counter(gpointer key, gpointer value, gpointer user_data)
212 {
213         struct connman_counter *counter = value;
214         DBusMessage *message;
215
216         DBG("owner %s path %s", counter->owner, counter->path);
217
218         message = dbus_message_new_method_call(counter->owner, counter->path,
219                                         CONNMAN_COUNTER_INTERFACE, "Release");
220         if (message == NULL)
221                 return;
222
223         dbus_message_set_no_reply(message, TRUE);
224
225         g_dbus_send_message(connection, message);
226 }
227
228 int __connman_counter_init(void)
229 {
230         DBG("");
231
232         connection = connman_dbus_get_connection();
233         if (connection == NULL)
234                 return -1;
235
236         stats_table = g_hash_table_new_full(g_str_hash, g_str_equal,
237                                                         NULL, remove_stats);
238
239         counter_table = g_hash_table_new_full(g_str_hash, g_str_equal,
240                                                         NULL, remove_counter);
241         owner_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
242                                                                 NULL, NULL);
243
244         return 0;
245 }
246
247 void __connman_counter_cleanup(void)
248 {
249         DBG("");
250
251         if (connection == NULL)
252                 return;
253
254         g_hash_table_foreach(counter_table, release_counter, NULL);
255
256         g_hash_table_destroy(owner_mapping);
257         g_hash_table_destroy(counter_table);
258
259         g_hash_table_destroy(stats_table);
260
261         dbus_connection_unref(connection);
262 }