service: Split service state to IPv4 and IPv6 parts
[platform/upstream/connman.git] / src / session.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 <string.h>
27
28 #include <gdbus.h>
29
30 #include "connman.h"
31
32 static DBusConnection *connection;
33 static GHashTable *session_hash;
34 static GHashTable *bearer_hash;
35
36 struct connman_bearer {
37         gint refcount;
38         char *name;
39 };
40
41 struct connman_session {
42         gint refcount;
43         char *owner;
44         guint watch;
45         struct connman_bearer *bearer;
46         struct connman_service *service;
47 };
48
49 static enum connman_service_type bearer2service(const char *bearer)
50 {
51         if (bearer == NULL)
52                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
53
54         DBG("%s", bearer);
55
56         if (g_strcmp0(bearer, "ethernet") == 0)
57                 return CONNMAN_SERVICE_TYPE_ETHERNET;
58         else if (g_strcmp0(bearer, "wifi") == 0)
59                 return CONNMAN_SERVICE_TYPE_WIFI;
60         else if (g_strcmp0(bearer, "wimax") == 0)
61                 return CONNMAN_SERVICE_TYPE_WIMAX;
62         else if (g_strcmp0(bearer, "bluetooth") == 0)
63                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
64         else if (g_strcmp0(bearer, "3g") == 0)
65                 return CONNMAN_SERVICE_TYPE_CELLULAR;
66         else
67                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
68 }
69
70 static char *service2bearer(enum connman_service_type type)
71 {
72         DBG("%d", type);
73
74         switch (type) {
75         case CONNMAN_SERVICE_TYPE_ETHERNET:
76                 return "ethernet";
77         case CONNMAN_SERVICE_TYPE_WIFI:
78                 return "wifi";
79         case CONNMAN_SERVICE_TYPE_WIMAX:
80                 return "wimax";
81         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
82                 return "bluetooth";
83         case CONNMAN_SERVICE_TYPE_CELLULAR:
84                 return "3g";
85         case CONNMAN_SERVICE_TYPE_UNKNOWN:
86         case CONNMAN_SERVICE_TYPE_SYSTEM:
87         case CONNMAN_SERVICE_TYPE_GPS:
88         case CONNMAN_SERVICE_TYPE_VPN:
89         case CONNMAN_SERVICE_TYPE_GADGET:
90                 return NULL;
91         }
92
93         return NULL;
94 }
95
96 static void remove_bearer(gpointer user_data)
97 {
98         struct connman_bearer *bearer = user_data;
99
100         g_free(bearer->name);
101         g_free(bearer);
102 }
103
104 static void remove_session(gpointer user_data)
105 {
106         struct connman_session *session = user_data;
107
108         session->bearer = NULL;
109         if (session->service)
110                 connman_service_unref(session->service);
111         g_free(session->owner);
112         g_free(session);
113 }
114
115 static int session_disconnect(struct connman_session *session)
116 {
117         struct connman_bearer *bearer = session->bearer;
118
119         DBG("%s", session->owner);
120
121         if (session == NULL)
122                 return -EINVAL;
123
124         /*
125          * Once a bearer is no longer referenced we actually disconnect
126          * the corresponding service.
127          */
128         if (bearer == NULL || g_atomic_int_dec_and_test(&bearer->refcount)) {
129                 struct connman_network *network;
130                 struct connman_device *device;
131
132                 /*
133                  * We toggle the reconnect flag to false when releasing a
134                  * session. This way a previously connected service will
135                  * not autoconnect once we've completely release a session.
136                  */
137                 network = __connman_service_get_network(session->service);
138                 if (network == NULL)
139                         return -EINVAL;
140
141                 device = connman_network_get_device(network);
142                 if (device == NULL)
143                         return -EINVAL;
144
145                 __connman_device_set_reconnect(device, FALSE);
146
147                 __connman_service_disconnect(session->service);
148                 connman_service_unref(session->service);
149
150                 g_hash_table_remove(bearer_hash, bearer);
151         }
152
153         if (session->watch > 0)
154                 g_dbus_remove_watch(connection, session->watch);
155
156         g_hash_table_remove(session_hash, session);
157
158         return 0;
159 }
160
161 static void owner_disconnect(DBusConnection *connection, void *user_data)
162 {
163         struct connman_session *session;
164         char *owner = user_data;
165
166         DBG("%s died", owner);
167
168         session = g_hash_table_lookup(session_hash, owner);
169         if (session == NULL) {
170                 connman_error("No session");
171                 return;
172         }
173
174         session_disconnect(session);
175 }
176
177 int __connman_session_release(const char *owner)
178 {
179         struct connman_session *session;
180
181         DBG("owner %s", owner);
182
183         session = g_hash_table_lookup(session_hash, owner);
184         if (session == NULL)
185                 return -EINVAL;
186
187         if (g_atomic_int_dec_and_test(&session->refcount))
188                 return session_disconnect(session);
189
190         return 0;
191 }
192
193 struct connman_service *__connman_session_request(const char *bearer_name,
194                                                         const char *owner)
195 {
196         struct connman_session *session;
197         struct connman_bearer *bearer;
198         enum connman_service_type service_type;
199         const char *bearer_name_new;
200         size_t bearer_name_len;
201
202         if (bearer_name == NULL)
203                 return NULL;
204
205         DBG("owner %s bearer %s", owner, bearer_name);
206
207         bearer_name_len = strlen(bearer_name);
208
209         session = g_hash_table_lookup(session_hash, owner);
210         if (session) {
211                 /* we only support one bearer per process */
212                 if (bearer_name_len &&
213                         g_strcmp0(session->bearer->name, bearer_name))
214                                 return NULL;
215
216                 g_atomic_int_inc(&session->refcount);
217
218                 return session->service;
219         }
220
221         session = g_try_new0(struct connman_session, 1);
222         if (session == NULL)
223                 return NULL;
224
225         session->refcount = 1;
226         session->owner = g_strdup(owner);
227         session->service = NULL;
228         g_hash_table_replace(session_hash, session->owner, session);
229
230         /* Find and connect service */
231         service_type = bearer2service(bearer_name);
232
233         session->service = __connman_service_connect_type(service_type);
234         if (session->service == NULL)
235                 goto failed_connect;
236
237         connman_service_ref(session->service);
238
239         service_type = connman_service_get_type(session->service);
240
241         /* We might get a different bearer from the one we requested */
242         bearer_name_new = service2bearer(service_type);
243
244         /* Refcount the exisiting bearer, or create one */
245         bearer = g_hash_table_lookup(bearer_hash, bearer_name_new);
246         if (bearer == NULL) {
247                 bearer = g_try_new0(struct connman_bearer, 1);
248                 if (bearer == NULL)
249                         goto failed_bearer;
250
251                 bearer->refcount = 0;
252                 bearer->name = g_strdup(bearer_name_new);
253                 g_hash_table_replace(bearer_hash, bearer->name, bearer);
254         }
255
256         g_atomic_int_inc(&bearer->refcount);
257         session->bearer = bearer;
258
259         session->watch = g_dbus_add_disconnect_watch(connection, session->owner,
260                                         owner_disconnect, session->owner, NULL);
261         return session->service;
262
263 failed_bearer:
264         session_disconnect(session);
265
266 failed_connect:
267         g_hash_table_remove(session_hash, session);
268
269         return NULL;
270 }
271
272 int __connman_session_init(void)
273 {
274         DBG("");
275
276         connection = connman_dbus_get_connection();
277         if (connection == NULL)
278                 return -1;
279
280         session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
281                                                 NULL, remove_session);
282
283         bearer_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
284                                                 NULL, remove_bearer);
285
286         return 0;
287 }
288
289 void __connman_session_cleanup(void)
290 {
291         DBG("");
292
293         if (connection == NULL)
294                 return;
295
296         g_hash_table_destroy(bearer_hash);
297         g_hash_table_destroy(session_hash);
298         dbus_connection_unref(connection);
299 }