session: Fix NULL pointer name append
[framework/connectivity/connman.git] / src / session.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2011  BWM CarIT GmbH. All rights reserved.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <gdbus.h>
28
29 #include "connman.h"
30
31 static DBusConnection *connection;
32 static GHashTable *session_hash;
33 static connman_bool_t sessionmode;
34 static struct connman_session *ecall_session;
35
36 enum connman_session_trigger {
37         CONNMAN_SESSION_TRIGGER_UNKNOWN         = 0,
38         CONNMAN_SESSION_TRIGGER_SETTING         = 1,
39         CONNMAN_SESSION_TRIGGER_CONNECT         = 2,
40         CONNMAN_SESSION_TRIGGER_DISCONNECT      = 3,
41         CONNMAN_SESSION_TRIGGER_PERIODIC        = 4,
42         CONNMAN_SESSION_TRIGGER_SERVICE         = 5,
43         CONNMAN_SESSION_TRIGGER_ECALL           = 6,
44 };
45
46 enum connman_session_roaming_policy {
47         CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN          = 0,
48         CONNMAN_SESSION_ROAMING_POLICY_DEFAULT          = 1,
49         CONNMAN_SESSION_ROAMING_POLICY_ALWAYS           = 2,
50         CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN        = 3,
51         CONNMAN_SESSION_ROAMING_POLICY_NATIONAL         = 4,
52         CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL    = 5,
53 };
54
55 struct session_info {
56         char *bearer;
57         const char *name;
58         char *ifname;
59         connman_bool_t online;
60         connman_bool_t priority;
61         GSList *allowed_bearers;
62         connman_bool_t avoid_handover;
63         connman_bool_t stay_connected;
64         unsigned int periodic_connect;
65         unsigned int idle_timeout;
66         connman_bool_t ecall;
67         enum connman_session_roaming_policy roaming_policy;
68         unsigned int marker;
69
70         struct connman_service *service;
71 };
72
73 struct connman_session {
74         char *owner;
75         char *session_path;
76         char *notify_path;
77         guint notify_watch;
78
79         connman_bool_t append_all;
80         connman_bool_t info_dirty;
81         struct session_info info;
82         struct session_info info_last;
83
84         GSequence *service_list;
85 };
86
87 struct bearer_info {
88         char *name;
89         connman_bool_t match_all;
90         enum connman_service_type service_type;
91 };
92
93 static const char *trigger2string(enum connman_session_trigger trigger)
94 {
95         switch (trigger) {
96         case CONNMAN_SESSION_TRIGGER_UNKNOWN:
97                 break;
98         case CONNMAN_SESSION_TRIGGER_SETTING:
99                 return "setting";
100         case CONNMAN_SESSION_TRIGGER_CONNECT:
101                 return "connect";
102         case CONNMAN_SESSION_TRIGGER_DISCONNECT:
103                 return "disconnect";
104         case CONNMAN_SESSION_TRIGGER_PERIODIC:
105                 return "periodic";
106         case CONNMAN_SESSION_TRIGGER_SERVICE:
107                 return "service";
108         case CONNMAN_SESSION_TRIGGER_ECALL:
109                 return "ecall";
110         }
111
112         return NULL;
113 }
114
115 static const char *roamingpolicy2string(enum connman_session_roaming_policy policy)
116 {
117         switch (policy) {
118         case CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN:
119                 break;
120         case CONNMAN_SESSION_ROAMING_POLICY_DEFAULT:
121                 return "default";
122         case CONNMAN_SESSION_ROAMING_POLICY_ALWAYS:
123                 return "always";
124         case CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN:
125                 return "forbidden";
126         case CONNMAN_SESSION_ROAMING_POLICY_NATIONAL:
127                 return "national";
128         case CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL:
129                 return "international";
130         }
131
132         return NULL;
133 }
134
135 static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
136 {
137         if (g_strcmp0(policy, "default") == 0)
138                 return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
139         else if (g_strcmp0(policy, "always") == 0)
140                 return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
141         else if (g_strcmp0(policy, "forbidden") == 0)
142                 return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
143         else if (g_strcmp0(policy, "national") == 0)
144                 return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
145         else if (g_strcmp0(policy, "international") == 0)
146                 return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
147         else
148                 return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
149 }
150
151 static enum connman_service_type bearer2service(const char *bearer)
152 {
153         if (bearer == NULL)
154                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
155
156         if (g_strcmp0(bearer, "ethernet") == 0)
157                 return CONNMAN_SERVICE_TYPE_ETHERNET;
158         else if (g_strcmp0(bearer, "wifi") == 0)
159                 return CONNMAN_SERVICE_TYPE_WIFI;
160         else if (g_strcmp0(bearer, "wimax") == 0)
161                 return CONNMAN_SERVICE_TYPE_WIMAX;
162         else if (g_strcmp0(bearer, "bluetooth") == 0)
163                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
164         else if (g_strcmp0(bearer, "3g") == 0)
165                 return CONNMAN_SERVICE_TYPE_CELLULAR;
166         else
167                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
168 }
169
170 static char *service2bearer(enum connman_service_type type)
171 {
172         switch (type) {
173         case CONNMAN_SERVICE_TYPE_ETHERNET:
174                 return "ethernet";
175         case CONNMAN_SERVICE_TYPE_WIFI:
176                 return "wifi";
177         case CONNMAN_SERVICE_TYPE_WIMAX:
178                 return "wimax";
179         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
180                 return "bluetooth";
181         case CONNMAN_SERVICE_TYPE_CELLULAR:
182                 return "3g";
183         case CONNMAN_SERVICE_TYPE_UNKNOWN:
184         case CONNMAN_SERVICE_TYPE_SYSTEM:
185         case CONNMAN_SERVICE_TYPE_GPS:
186         case CONNMAN_SERVICE_TYPE_VPN:
187         case CONNMAN_SERVICE_TYPE_GADGET:
188                 return "";
189         }
190
191         return "";
192 }
193
194 static void cleanup_bearer_info(gpointer data, gpointer user_data)
195 {
196         struct bearer_info *info = data;
197
198         g_free(info->name);
199         g_free(info);
200 }
201
202 static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
203 {
204         struct bearer_info *info;
205         DBusMessageIter array;
206         GSList *list = NULL;
207
208         dbus_message_iter_recurse(iter, &array);
209
210         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
211                 char *bearer = NULL;
212
213                 dbus_message_iter_get_basic(&array, &bearer);
214
215                 info = g_try_new0(struct bearer_info, 1);
216                 if (info == NULL) {
217                         g_slist_foreach(list, cleanup_bearer_info, NULL);
218                         g_slist_free(list);
219
220                         return NULL;
221                 }
222
223                 info->name = g_strdup(bearer);
224                 info->service_type = bearer2service(info->name);
225
226                 if (info->service_type == CONNMAN_SERVICE_TYPE_UNKNOWN &&
227                                 g_strcmp0(info->name, "*") == 0) {
228                         info->match_all = TRUE;
229                 } else {
230                         info->match_all = FALSE;
231                 }
232
233                 list = g_slist_append(list, info);
234
235                 dbus_message_iter_next(&array);
236         }
237
238         return list;
239 }
240
241 static GSList *session_allowed_bearers_any(void)
242 {
243         struct bearer_info *info;
244         GSList *list = NULL;
245
246         info = g_try_new0(struct bearer_info, 1);
247         if (info == NULL) {
248                 g_slist_free(list);
249
250                 return NULL;
251         }
252
253         info->name = g_strdup("");
254         info->match_all = TRUE;
255         info->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
256
257         list = g_slist_append(list, info);
258
259         return list;
260 }
261
262 static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
263 {
264         struct session_info *info = user_data;
265         GSList *list;
266
267         for (list = info->allowed_bearers;
268                         list != NULL; list = list->next) {
269                 struct bearer_info *info = list->data;
270
271                 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
272                                                 &info->name);
273         }
274 }
275
276 static void append_ipconfig_ipv4(DBusMessageIter *iter, void *user_data)
277 {
278         struct connman_service *service = user_data;
279         struct connman_ipconfig *ipconfig_ipv4;
280
281         if (service == NULL)
282                 return;
283
284         ipconfig_ipv4 = __connman_service_get_ip4config(service);
285         if (ipconfig_ipv4 == NULL)
286                 return;
287
288         __connman_ipconfig_append_ipv4(ipconfig_ipv4, iter);
289 }
290
291 static void append_ipconfig_ipv6(DBusMessageIter *iter, void *user_data)
292 {
293         struct connman_service *service = user_data;
294         struct connman_ipconfig *ipconfig_ipv4, *ipconfig_ipv6;
295
296         if (service == NULL)
297                 return;
298
299         ipconfig_ipv4 = __connman_service_get_ip4config(service);
300         ipconfig_ipv6 = __connman_service_get_ip6config(service);
301         if (ipconfig_ipv6 == NULL)
302                 return;
303
304         __connman_ipconfig_append_ipv6(ipconfig_ipv6, iter, ipconfig_ipv4);
305 }
306
307 static void append_notify(DBusMessageIter *dict,
308                                         struct connman_session *session)
309 {
310         struct session_info *info = &session->info;
311         struct session_info *info_last = &session->info_last;
312         const char *policy;
313
314         if (session->append_all == TRUE ||
315                         info->bearer != info_last->bearer) {
316                 connman_dbus_dict_append_basic(dict, "Bearer",
317                                                 DBUS_TYPE_STRING,
318                                                 &info->bearer);
319                 info_last->bearer = info->bearer;
320         }
321
322         if (session->append_all == TRUE ||
323                         info->online != info_last->online) {
324                 connman_dbus_dict_append_basic(dict, "Online",
325                                                 DBUS_TYPE_BOOLEAN,
326                                                 &info->online);
327                 info_last->online = info->online;
328         }
329
330         if (session->append_all == TRUE ||
331                         info->name != info_last->name) {
332                 connman_dbus_dict_append_basic(dict, "Name",
333                                                 DBUS_TYPE_STRING,
334                                                 &info->name);
335                 info_last->name = info->name;
336         }
337
338         if (session->append_all == TRUE ||
339                         info->service != info_last->service) {
340                 connman_dbus_dict_append_dict(dict, "IPv4",
341                                                 append_ipconfig_ipv4,
342                                                 info->service);
343
344                 connman_dbus_dict_append_dict(dict, "IPv6",
345                                                 append_ipconfig_ipv6,
346                                                 info->service);
347
348                 connman_dbus_dict_append_basic(dict, "Interface",
349                                                 DBUS_TYPE_STRING,
350                                                 &info->ifname);
351
352                 info_last->ifname = info->ifname;
353                 info_last->service = info->service;
354         }
355
356
357         if (session->append_all == TRUE ||
358                         info->priority != info_last->priority) {
359                 connman_dbus_dict_append_basic(dict, "Priority",
360                                                 DBUS_TYPE_BOOLEAN,
361                                                 &info->priority);
362                 info_last->priority = info->priority;
363         }
364
365         if (session->append_all == TRUE ||
366                         info->allowed_bearers != info_last->allowed_bearers) {
367                 connman_dbus_dict_append_array(dict, "AllowedBearers",
368                                                 DBUS_TYPE_STRING,
369                                                 append_allowed_bearers,
370                                                 info);
371                 info_last->allowed_bearers = info->allowed_bearers;
372         }
373
374         if (session->append_all == TRUE ||
375                         info->avoid_handover != info_last->avoid_handover) {
376                 connman_dbus_dict_append_basic(dict, "AvoidHandover",
377                                                 DBUS_TYPE_BOOLEAN,
378                                                 &info->avoid_handover);
379                 info_last->avoid_handover = info->avoid_handover;
380         }
381
382         if (session->append_all == TRUE ||
383                         info->stay_connected != info_last->stay_connected) {
384                 connman_dbus_dict_append_basic(dict, "StayConnected",
385                                                 DBUS_TYPE_BOOLEAN,
386                                                 &info->stay_connected);
387                 info_last->stay_connected = info->stay_connected;
388         }
389
390         if (session->append_all == TRUE ||
391                         info->periodic_connect != info_last->periodic_connect) {
392                 connman_dbus_dict_append_basic(dict, "PeriodicConnect",
393                                                 DBUS_TYPE_UINT32,
394                                                 &info->periodic_connect);
395                 info_last->periodic_connect = info->periodic_connect;
396         }
397
398         if (session->append_all == TRUE ||
399                         info->idle_timeout != info_last->idle_timeout) {
400                 connman_dbus_dict_append_basic(dict, "IdleTimeout",
401                                                 DBUS_TYPE_UINT32,
402                                                 &info->idle_timeout);
403                 info_last->idle_timeout = info->idle_timeout;
404         }
405
406         if (session->append_all == TRUE ||
407                         info->ecall != info_last->ecall) {
408                 connman_dbus_dict_append_basic(dict, "EmergencyCall",
409                                                 DBUS_TYPE_BOOLEAN,
410                                                 &info->ecall);
411                 info_last->ecall = info->ecall;
412         }
413
414         if (session->append_all == TRUE ||
415                         info->roaming_policy != info_last->roaming_policy) {
416                 policy = roamingpolicy2string(info->roaming_policy);
417                 connman_dbus_dict_append_basic(dict, "RoamingPolicy",
418                                                 DBUS_TYPE_STRING,
419                                                 &policy);
420                 info_last->roaming_policy = info->roaming_policy;
421         }
422
423         if (session->append_all == TRUE ||
424                         info->marker != info_last->marker) {
425                 connman_dbus_dict_append_basic(dict, "SessionMarker",
426                                                 DBUS_TYPE_UINT32,
427                                                 &info->marker);
428                 info_last->marker = info->marker;
429         }
430
431         session->append_all = FALSE;
432         session->info_dirty = FALSE;
433 }
434
435 static gboolean session_notify(gpointer user_data)
436 {
437         struct connman_session *session = user_data;
438         DBusMessage *msg;
439         DBusMessageIter array, dict;
440
441         if (session->info_dirty == FALSE)
442                 return 0;
443
444         DBG("session %p owner %s notify_path %s", session,
445                 session->owner, session->notify_path);
446
447         msg = dbus_message_new_method_call(session->owner, session->notify_path,
448                                                 CONNMAN_NOTIFICATION_INTERFACE,
449                                                 "Update");
450         if (msg == NULL)
451                 return FALSE;
452
453         dbus_message_iter_init_append(msg, &array);
454         connman_dbus_dict_open(&array, &dict);
455
456         append_notify(&dict, session);
457
458         connman_dbus_dict_close(&array, &dict);
459
460         g_dbus_send_message(connection, msg);
461
462         session->info_dirty = FALSE;
463
464         return FALSE;
465 }
466
467 static void ipconfig_ipv4_changed(struct connman_session *session)
468 {
469         struct session_info *info = &session->info;
470
471         connman_dbus_setting_changed_dict(session->owner, session->notify_path,
472                                                 "IPv4", append_ipconfig_ipv4,
473                                                 info->service);
474 }
475
476 static void ipconfig_ipv6_changed(struct connman_session *session)
477 {
478         struct session_info *info = &session->info;
479
480         connman_dbus_setting_changed_dict(session->owner, session->notify_path,
481                                                 "IPv6", append_ipconfig_ipv6,
482                                                 info->service);
483 }
484
485 static GSequenceIter *lookup_service(struct connman_session *session,
486                                         struct connman_service *service)
487 {
488         GSequenceIter *iter;
489
490         if (service == NULL)
491                 return NULL;
492
493         iter = g_sequence_get_begin_iter(session->service_list);
494
495         while (g_sequence_iter_is_end(iter) == FALSE) {
496                 struct connman_service *service_iter = g_sequence_get(iter);
497
498                 if (service_iter == service)
499                         return iter;
500
501                 iter = g_sequence_iter_next(iter);
502         }
503
504         return NULL;
505 }
506
507 static connman_bool_t service_type_match(struct connman_session *session,
508                                         struct connman_service *service)
509 {
510         struct session_info *info = &session->info;
511         GSList *list;
512
513         for (list = info->allowed_bearers;
514                         list != NULL; list = list->next) {
515                 struct bearer_info *info = list->data;
516                 enum connman_service_type service_type;
517
518                 if (info->match_all == TRUE)
519                         return TRUE;
520
521                 service_type = connman_service_get_type(service);
522                 if (info->service_type == service_type)
523                         return TRUE;
524         }
525
526         return FALSE;
527 }
528
529 static connman_bool_t service_match(struct connman_session *session,
530                                         struct connman_service *service)
531 {
532         if (service_type_match(session, service) == FALSE)
533                 return FALSE;
534
535         return TRUE;
536 }
537
538 static int service_type_weight(enum connman_service_type type)
539 {
540         /*
541          * The session doesn't care which service
542          * to use. Nevertheless we have to sort them
543          * according their type. The ordering is
544          *
545          * 1. Ethernet
546          * 2. Bluetooth
547          * 3. WiFi/WiMAX
548          * 4. GSM/UTMS/3G
549          */
550
551         switch (type) {
552         case CONNMAN_SERVICE_TYPE_ETHERNET:
553                 return 4;
554         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
555                 return 3;
556         case CONNMAN_SERVICE_TYPE_WIFI:
557         case CONNMAN_SERVICE_TYPE_WIMAX:
558                 return 2;
559         case CONNMAN_SERVICE_TYPE_CELLULAR:
560                 return 1;
561         case CONNMAN_SERVICE_TYPE_UNKNOWN:
562         case CONNMAN_SERVICE_TYPE_SYSTEM:
563         case CONNMAN_SERVICE_TYPE_GPS:
564         case CONNMAN_SERVICE_TYPE_VPN:
565         case CONNMAN_SERVICE_TYPE_GADGET:
566                 break;
567         }
568
569         return 0;
570 }
571
572 static gint sort_allowed_bearers(struct connman_service *service_a,
573                                         struct connman_service *service_b,
574                                         struct connman_session *session)
575 {
576         struct session_info *info = &session->info;
577         GSList *list;
578         enum connman_service_type type_a, type_b;
579         int weight_a, weight_b;
580
581         type_a = connman_service_get_type(service_a);
582         type_b = connman_service_get_type(service_b);
583
584         for (list = info->allowed_bearers;
585                         list != NULL; list = list->next) {
586                 struct bearer_info *info = list->data;
587
588                 if (info->match_all == TRUE) {
589                         if (type_a != type_b) {
590                                 weight_a = service_type_weight(type_a);
591                                 weight_b = service_type_weight(type_b);
592
593                                 if (weight_a > weight_b)
594                                         return -1;
595
596                                 if (weight_a < weight_b)
597                                         return 1;
598
599                                 return 0;
600                         }
601                 }
602
603                 if (type_a == info->service_type &&
604                                 type_b == info->service_type) {
605                         return 0;
606                 }
607
608                 if (type_a == info->service_type &&
609                                 type_b != info->service_type) {
610                         return -1;
611                 }
612
613                 if (type_a != info->service_type &&
614                                 type_b == info->service_type) {
615                         return 1;
616                 }
617         }
618
619         return 0;
620 }
621
622 static gint sort_services(gconstpointer a, gconstpointer b, gpointer user_data)
623 {
624         struct connman_service *service_a = (void *)a;
625         struct connman_service *service_b = (void *)b;
626         struct connman_session *session = user_data;
627
628         return sort_allowed_bearers(service_a, service_b, session);
629 }
630
631 static void cleanup_session(gpointer user_data)
632 {
633         struct connman_session *session = user_data;
634         struct session_info *info = &session->info;
635
636         DBG("remove %s", session->session_path);
637
638         g_sequence_free(session->service_list);
639
640         g_slist_foreach(info->allowed_bearers, cleanup_bearer_info, NULL);
641         g_slist_free(info->allowed_bearers);
642
643         g_free(session->owner);
644         g_free(session->session_path);
645         g_free(session->notify_path);
646
647         g_free(session);
648 }
649
650 static void release_session(gpointer key, gpointer value, gpointer user_data)
651 {
652         struct connman_session *session = value;
653         DBusMessage *message;
654
655         DBG("owner %s path %s", session->owner, session->notify_path);
656
657         if (session->notify_watch > 0)
658                 g_dbus_remove_watch(connection, session->notify_watch);
659
660         g_dbus_unregister_interface(connection, session->session_path,
661                                                 CONNMAN_SESSION_INTERFACE);
662
663         message = dbus_message_new_method_call(session->owner,
664                                                 session->notify_path,
665                                                 CONNMAN_NOTIFICATION_INTERFACE,
666                                                 "Release");
667         if (message == NULL)
668                 return;
669
670         dbus_message_set_no_reply(message, TRUE);
671
672         g_dbus_send_message(connection, message);
673 }
674
675 static int session_disconnect(struct connman_session *session)
676 {
677         DBG("session %p, %s", session, session->owner);
678
679         if (session->notify_watch > 0)
680                 g_dbus_remove_watch(connection, session->notify_watch);
681
682         g_dbus_unregister_interface(connection, session->session_path,
683                                                 CONNMAN_SESSION_INTERFACE);
684
685         g_hash_table_remove(session_hash, session->session_path);
686
687         return 0;
688 }
689
690 static void owner_disconnect(DBusConnection *conn, void *user_data)
691 {
692         struct connman_session *session = user_data;
693
694         DBG("session %p, %s died", session, session->owner);
695
696         session_disconnect(session);
697 }
698
699 static DBusMessage *destroy_session(DBusConnection *conn,
700                                         DBusMessage *msg, void *user_data)
701 {
702         struct connman_session *session = user_data;
703
704         DBG("session %p", session);
705
706         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
707 }
708
709 static void update_info(struct session_info *info)
710 {
711         enum connman_service_type type;
712         int idx;
713
714         if (info->service != NULL) {
715                 type = connman_service_get_type(info->service);
716                 info->bearer = service2bearer(type);
717
718                 info->online = __connman_service_is_connected(info->service);
719                 info->name = __connman_service_get_name(info->service);
720                 if (info->name == NULL)
721                         info->name = "";
722
723                 idx = __connman_service_get_index(info->service);
724                 info->ifname = connman_inet_ifname(idx);
725                 if (info->ifname == NULL)
726                         info->ifname = "";
727         } else {
728                 info->bearer = "";
729                 info->online = FALSE;
730                 info->name = "";
731                 info->ifname = "";
732         }
733 }
734
735 static void notify_service_changes(struct connman_session *session)
736 {
737         struct session_info *info = &session->info;
738         struct session_info *info_last = &session->info_last;
739
740         if (info->service == info_last->service)
741                 return;
742
743         update_info(info);
744         session->info_dirty = TRUE;
745 }
746
747 static void select_and_connect(struct connman_session *session,
748                                 connman_bool_t do_connect)
749 {
750         struct session_info *info = &session->info;
751         struct connman_service *service = NULL;
752         GSequenceIter *iter;
753
754         DBG("session %p connect %d", session, do_connect);
755
756         iter = g_sequence_get_begin_iter(session->service_list);
757
758         while (g_sequence_iter_is_end(iter) == FALSE) {
759                 service = g_sequence_get(iter);
760
761                 if (__connman_service_is_connecting(service) == TRUE ||
762                                 __connman_service_is_connected(service) == TRUE) {
763                         break;
764                 }
765
766                 if (__connman_service_is_idle(service) == TRUE &&
767                                 do_connect == TRUE) {
768                         break;
769                 }
770
771                 service = NULL;
772
773                 iter = g_sequence_iter_next(iter);
774         }
775
776         if (info->service != NULL && info->service != service) {
777                 __connman_service_disconnect(info->service);
778                 info->service = NULL;
779         }
780
781         if (service != NULL) {
782                 info->service = service;
783
784                 if (do_connect == TRUE)
785                         __connman_service_connect(info->service);
786         }
787
788         notify_service_changes(session);
789 }
790
791 static void session_changed(struct connman_session *session,
792                                 enum connman_session_trigger trigger)
793 {
794         struct session_info *info = &session->info;
795         GSequenceIter *iter;
796
797         /*
798          * TODO: This only a placeholder for the 'real' algorithm to
799          * play a bit around. So we are going to improve it step by step.
800          */
801
802         DBG("session %p trigger %s", session, trigger2string(trigger));
803
804         switch (trigger) {
805         case CONNMAN_SESSION_TRIGGER_UNKNOWN:
806                 DBG("ignore session changed event");
807                 return;
808         case CONNMAN_SESSION_TRIGGER_SETTING:
809                 if (info->service != NULL) {
810                         iter = lookup_service(session, info->service);
811                         if (iter == NULL) {
812                                 /*
813                                  * This service is not part of this
814                                  * session anymore.
815                                  */
816
817                                 __connman_service_disconnect(info->service);
818                                 info->service = NULL;
819                         }
820
821                         notify_service_changes(session);
822                 }
823
824                 /* Try to free ride */
825                 if (info->online == FALSE)
826                         select_and_connect(session, FALSE);
827
828                 break;
829         case CONNMAN_SESSION_TRIGGER_CONNECT:
830                 if (info->online == TRUE)
831                         break;
832
833                 select_and_connect(session, TRUE);
834
835                 break;
836         case CONNMAN_SESSION_TRIGGER_DISCONNECT:
837                 if (info->online == FALSE)
838                         break;
839
840                 if (info->service != NULL)
841                         __connman_service_disconnect(info->service);
842
843                 info->service = NULL;
844                 notify_service_changes(session);
845
846                 break;
847         case CONNMAN_SESSION_TRIGGER_PERIODIC:
848                 select_and_connect(session, TRUE);
849
850                 break;
851         case CONNMAN_SESSION_TRIGGER_SERVICE:
852                 if (info->online == TRUE)
853                         break;
854
855                 if (info->stay_connected == TRUE) {
856                         DBG("StayConnected");
857                         select_and_connect(session, TRUE);
858
859                         break;
860                 }
861
862                 /* Try to free ride */
863                 select_and_connect(session, FALSE);
864
865                 break;
866         case CONNMAN_SESSION_TRIGGER_ECALL:
867                 if (info->online == FALSE && info->service != NULL)
868                         info->service = NULL;
869
870                 notify_service_changes(session);
871
872                 break;
873         }
874
875         switch (trigger) {
876         case CONNMAN_SESSION_TRIGGER_UNKNOWN:
877                 break;
878         case CONNMAN_SESSION_TRIGGER_CONNECT:
879         case CONNMAN_SESSION_TRIGGER_DISCONNECT:
880         case CONNMAN_SESSION_TRIGGER_ECALL:
881                 g_timeout_add_seconds(0, session_notify, session);
882                 break;
883         case CONNMAN_SESSION_TRIGGER_SETTING:
884         case CONNMAN_SESSION_TRIGGER_PERIODIC:
885         case CONNMAN_SESSION_TRIGGER_SERVICE:
886                 session_notify(session);
887                 break;
888         }
889 }
890
891 static DBusMessage *connect_session(DBusConnection *conn,
892                                         DBusMessage *msg, void *user_data)
893 {
894         struct connman_session *session = user_data;
895
896         DBG("session %p", session);
897
898         if (ecall_session != NULL && ecall_session != session)
899                 return __connman_error_failed(msg, EBUSY);
900
901         session_changed(session, CONNMAN_SESSION_TRIGGER_CONNECT);
902
903         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
904 }
905
906 static DBusMessage *disconnect_session(DBusConnection *conn,
907                                         DBusMessage *msg, void *user_data)
908 {
909         struct connman_session *session = user_data;
910
911         DBG("session %p", session);
912
913         if (ecall_session != NULL && ecall_session != session)
914                 return __connman_error_failed(msg, EBUSY);
915
916         session_changed(session, CONNMAN_SESSION_TRIGGER_DISCONNECT);
917
918         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
919 }
920
921 static void print_name(gpointer data, gpointer user_data)
922 {
923         struct connman_service *service = data;
924
925         DBG("service %p type %s name %s", service,
926                 service2bearer(connman_service_get_type(service)),
927                 __connman_service_get_name(service));
928 }
929
930 static void update_allowed_bearers(struct connman_session *session)
931 {
932         if (session->service_list != NULL)
933                 g_sequence_free(session->service_list);
934
935         session->service_list = __connman_service_get_list(session,
936                                                                 service_match);
937         g_sequence_sort(session->service_list, sort_services, session);
938         g_sequence_foreach(session->service_list, print_name, NULL);
939
940         session->info_dirty = TRUE;
941 }
942
943 static void update_ecall_sessions(struct connman_session *session)
944 {
945         struct session_info *info = &session->info;
946         struct connman_session *session_iter;
947         GHashTableIter iter;
948         gpointer key, value;
949
950         g_hash_table_iter_init(&iter, session_hash);
951
952         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
953                 session_iter = value;
954
955                 if (session_iter == session)
956                         continue;
957
958                 session_iter->info.ecall = info->ecall;
959                 session_iter->info_dirty = TRUE;
960
961                 session_changed(session_iter, CONNMAN_SESSION_TRIGGER_ECALL);
962         }
963 }
964
965 static void update_ecall(struct connman_session *session)
966 {
967         struct session_info *info = &session->info;
968         struct session_info *info_last = &session->info_last;
969
970         DBG("session %p ecall_session %p ecall %d -> %d", session,
971                 ecall_session, info_last->ecall, info->ecall);
972
973         if (ecall_session == NULL) {
974                 if (!(info_last->ecall == FALSE && info->ecall == TRUE))
975                         goto err;
976
977                 ecall_session = session;
978         } else if (ecall_session == session) {
979                 if (!(info_last->ecall == TRUE && info->ecall == FALSE))
980                         goto err;
981
982                 ecall_session = NULL;
983         } else {
984                 goto err;
985         }
986
987         update_ecall_sessions(session);
988
989         session->info_dirty = TRUE;
990         return;
991
992 err:
993         /* not a valid transition */
994         info->ecall = info_last->ecall;
995 }
996
997 static DBusMessage *change_session(DBusConnection *conn,
998                                         DBusMessage *msg, void *user_data)
999 {
1000         struct connman_session *session = user_data;
1001         struct session_info *info = &session->info;
1002         struct session_info *info_last = &session->info_last;
1003         DBusMessageIter iter, value;
1004         const char *name;
1005         GSList *allowed_bearers;
1006
1007         DBG("session %p", session);
1008         if (dbus_message_iter_init(msg, &iter) == FALSE)
1009                 return __connman_error_invalid_arguments(msg);
1010
1011         dbus_message_iter_get_basic(&iter, &name);
1012         dbus_message_iter_next(&iter);
1013         dbus_message_iter_recurse(&iter, &value);
1014
1015         switch (dbus_message_iter_get_arg_type(&value)) {
1016         case DBUS_TYPE_ARRAY:
1017                 if (g_str_equal(name, "AllowedBearers") == TRUE) {
1018                         allowed_bearers = session_parse_allowed_bearers(&value);
1019
1020                         g_slist_foreach(info->allowed_bearers,
1021                                         cleanup_bearer_info, NULL);
1022                         g_slist_free(info->allowed_bearers);
1023
1024                         if (allowed_bearers == NULL) {
1025                                 allowed_bearers = session_allowed_bearers_any();
1026
1027                                 if (allowed_bearers == NULL)
1028                                         return __connman_error_failed(msg, ENOMEM);
1029                         }
1030
1031                         info->allowed_bearers = allowed_bearers;
1032
1033                         update_allowed_bearers(session);
1034                 } else {
1035                         goto err;
1036                 }
1037                 break;
1038         case DBUS_TYPE_BOOLEAN:
1039                 if (g_str_equal(name, "Priority") == TRUE) {
1040                         dbus_message_iter_get_basic(&value,
1041                                         &info->priority);
1042
1043                         if (info_last->priority != info->priority)
1044                                 session->info_dirty = TRUE;
1045                 } else if (g_str_equal(name, "AvoidHandover") == TRUE) {
1046                         dbus_message_iter_get_basic(&value,
1047                                         &info->avoid_handover);
1048
1049                         if (info_last->avoid_handover != info->avoid_handover)
1050                                 session->info_dirty = TRUE;
1051                         } else if (g_str_equal(name, "StayConnected") == TRUE) {
1052                         dbus_message_iter_get_basic(&value,
1053                                         &info->stay_connected);
1054
1055                         if (info_last->stay_connected != info->stay_connected)
1056                                 session->info_dirty = TRUE;
1057                 } else if (g_str_equal(name, "EmergencyCall") == TRUE) {
1058                         dbus_message_iter_get_basic(&value,
1059                                         &info->ecall);
1060
1061                         update_ecall(session);
1062                 } else {
1063                         goto err;
1064                 }
1065                 break;
1066         case DBUS_TYPE_UINT32:
1067                 if (g_str_equal(name, "PeriodicConnect") == TRUE) {
1068                         dbus_message_iter_get_basic(&value,
1069                                         &info->periodic_connect);
1070
1071                         if (info_last->periodic_connect != info->periodic_connect)
1072                                 session->info_dirty = TRUE;
1073                 } else if (g_str_equal(name, "IdleTimeout") == TRUE) {
1074                         dbus_message_iter_get_basic(&value,
1075                                         &info->idle_timeout);
1076
1077                         if (info_last->idle_timeout != info->idle_timeout)
1078                                 session->info_dirty = TRUE;
1079                 } else {
1080                         goto err;
1081                 }
1082                 break;
1083         case DBUS_TYPE_STRING:
1084                 if (g_str_equal(name, "RoamingPolicy") == TRUE) {
1085                         const char *val;
1086                         dbus_message_iter_get_basic(&value, &val);
1087                         info->roaming_policy =
1088                                         string2roamingpolicy(val);
1089
1090                         if (info_last->roaming_policy != info->roaming_policy)
1091                                 session->info_dirty = TRUE;
1092                 } else {
1093                         goto err;
1094                 }
1095                 break;
1096         default:
1097                 goto err;
1098         }
1099
1100         if (session->info_dirty == TRUE)
1101                 session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1102
1103         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1104
1105 err:
1106         return __connman_error_invalid_arguments(msg);
1107 }
1108
1109 static GDBusMethodTable session_methods[] = {
1110         { "Destroy",    "",   "", destroy_session    },
1111         { "Connect",    "",   "", connect_session    },
1112         { "Disconnect", "",   "", disconnect_session },
1113         { "Change",     "sv", "", change_session     },
1114         { },
1115 };
1116
1117 int __connman_session_create(DBusMessage *msg)
1118 {
1119         const char *owner, *notify_path;
1120         char *session_path = NULL;
1121         DBusMessageIter iter, array;
1122         struct connman_session *session;
1123         struct session_info *info, *info_last;
1124
1125         connman_bool_t priority = FALSE, avoid_handover = FALSE;
1126         connman_bool_t stay_connected = FALSE, ecall = FALSE;
1127         enum connman_session_roaming_policy roaming_policy =
1128                                 CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
1129         GSList *allowed_bearers = NULL;
1130         unsigned int periodic_connect = 0;
1131         unsigned int idle_timeout = 0;
1132
1133         int err;
1134
1135         owner = dbus_message_get_sender(msg);
1136
1137         DBG("owner %s", owner);
1138
1139         if (ecall_session != NULL) {
1140                 /*
1141                  * If there is an emergency call already going on,
1142                  * ignore session creation attempt
1143                  */
1144                 err = -EBUSY;
1145                 goto err;
1146         }
1147
1148         dbus_message_iter_init(msg, &iter);
1149         dbus_message_iter_recurse(&iter, &array);
1150
1151         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
1152                 DBusMessageIter entry, value;
1153                 const char *key, *val;
1154
1155                 dbus_message_iter_recurse(&array, &entry);
1156                 dbus_message_iter_get_basic(&entry, &key);
1157
1158                 dbus_message_iter_next(&entry);
1159                 dbus_message_iter_recurse(&entry, &value);
1160
1161                 switch (dbus_message_iter_get_arg_type(&value)) {
1162                 case DBUS_TYPE_ARRAY:
1163                         if (g_str_equal(key, "AllowedBearers") == TRUE) {
1164                                 allowed_bearers =
1165                                         session_parse_allowed_bearers(&value);
1166                         } else {
1167                                 return -EINVAL;
1168                         }
1169                         break;
1170                 case DBUS_TYPE_BOOLEAN:
1171                         if (g_str_equal(key, "Priority") == TRUE) {
1172                                 dbus_message_iter_get_basic(&value,
1173                                                         &priority);
1174                         } else if (g_str_equal(key, "AvoidHandover") == TRUE) {
1175                                 dbus_message_iter_get_basic(&value,
1176                                                         &avoid_handover);
1177                         } else if (g_str_equal(key, "StayConnected") == TRUE) {
1178                                 dbus_message_iter_get_basic(&value,
1179                                                         &stay_connected);
1180                         } else if (g_str_equal(key, "EmergencyCall") == TRUE) {
1181                                 dbus_message_iter_get_basic(&value,
1182                                                         &ecall);
1183                         } else {
1184                                 return -EINVAL;
1185                         }
1186                         break;
1187                 case DBUS_TYPE_UINT32:
1188                         if (g_str_equal(key, "PeriodicConnect") == TRUE) {
1189                                 dbus_message_iter_get_basic(&value,
1190                                                         &periodic_connect);
1191                         } else if (g_str_equal(key, "IdleTimeout") == TRUE) {
1192                                 dbus_message_iter_get_basic(&value,
1193                                                         &idle_timeout);
1194                         } else {
1195                                 return -EINVAL;
1196                         }
1197                         break;
1198                 case DBUS_TYPE_STRING:
1199                         if (g_str_equal(key, "RoamingPolicy") == TRUE) {
1200                                 dbus_message_iter_get_basic(&value, &val);
1201                                 roaming_policy = string2roamingpolicy(val);
1202                         } else {
1203                                 return -EINVAL;
1204                         }
1205                 }
1206                 dbus_message_iter_next(&array);
1207         }
1208
1209         dbus_message_iter_next(&iter);
1210         dbus_message_iter_get_basic(&iter, &notify_path);
1211
1212         if (notify_path == NULL) {
1213                 err = -EINVAL;
1214                 goto err;
1215         }
1216
1217         session_path = g_strdup_printf("/sessions%s", notify_path);
1218         if (session_path == NULL) {
1219                 err = -ENOMEM;
1220                 goto err;
1221         }
1222
1223         session = g_hash_table_lookup(session_hash, session_path);
1224         if (session != NULL) {
1225                 err = -EEXIST;
1226                 goto err;
1227         }
1228
1229         session = g_try_new0(struct connman_session, 1);
1230         if (session == NULL) {
1231                 err = -ENOMEM;
1232                 goto err;
1233         }
1234
1235         info = &session->info;
1236         info_last = &session->info_last;
1237
1238         session->owner = g_strdup(owner);
1239         session->session_path = session_path;
1240         session->notify_path = g_strdup(notify_path);
1241         session->notify_watch =
1242                 g_dbus_add_disconnect_watch(connection, session->owner,
1243                                         owner_disconnect, session, NULL);
1244
1245         info->bearer = "";
1246         info->online = FALSE;
1247         info->priority = priority;
1248         info->avoid_handover = avoid_handover;
1249         info->stay_connected = stay_connected;
1250         info->periodic_connect = periodic_connect;
1251         info->idle_timeout = idle_timeout;
1252         info->ecall = ecall;
1253         info->roaming_policy = roaming_policy;
1254         info->service = NULL;
1255         info->marker = 0;
1256
1257         if (allowed_bearers == NULL) {
1258                 info->allowed_bearers =
1259                                 session_allowed_bearers_any();
1260
1261                 if (info->allowed_bearers == NULL) {
1262                         err = -ENOMEM;
1263                         goto err;
1264                 }
1265         } else {
1266                 info->allowed_bearers = allowed_bearers;
1267         }
1268
1269         g_hash_table_replace(session_hash, session->session_path, session);
1270
1271         DBG("add %s", session->session_path);
1272
1273         if (g_dbus_register_interface(connection, session->session_path,
1274                                         CONNMAN_SESSION_INTERFACE,
1275                                         session_methods, NULL,
1276                                         NULL, session, NULL) == FALSE) {
1277                 connman_error("Failed to register %s", session->session_path);
1278                 g_hash_table_remove(session_hash, session->session_path);
1279                 session = NULL;
1280
1281                 err = -EINVAL;
1282                 goto err;
1283         }
1284
1285         g_dbus_send_reply(connection, msg,
1286                                 DBUS_TYPE_OBJECT_PATH, &session->session_path,
1287                                 DBUS_TYPE_INVALID);
1288
1289
1290         update_allowed_bearers(session);
1291         update_info(info);
1292         if (info->ecall == TRUE) {
1293                 ecall_session = session;
1294                 update_ecall_sessions(session);
1295         }
1296
1297         info_last->bearer = info->bearer;
1298         info_last->online = info->online;
1299         info_last->priority = info->priority;
1300         info_last->avoid_handover = info->avoid_handover;
1301         info_last->stay_connected = info->stay_connected;
1302         info_last->periodic_connect = info->periodic_connect;
1303         info_last->idle_timeout = info->idle_timeout;
1304         info_last->ecall = info->ecall;
1305         info_last->roaming_policy = info->roaming_policy;
1306         info_last->service = info->service;
1307         info_last->marker = info->marker;
1308         info_last->allowed_bearers = info->allowed_bearers;
1309         update_info(info_last);
1310
1311         session->info_dirty = TRUE;
1312         session->append_all = TRUE;
1313
1314         session_changed(session, CONNMAN_SESSION_TRIGGER_SETTING);
1315
1316         return 0;
1317
1318 err:
1319         connman_error("Failed to create session");
1320         g_free(session_path);
1321
1322         g_slist_foreach(allowed_bearers, cleanup_bearer_info, NULL);
1323         g_slist_free(allowed_bearers);
1324
1325         return err;
1326 }
1327
1328 int __connman_session_destroy(DBusMessage *msg)
1329 {
1330         const char *owner, *session_path;
1331         struct connman_session *session;
1332
1333         owner = dbus_message_get_sender(msg);
1334
1335         DBG("owner %s", owner);
1336
1337         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &session_path,
1338                                                         DBUS_TYPE_INVALID);
1339         if (session_path == NULL)
1340                 return -EINVAL;
1341
1342         session = g_hash_table_lookup(session_hash, session_path);
1343         if (session == NULL)
1344                 return -EINVAL;
1345
1346         if (g_strcmp0(owner, session->owner) != 0)
1347                 return -EACCES;
1348
1349         session_disconnect(session);
1350
1351         return 0;
1352 }
1353
1354 connman_bool_t __connman_session_mode()
1355 {
1356         return sessionmode;
1357 }
1358
1359 void __connman_session_set_mode(connman_bool_t enable)
1360 {
1361         DBG("enable %d", enable);
1362
1363         if (sessionmode == enable)
1364                 return;
1365
1366         sessionmode = enable;
1367
1368         if (sessionmode == TRUE)
1369                 __connman_service_disconnect_all();
1370 }
1371
1372 static void service_add(struct connman_service *service)
1373 {
1374         GHashTableIter iter;
1375         gpointer key, value;
1376         struct connman_session *session;
1377
1378         DBG("service %p", service);
1379
1380         g_hash_table_iter_init(&iter, session_hash);
1381
1382         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1383                 session = value;
1384
1385                 if (service_match(session, service) == FALSE)
1386                         continue;
1387
1388                 g_sequence_insert_sorted(session->service_list, service,
1389                                                 sort_services, session);
1390
1391                 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1392         }
1393 }
1394
1395 static int service_remove_from_session(struct connman_session *session,
1396                                         struct connman_service *service)
1397 {
1398         GSequenceIter *iter;
1399
1400         iter = lookup_service(session, service);
1401         if (iter == NULL)
1402                 return -ENOENT;
1403
1404         session->info.online = FALSE;
1405         g_sequence_remove(iter);
1406
1407         return 0;
1408 }
1409
1410 static void service_remove(struct connman_service *service)
1411 {
1412
1413         GHashTableIter iter;
1414         gpointer key, value;
1415         struct connman_session *session;
1416         struct session_info *info;
1417
1418         DBG("service %p", service);
1419
1420         g_hash_table_iter_init(&iter, session_hash);
1421
1422         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1423                 session = value;
1424                 info = &session->info;
1425
1426                 if (service_remove_from_session(session, service) != 0)
1427                         continue;
1428
1429                 info->service = NULL;
1430                 session_changed(session, CONNMAN_SESSION_TRIGGER_SERVICE);
1431         }
1432 }
1433
1434 static void service_state_changed(struct connman_service *service,
1435                                         enum connman_service_state state)
1436 {
1437         GHashTableIter iter;
1438         gpointer key, value;
1439         struct connman_session *session;
1440         struct session_info *info;
1441         connman_bool_t online;
1442
1443         DBG("service %p state %d", service, state);
1444
1445         g_hash_table_iter_init(&iter, session_hash);
1446
1447         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1448                 session = value;
1449                 info = &session->info;
1450
1451                 if (info->service == service) {
1452                         online = __connman_service_is_connected(service);
1453                         if (info->online == online)
1454                                 continue;
1455
1456                         info->online = online;
1457                         session->info_dirty = TRUE;
1458                         session_changed(session,
1459                                         CONNMAN_SESSION_TRIGGER_SERVICE);
1460                 }
1461         }
1462 }
1463
1464 static void ipconfig_changed(struct connman_service *service,
1465                                 struct connman_ipconfig *ipconfig)
1466 {
1467         GHashTableIter iter;
1468         gpointer key, value;
1469         struct connman_session *session;
1470         struct session_info *info;
1471         enum connman_ipconfig_type type;
1472
1473         DBG("service %p ipconfig %p", service, ipconfig);
1474
1475         type = __connman_ipconfig_get_config_type(ipconfig);
1476
1477         g_hash_table_iter_init(&iter, session_hash);
1478
1479         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
1480                 session = value;
1481                 info = &session->info;
1482
1483                 if (info->service == service) {
1484                         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1485                                 ipconfig_ipv4_changed(session);
1486                         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1487                                 ipconfig_ipv6_changed(session);
1488                 }
1489         }
1490 }
1491
1492 static struct connman_notifier session_notifier = {
1493         .name                   = "session",
1494         .service_add            = service_add,
1495         .service_remove         = service_remove,
1496         .service_state_changed  = service_state_changed,
1497         .ipconfig_changed       = ipconfig_changed,
1498 };
1499
1500 int __connman_session_init(void)
1501 {
1502         int err;
1503
1504         DBG("");
1505
1506         connection = connman_dbus_get_connection();
1507         if (connection == NULL)
1508                 return -1;
1509
1510         err = connman_notifier_register(&session_notifier);
1511         if (err < 0) {
1512                 dbus_connection_unref(connection);
1513                 return err;
1514         }
1515
1516         session_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1517                                                 NULL, cleanup_session);
1518
1519         sessionmode = FALSE;
1520         return 0;
1521 }
1522
1523 void __connman_session_cleanup(void)
1524 {
1525         DBG("");
1526
1527         if (connection == NULL)
1528                 return;
1529
1530         connman_notifier_unregister(&session_notifier);
1531
1532         g_hash_table_foreach(session_hash, release_session, NULL);
1533         g_hash_table_destroy(session_hash);
1534
1535         dbus_connection_unref(connection);
1536 }