893ccba980e6557b754ff002ec1757a560a1faf8
[framework/connectivity/connman.git] / src / service.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 <stdio.h>
27 #include <string.h>
28 #include <gdbus.h>
29
30 #include "connman.h"
31
32 #define CONNECT_TIMEOUT         120
33
34 static DBusConnection *connection = NULL;
35
36 static GSequence *service_list = NULL;
37 static GHashTable *service_hash = NULL;
38
39 struct connman_stats {
40         connman_bool_t valid;
41         connman_bool_t enabled;
42         unsigned int rx_packets_last;
43         unsigned int tx_packets_last;
44         unsigned int rx_packets;
45         unsigned int tx_packets;
46         unsigned int rx_bytes_last;
47         unsigned int tx_bytes_last;
48         unsigned int rx_bytes;
49         unsigned int tx_bytes;
50         unsigned int rx_errors_last;
51         unsigned int tx_errors_last;
52         unsigned int rx_errors;
53         unsigned int tx_errors;
54         unsigned int rx_dropped_last;
55         unsigned int tx_dropped_last;
56         unsigned int rx_dropped;
57         unsigned int tx_dropped;
58         unsigned int time_start;
59         unsigned int time;
60         GTimer *timer;
61 };
62
63 struct connman_service {
64         gint refcount;
65         char *identifier;
66         char *path;
67         enum connman_service_type type;
68         enum connman_service_mode mode;
69         enum connman_service_security security;
70         enum connman_service_state state;
71         enum connman_service_error error;
72         connman_uint8_t strength;
73         connman_bool_t favorite;
74         connman_bool_t immutable;
75         connman_bool_t hidden;
76         connman_bool_t ignore;
77         connman_bool_t autoconnect;
78         connman_bool_t userconnect;
79         GTimeVal modified;
80         unsigned int order;
81         char *name;
82         char *passphrase;
83         char *profile;
84         char *apn;
85         char *username;
86         char *password;
87         char *mcc;
88         char *mnc;
89         connman_bool_t roaming;
90         connman_bool_t login_required;
91         struct connman_ipconfig *ipconfig;
92         struct connman_network *network;
93         struct connman_provider *provider;
94         char **nameservers;
95         char *nameserver;
96         char **domains;
97         char *domainname;
98         /* 802.1x settings from the config files */
99         char *eap;
100         char *identity;
101         char *ca_cert_file;
102         char *client_cert_file;
103         char *private_key_file;
104         char *private_key_passphrase;
105         char *phase2;
106         DBusMessage *pending;
107         guint timeout;
108         struct connman_location *location;
109         struct connman_stats stats;
110 };
111
112 static void append_path(gpointer value, gpointer user_data)
113 {
114         struct connman_service *service = value;
115         DBusMessageIter *iter = user_data;
116
117         if (service->path == NULL || service->hidden == TRUE)
118                 return;
119
120         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
121                                                         &service->path);
122 }
123
124 void __connman_service_list(DBusMessageIter *iter, void *user_data)
125 {
126         g_sequence_foreach(service_list, append_path, iter);
127 }
128
129 struct find_data {
130         const char *path;
131         struct connman_service *service;
132 };
133
134 static void compare_path(gpointer value, gpointer user_data)
135 {
136         struct connman_service *service = value;
137         struct find_data *data = user_data;
138
139         if (data->service != NULL)
140                 return;
141
142         if (g_strcmp0(service->path, data->path) == 0)
143                 data->service = service;
144 }
145
146 static struct connman_service *find_service(const char *path)
147 {
148         struct find_data data = { .path = path, .service = NULL };
149
150         DBG("path %s", path);
151
152         g_sequence_foreach(service_list, compare_path, &data);
153
154         return data.service;
155 }
156
157 const char *__connman_service_type2string(enum connman_service_type type)
158 {
159         switch (type) {
160         case CONNMAN_SERVICE_TYPE_UNKNOWN:
161                 break;
162         case CONNMAN_SERVICE_TYPE_SYSTEM:
163                 return "system";
164         case CONNMAN_SERVICE_TYPE_ETHERNET:
165                 return "ethernet";
166         case CONNMAN_SERVICE_TYPE_WIFI:
167                 return "wifi";
168         case CONNMAN_SERVICE_TYPE_WIMAX:
169                 return "wimax";
170         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
171                 return "bluetooth";
172         case CONNMAN_SERVICE_TYPE_CELLULAR:
173                 return "cellular";
174         case CONNMAN_SERVICE_TYPE_GPS:
175                 return "gps";
176         case CONNMAN_SERVICE_TYPE_VPN:
177                 return "vpn";
178         }
179
180         return NULL;
181 }
182
183 static const char *mode2string(enum connman_service_mode mode)
184 {
185         switch (mode) {
186         case CONNMAN_SERVICE_MODE_UNKNOWN:
187                 break;
188         case CONNMAN_SERVICE_MODE_MANAGED:
189                 return "managed";
190         case CONNMAN_SERVICE_MODE_ADHOC:
191                 return "adhoc";
192         case CONNMAN_SERVICE_MODE_GPRS:
193                 return "gprs";
194         case CONNMAN_SERVICE_MODE_EDGE:
195                 return "edge";
196         case CONNMAN_SERVICE_MODE_UMTS:
197                 return "umts";
198         }
199
200         return NULL;
201 }
202
203 static const char *security2string(enum connman_service_security security)
204 {
205         switch (security) {
206         case CONNMAN_SERVICE_SECURITY_UNKNOWN:
207                 break;
208         case CONNMAN_SERVICE_SECURITY_NONE:
209                 return "none";
210         case CONNMAN_SERVICE_SECURITY_WEP:
211                 return "wep";
212         case CONNMAN_SERVICE_SECURITY_PSK:
213                 return "psk";
214         case CONNMAN_SERVICE_SECURITY_8021X:
215                 return "ieee8021x";
216         case CONNMAN_SERVICE_SECURITY_WPA:
217                 return "wpa";
218         case CONNMAN_SERVICE_SECURITY_RSN:
219                 return "rsn";
220         }
221
222         return NULL;
223 }
224
225 static const char *state2string(enum connman_service_state state)
226 {
227         switch (state) {
228         case CONNMAN_SERVICE_STATE_UNKNOWN:
229                 break;
230         case CONNMAN_SERVICE_STATE_IDLE:
231                 return "idle";
232         case CONNMAN_SERVICE_STATE_ASSOCIATION:
233                 return "association";
234         case CONNMAN_SERVICE_STATE_CONFIGURATION:
235                 return "configuration";
236         case CONNMAN_SERVICE_STATE_READY:
237                 return "ready";
238         case CONNMAN_SERVICE_STATE_ONLINE:
239                 return "online";
240         case CONNMAN_SERVICE_STATE_DISCONNECT:
241                 return "disconnect";
242         case CONNMAN_SERVICE_STATE_FAILURE:
243                 return "failure";
244         }
245
246         return NULL;
247 }
248
249 static const char *error2string(enum connman_service_error error)
250 {
251         switch (error) {
252         case CONNMAN_SERVICE_ERROR_UNKNOWN:
253                 break;
254         case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
255                 return "out-of-range";
256         case CONNMAN_SERVICE_ERROR_PIN_MISSING:
257                 return "pin-missing";
258         case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
259                 return "dhcp-failed";
260         case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
261                 return "connect-failed";
262         }
263
264         return NULL;
265 }
266
267 static enum connman_service_error string2error(const char *error)
268 {
269         if (g_strcmp0(error, "dhcp-failed") == 0)
270                 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
271         else if (g_strcmp0(error, "pin-missing") == 0)
272                 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
273
274         return CONNMAN_SERVICE_ERROR_UNKNOWN;
275 }
276
277 static connman_bool_t is_connecting(struct connman_service *service)
278 {
279         switch (service->state) {
280         case CONNMAN_SERVICE_STATE_UNKNOWN:
281         case CONNMAN_SERVICE_STATE_IDLE:
282         case CONNMAN_SERVICE_STATE_FAILURE:
283         case CONNMAN_SERVICE_STATE_DISCONNECT:
284         case CONNMAN_SERVICE_STATE_READY:
285         case CONNMAN_SERVICE_STATE_ONLINE:
286                 break;
287         case CONNMAN_SERVICE_STATE_ASSOCIATION:
288         case CONNMAN_SERVICE_STATE_CONFIGURATION:
289                 return TRUE;
290         }
291
292         return FALSE;
293 }
294
295 static connman_bool_t is_connected(const struct connman_service *service)
296 {
297         switch (service->state) {
298         case CONNMAN_SERVICE_STATE_UNKNOWN:
299         case CONNMAN_SERVICE_STATE_IDLE:
300         case CONNMAN_SERVICE_STATE_ASSOCIATION:
301         case CONNMAN_SERVICE_STATE_CONFIGURATION:
302         case CONNMAN_SERVICE_STATE_DISCONNECT:
303         case CONNMAN_SERVICE_STATE_FAILURE:
304                 break;
305         case CONNMAN_SERVICE_STATE_READY:
306         case CONNMAN_SERVICE_STATE_ONLINE:
307                 return TRUE;
308         }
309
310         return FALSE;
311 }
312
313 static void update_nameservers(struct connman_service *service)
314 {
315         const char *ifname = connman_ipconfig_get_ifname(service->ipconfig);
316
317         if (ifname == NULL)
318                 return;
319
320         switch (service->state) {
321         case CONNMAN_SERVICE_STATE_UNKNOWN:
322         case CONNMAN_SERVICE_STATE_IDLE:
323         case CONNMAN_SERVICE_STATE_ASSOCIATION:
324         case CONNMAN_SERVICE_STATE_CONFIGURATION:
325                 return;
326         case CONNMAN_SERVICE_STATE_FAILURE:
327         case CONNMAN_SERVICE_STATE_DISCONNECT:
328                 connman_resolver_remove_all(ifname);
329                 return;
330         case CONNMAN_SERVICE_STATE_READY:
331         case CONNMAN_SERVICE_STATE_ONLINE:
332                 break;
333         }
334
335         connman_resolver_remove_all(ifname);
336
337         if (service->nameservers != NULL) {
338                 int i;
339
340                 for (i = 0; service->nameservers[i]; i++)
341                         connman_resolver_append(ifname, NULL,
342                                                 service->nameservers[i]);
343         } else if (service->nameserver != NULL)
344                 connman_resolver_append(ifname, NULL, service->nameserver);
345
346         connman_resolver_flush();
347 }
348
349 void __connman_service_append_nameserver(struct connman_service *service,
350                                                 const char *nameserver)
351 {
352         DBG("service %p nameserver %s", service, nameserver);
353
354         if (nameserver == NULL)
355                 return;
356
357         g_free(service->nameserver);
358         service->nameserver = g_strdup(nameserver);
359
360         update_nameservers(service);
361 }
362
363 void __connman_service_remove_nameserver(struct connman_service *service,
364                                                 const char *nameserver)
365 {
366         DBG("service %p nameserver %s", service, nameserver);
367
368         if (nameserver == NULL)
369                 return;
370
371         g_free(service->nameserver);
372         service->nameserver = NULL;
373
374         update_nameservers(service);
375 }
376
377 void __connman_service_nameserver_add_routes(struct connman_service *service,
378                                                 const char *gw)
379 {
380         int index;
381
382         if (service == NULL)
383                 return;
384
385         index = connman_network_get_index(service->network);
386
387         if (service->nameservers != NULL) {
388                 int i;
389
390                 /*
391                  * We add nameservers host routes for nameservers that
392                  * are not on our subnet. For those who are, the subnet
393                  * route will be installed by the time the dns proxy code
394                  * tries to reach them. The subnet route is installed
395                  * when setting the interface IP address.
396                  */
397                 for (i = 0; service->nameservers[i]; i++) {
398                         if (connman_inet_compare_subnet(index,
399                                                         service->nameservers[i]))
400                                 continue;
401
402                         connman_inet_add_host_route(index,
403                                                 service->nameservers[i], gw);
404                 }
405         } else if (service->nameserver != NULL) {
406                 if (connman_inet_compare_subnet(index, service->nameserver))
407                         return;
408
409                 connman_inet_add_host_route(index, service->nameserver, gw);
410         }
411 }
412
413 void __connman_service_nameserver_del_routes(struct connman_service *service)
414 {
415         int index;
416
417         if (service == NULL)
418                 return;
419
420         index = connman_network_get_index(service->network);
421
422         if (service->nameservers != NULL) {
423                 int i;
424
425                 for (i = 0; service->nameservers[i]; i++)
426                         connman_inet_del_host_route(index,
427                                                 service->nameservers[i]);
428         } else if (service->nameserver != NULL) {
429                 connman_inet_del_host_route(index, service->nameserver);
430         }
431 }
432
433 static void stats_start(struct connman_service *service)
434 {
435         DBG("service %p", service);
436
437         if (service->stats.timer == NULL)
438                 return;
439
440         service->stats.enabled = TRUE;
441
442         service->stats.time_start = service->stats.time;
443
444         g_timer_start(service->stats.timer);
445
446         __connman_counter_add_service(service);
447 }
448
449 static void stats_stop(struct connman_service *service)
450 {
451         unsigned int seconds;
452
453         DBG("service %p", service);
454
455         if (service->stats.timer == NULL)
456                 return;
457
458         if (service->stats.enabled == FALSE)
459                 return;
460
461         __connman_counter_remove_service(service);
462
463         g_timer_stop(service->stats.timer);
464
465         seconds = g_timer_elapsed(service->stats.timer, NULL);
466         service->stats.time = service->stats.time_start + seconds;
467
468         service->stats.enabled = FALSE;
469 }
470
471 static int stats_load(struct connman_service *service,
472                 GKeyFile *keyfile, const char *identifier)
473 {
474         service->stats.rx_packets = g_key_file_get_integer(keyfile,
475                                 identifier, "rx_packets", NULL);
476         service->stats.tx_packets = g_key_file_get_integer(keyfile,
477                                 identifier, "tx_packets", NULL);
478         service->stats.rx_bytes = g_key_file_get_integer(keyfile,
479                                 identifier, "rx_bytes", NULL);
480         service->stats.tx_bytes = g_key_file_get_integer(keyfile,
481                                 identifier, "tx_bytes", NULL);
482         service->stats.rx_errors = g_key_file_get_integer(keyfile,
483                                 identifier, "rx_errors", NULL);
484         service->stats.tx_errors = g_key_file_get_integer(keyfile,
485                                 identifier, "tx_errors", NULL);
486         service->stats.rx_dropped = g_key_file_get_integer(keyfile,
487                                 identifier, "rx_dropped", NULL);
488         service->stats.tx_dropped = g_key_file_get_integer(keyfile,
489                                 identifier, "tx_dropped", NULL);
490         service->stats.time = g_key_file_get_integer(keyfile,
491                                 identifier, "time", NULL);
492
493         return 0;
494 }
495
496 static int stats_save(struct connman_service *service,
497                 GKeyFile *keyfile, const char *identifier)
498 {
499         g_key_file_set_integer(keyfile, identifier, "rx_packets",
500                         service->stats.rx_packets);
501         g_key_file_set_integer(keyfile, identifier, "tx_packets",
502                         service->stats.tx_packets);
503         g_key_file_set_integer(keyfile, identifier, "rx_bytes",
504                         service->stats.rx_bytes);
505         g_key_file_set_integer(keyfile, identifier, "tx_bytes",
506                         service->stats.tx_bytes);
507         g_key_file_set_integer(keyfile, identifier, "rx_errors",
508                         service->stats.rx_errors);
509         g_key_file_set_integer(keyfile, identifier, "tx_errors",
510                         service->stats.tx_errors);
511         g_key_file_set_integer(keyfile, identifier, "rx_dropped",
512                         service->stats.rx_dropped);
513         g_key_file_set_integer(keyfile, identifier, "tx_dropped",
514                         service->stats.tx_dropped);
515         g_key_file_set_integer(keyfile, identifier, "time",
516                         service->stats.time);
517
518         return 0;
519 }
520
521 static void reset_stats(struct connman_service *service)
522 {
523         DBG("service %p", service);
524
525         service->stats.valid = FALSE;
526         service->stats.rx_packets = 0;
527         service->stats.tx_packets = 0;
528         service->stats.rx_bytes = 0;
529         service->stats.tx_bytes = 0;
530         service->stats.rx_errors = 0;
531         service->stats.tx_errors = 0;
532         service->stats.rx_dropped = 0;
533         service->stats.tx_dropped = 0;
534         service->stats.time = 0;
535         service->stats.time_start = 0;
536         g_timer_reset(service->stats.timer);
537
538 }
539
540 unsigned long __connman_service_stats_get_rx_packets(struct connman_service *service)
541 {
542         return service->stats.rx_packets;
543 }
544
545 unsigned long __connman_service_stats_get_tx_packets(struct connman_service *service)
546 {
547         return service->stats.tx_packets;
548 }
549
550 unsigned long __connman_service_stats_get_rx_bytes(struct connman_service *service)
551 {
552         return service->stats.rx_bytes;
553 }
554
555 unsigned long __connman_service_stats_get_tx_bytes(struct connman_service *service)
556 {
557         return service->stats.tx_bytes;
558 }
559
560 unsigned long __connman_service_stats_get_rx_errors(struct connman_service *service)
561 {
562         return service->stats.rx_errors;
563 }
564
565 unsigned long __connman_service_stats_get_tx_errors(struct connman_service *service)
566 {
567         return service->stats.tx_errors;
568 }
569
570 unsigned long __connman_service_stats_get_rx_dropped(struct connman_service *service)
571 {
572         return service->stats.rx_dropped;
573 }
574
575 unsigned long __connman_service_stats_get_tx_dropped(struct connman_service *service)
576 {
577         return service->stats.tx_dropped;
578 }
579
580 unsigned long __connman_service_stats_get_time(struct connman_service *service)
581 {
582         return service->stats.time;
583 }
584
585 static struct connman_service *get_default(void)
586 {
587         struct connman_service *service;
588         GSequenceIter *iter;
589
590         iter = g_sequence_get_begin_iter(service_list);
591
592         if (g_sequence_iter_is_end(iter) == TRUE)
593                 return NULL;
594
595         service = g_sequence_get(iter);
596
597         if (is_connected(service) == FALSE)
598                 return NULL;
599
600         return service;
601 }
602
603 static void default_changed(void)
604 {
605         struct connman_service *service = get_default();
606
607         __connman_notifier_default_changed(service);
608 }
609
610 const char *__connman_service_default(void)
611 {
612         struct connman_service *service;
613
614         service = get_default();
615         if (service == NULL)
616                 return "";
617
618         return __connman_service_type2string(service->type);
619 }
620
621 static void mode_changed(struct connman_service *service)
622 {
623         const char *str;
624
625         str = mode2string(service->mode);
626         if (str == NULL)
627                 return;
628
629         connman_dbus_property_changed_basic(service->path,
630                                 CONNMAN_SERVICE_INTERFACE, "Mode",
631                                                 DBUS_TYPE_STRING, &str);
632 }
633
634 static void state_changed(struct connman_service *service)
635 {
636         const char *str;
637
638         str = state2string(service->state);
639         if (str == NULL)
640                 return;
641
642         connman_dbus_property_changed_basic(service->path,
643                                 CONNMAN_SERVICE_INTERFACE, "State",
644                                                 DBUS_TYPE_STRING, &str);
645 }
646
647 static void strength_changed(struct connman_service *service)
648 {
649         if (service->strength == 0)
650                 return;
651
652         connman_dbus_property_changed_basic(service->path,
653                                 CONNMAN_SERVICE_INTERFACE, "Strength",
654                                         DBUS_TYPE_BYTE, &service->strength);
655 }
656
657 static void favorite_changed(struct connman_service *service)
658 {
659         if (service->path == NULL)
660                 return;
661
662         connman_dbus_property_changed_basic(service->path,
663                                 CONNMAN_SERVICE_INTERFACE, "Favorite",
664                                         DBUS_TYPE_BOOLEAN, &service->favorite);
665 }
666
667 static void immutable_changed(struct connman_service *service)
668 {
669         if (service->path == NULL)
670                 return;
671
672         connman_dbus_property_changed_basic(service->path,
673                                 CONNMAN_SERVICE_INTERFACE, "Immutable",
674                                         DBUS_TYPE_BOOLEAN, &service->immutable);
675 }
676
677 static void roaming_changed(struct connman_service *service)
678 {
679         if (service->path == NULL)
680                 return;
681
682         connman_dbus_property_changed_basic(service->path,
683                                 CONNMAN_SERVICE_INTERFACE, "Roaming",
684                                         DBUS_TYPE_BOOLEAN, &service->roaming);
685 }
686
687 static void autoconnect_changed(struct connman_service *service)
688 {
689         if (service->path == NULL)
690                 return;
691
692         connman_dbus_property_changed_basic(service->path,
693                                 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
694                                 DBUS_TYPE_BOOLEAN, &service->autoconnect);
695 }
696
697 static void passphrase_changed(struct connman_service *service)
698 {
699         dbus_bool_t required;
700
701         switch (service->type) {
702         case CONNMAN_SERVICE_TYPE_UNKNOWN:
703         case CONNMAN_SERVICE_TYPE_SYSTEM:
704         case CONNMAN_SERVICE_TYPE_ETHERNET:
705         case CONNMAN_SERVICE_TYPE_WIMAX:
706         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
707         case CONNMAN_SERVICE_TYPE_CELLULAR:
708         case CONNMAN_SERVICE_TYPE_GPS:
709         case CONNMAN_SERVICE_TYPE_VPN:
710                 return;
711         case CONNMAN_SERVICE_TYPE_WIFI:
712                 required = FALSE;
713
714                 switch (service->security) {
715                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
716                 case CONNMAN_SERVICE_SECURITY_NONE:
717                         break;
718                 case CONNMAN_SERVICE_SECURITY_WEP:
719                 case CONNMAN_SERVICE_SECURITY_PSK:
720                 case CONNMAN_SERVICE_SECURITY_WPA:
721                 case CONNMAN_SERVICE_SECURITY_RSN:
722                         if (service->passphrase == NULL)
723                                 required = TRUE;
724                         break;
725                 case CONNMAN_SERVICE_SECURITY_8021X:
726                         break;
727                 }
728                 break;
729         }
730
731         connman_dbus_property_changed_basic(service->path,
732                                 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
733                                                 DBUS_TYPE_BOOLEAN, &required);
734 }
735
736 static void login_changed(struct connman_service *service)
737 {
738         dbus_bool_t required = service->login_required;
739
740         if (service->path == NULL)
741                 return;
742
743         connman_dbus_property_changed_basic(service->path,
744                                 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
745                                                 DBUS_TYPE_BOOLEAN, &required);
746 }
747
748 static void apn_changed(struct connman_service *service)
749 {
750         dbus_bool_t required;
751
752         switch (service->type) {
753         case CONNMAN_SERVICE_TYPE_UNKNOWN:
754         case CONNMAN_SERVICE_TYPE_SYSTEM:
755         case CONNMAN_SERVICE_TYPE_ETHERNET:
756         case CONNMAN_SERVICE_TYPE_WIMAX:
757         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
758         case CONNMAN_SERVICE_TYPE_WIFI:
759         case CONNMAN_SERVICE_TYPE_GPS:
760         case CONNMAN_SERVICE_TYPE_VPN:
761                 return;
762         case CONNMAN_SERVICE_TYPE_CELLULAR:
763                 break;
764         }
765
766         required = (service->apn == NULL) ? TRUE : FALSE;
767
768         connman_dbus_property_changed_basic(service->path,
769                                 CONNMAN_SERVICE_INTERFACE, "SetupRequired",
770                                                 DBUS_TYPE_BOOLEAN, &required);
771 }
772
773 static void append_ethernet(DBusMessageIter *iter, void *user_data)
774 {
775         struct connman_service *service = user_data;
776
777         if (is_connecting(service) == FALSE &&
778                         is_connected(service) == FALSE)
779                 return;
780
781         if (service->ipconfig != NULL)
782                 __connman_ipconfig_append_ethernet(service->ipconfig, iter);
783 }
784
785 static void append_ipv4(DBusMessageIter *iter, void *user_data)
786 {
787         struct connman_service *service = user_data;
788
789         if (is_connected(service) == FALSE)
790                 return;
791
792         if (service->ipconfig != NULL)
793                 __connman_ipconfig_append_ipv4(service->ipconfig, iter);
794 }
795
796 static void append_ipv6(DBusMessageIter *iter, void *user_data)
797 {
798         struct connman_service *service = user_data;
799         struct connman_ipconfig *ipv6config;
800
801         if (is_connected(service) == FALSE)
802                 return;
803
804         if (service->ipconfig == NULL)
805                 return;
806
807         ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
808         if (ipv6config == NULL)
809                 return;
810
811         __connman_ipconfig_append_ipv6(ipv6config, iter);
812 }
813
814 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
815 {
816         struct connman_service *service = user_data;
817
818         if (service->ipconfig != NULL)
819                 __connman_ipconfig_append_ipv4config(service->ipconfig, iter);
820 }
821
822 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
823 {
824         struct connman_service *service = user_data;
825         struct connman_ipconfig *ipv6config;
826
827         if (service->ipconfig == NULL)
828                 return;
829
830         ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
831         if (ipv6config == NULL)
832                 return;
833
834         __connman_ipconfig_append_ipv6config(ipv6config, iter);
835 }
836
837 static void append_dns(DBusMessageIter *iter, void *user_data)
838 {
839         struct connman_service *service = user_data;
840
841         if (is_connected(service) == FALSE)
842                 return;
843
844         if (service->nameservers != NULL) {
845                 int i;
846
847                 for (i = 0; service->nameservers[i]; i++)
848                         dbus_message_iter_append_basic(iter,
849                                 DBUS_TYPE_STRING, &service->nameservers[i]);
850
851                 return;
852         }
853
854         if (service->nameserver == NULL)
855                 return;
856
857         dbus_message_iter_append_basic(iter,
858                                 DBUS_TYPE_STRING, &service->nameserver);
859 }
860
861 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
862 {
863         struct connman_service *service = user_data;
864         int i;
865
866         if (service->nameservers == NULL)
867                 return;
868
869         for (i = 0; service->nameservers[i]; i++)
870                 dbus_message_iter_append_basic(iter,
871                                 DBUS_TYPE_STRING, &service->nameservers[i]);
872 }
873
874 static void append_domain(DBusMessageIter *iter, void *user_data)
875 {
876         struct connman_service *service = user_data;
877
878         if (is_connected(service) == FALSE && is_connecting(service) == FALSE)
879                 return;
880
881         if (service->domainname == NULL)
882                 return;
883
884         dbus_message_iter_append_basic(iter,
885                                 DBUS_TYPE_STRING, &service->domainname);
886 }
887
888 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
889 {
890         struct connman_service *service = user_data;
891         int i;
892
893         if (service->domains == NULL)
894                 return;
895
896         for (i = 0; service->domains[i]; i++)
897                 dbus_message_iter_append_basic(iter,
898                                 DBUS_TYPE_STRING, &service->domains[i]);
899 }
900
901 static void append_proxy(DBusMessageIter *iter, void *user_data)
902 {
903         struct connman_service *service = user_data;
904
905         if (is_connected(service) == FALSE)
906                 return;
907
908         if (service->ipconfig != NULL)
909                 __connman_ipconfig_append_proxy(service->ipconfig, iter);
910 }
911
912 static void append_provider(DBusMessageIter *iter, void *user_data)
913 {
914         struct connman_service *service = user_data;
915
916         DBG("%p %p", service, service->provider);
917
918         if (is_connected(service) == FALSE)
919                 return;
920
921         if (service->provider != NULL)
922                 __connman_provider_append_properties(service->provider, iter);
923 }
924
925
926 static void settings_changed(struct connman_service *service)
927 {
928         connman_dbus_property_changed_dict(service->path,
929                                         CONNMAN_SERVICE_INTERFACE, "IPv4",
930                                                         append_ipv4, service);
931
932         connman_dbus_property_changed_dict(service->path,
933                                         CONNMAN_SERVICE_INTERFACE, "IPv6",
934                                                         append_ipv6, service);
935 }
936
937 static void ipv4_configuration_changed(struct connman_service *service)
938 {
939         connman_dbus_property_changed_dict(service->path,
940                                         CONNMAN_SERVICE_INTERFACE,
941                                                         "IPv4.Configuration",
942                                                         append_ipv4config,
943                                                         service);
944 }
945
946 static void ipv6_configuration_changed(struct connman_service *service)
947 {
948         connman_dbus_property_changed_dict(service->path,
949                                         CONNMAN_SERVICE_INTERFACE,
950                                                         "IPv6.Configuration",
951                                                         append_ipv6config,
952                                                         service);
953 }
954
955 static void dns_changed(struct connman_service *service)
956 {
957         connman_dbus_property_changed_array(service->path,
958                                 CONNMAN_SERVICE_INTERFACE, "Nameservers",
959                                         DBUS_TYPE_STRING, append_dns, service);
960 }
961
962 static void dns_configuration_changed(struct connman_service *service)
963 {
964         connman_dbus_property_changed_array(service->path,
965                                 CONNMAN_SERVICE_INTERFACE,
966                                 "Nameservers.Configuration",
967                                 DBUS_TYPE_STRING, append_dnsconfig, service);
968
969         dns_changed(service);
970 }
971
972 static void domain_changed(struct connman_service *service)
973 {
974         connman_dbus_property_changed_array(service->path,
975                                 CONNMAN_SERVICE_INTERFACE, "Domains",
976                                 DBUS_TYPE_STRING, append_domain, service);
977 }
978
979 static void domain_configuration_changed(struct connman_service *service)
980 {
981         connman_dbus_property_changed_array(service->path,
982                                 CONNMAN_SERVICE_INTERFACE,
983                                 "Domains.Configuration",
984                                 DBUS_TYPE_STRING, append_domainconfig, service);
985 }
986
987 static void proxy_changed(struct connman_service *service)
988 {
989         connman_dbus_property_changed_dict(service->path,
990                                         CONNMAN_SERVICE_INTERFACE, "Proxy",
991                                                         append_proxy, service);
992 }
993
994 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
995                                         struct connman_service *service)
996 {
997         dbus_bool_t required;
998         const char *str;
999
1000         str = __connman_service_type2string(service->type);
1001         if (str != NULL)
1002                 connman_dbus_dict_append_basic(dict, "Type",
1003                                                 DBUS_TYPE_STRING, &str);
1004
1005         str = mode2string(service->mode);
1006         if (str != NULL)
1007                 connman_dbus_dict_append_basic(dict, "Mode",
1008                                                 DBUS_TYPE_STRING, &str);
1009
1010         str = security2string(service->security);
1011         if (str != NULL)
1012                 connman_dbus_dict_append_basic(dict, "Security",
1013                                                 DBUS_TYPE_STRING, &str);
1014
1015         str = state2string(service->state);
1016         if (str != NULL)
1017                 connman_dbus_dict_append_basic(dict, "State",
1018                                                 DBUS_TYPE_STRING, &str);
1019
1020         str = error2string(service->error);
1021         if (str != NULL)
1022                 connman_dbus_dict_append_basic(dict, "Error",
1023                                                 DBUS_TYPE_STRING, &str);
1024
1025         if (service->strength > 0)
1026                 connman_dbus_dict_append_basic(dict, "Strength",
1027                                         DBUS_TYPE_BYTE, &service->strength);
1028
1029         connman_dbus_dict_append_basic(dict, "Favorite",
1030                                         DBUS_TYPE_BOOLEAN, &service->favorite);
1031
1032         connman_dbus_dict_append_basic(dict, "Immutable",
1033                                         DBUS_TYPE_BOOLEAN, &service->immutable);
1034
1035         if (service->favorite == TRUE)
1036                 connman_dbus_dict_append_basic(dict, "AutoConnect",
1037                                 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1038         else
1039                 connman_dbus_dict_append_basic(dict, "AutoConnect",
1040                                         DBUS_TYPE_BOOLEAN, &service->favorite);
1041
1042         if (service->name != NULL)
1043                 connman_dbus_dict_append_basic(dict, "Name",
1044                                         DBUS_TYPE_STRING, &service->name);
1045
1046         connman_dbus_dict_append_basic(dict, "LoginRequired",
1047                                 DBUS_TYPE_BOOLEAN, &service->login_required);
1048
1049         switch (service->type) {
1050         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1051         case CONNMAN_SERVICE_TYPE_SYSTEM:
1052         case CONNMAN_SERVICE_TYPE_GPS:
1053         case CONNMAN_SERVICE_TYPE_VPN:
1054                 break;
1055         case CONNMAN_SERVICE_TYPE_CELLULAR:
1056                 connman_dbus_dict_append_basic(dict, "Roaming",
1057                                         DBUS_TYPE_BOOLEAN, &service->roaming);
1058
1059                 if (service->mcc != NULL && service->mnc != NULL) {
1060                         connman_dbus_dict_append_basic(dict, "MCC",
1061                                         DBUS_TYPE_STRING, &service->mcc);
1062                         connman_dbus_dict_append_basic(dict, "MNC",
1063                                         DBUS_TYPE_STRING, &service->mnc);
1064                 }
1065
1066                 if (service->apn != NULL) {
1067                         connman_dbus_dict_append_basic(dict, "APN",
1068                                         DBUS_TYPE_STRING, &service->apn);
1069
1070                         if (service->username != NULL)
1071                                 connman_dbus_dict_append_basic(dict,
1072                                         "Username", DBUS_TYPE_STRING,
1073                                                         &service->username);
1074
1075                         if (service->password != NULL)
1076                                 connman_dbus_dict_append_basic(dict,
1077                                         "Password", DBUS_TYPE_STRING,
1078                                                         &service->password);
1079
1080                         required = FALSE;
1081                 } else
1082                         required = TRUE;
1083
1084                 connman_dbus_dict_append_basic(dict, "SetupRequired",
1085                                                 DBUS_TYPE_BOOLEAN, &required);
1086                 connman_dbus_dict_append_dict(dict, "Ethernet",
1087                                                 append_ethernet, service);
1088                 break;
1089         case CONNMAN_SERVICE_TYPE_WIFI:
1090                 if (service->passphrase != NULL && limited == FALSE)
1091                         connman_dbus_dict_append_basic(dict, "Passphrase",
1092                                 DBUS_TYPE_STRING, &service->passphrase);
1093
1094                 required = FALSE;
1095
1096                 switch (service->security) {
1097                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1098                 case CONNMAN_SERVICE_SECURITY_NONE:
1099                         break;
1100                 case CONNMAN_SERVICE_SECURITY_WEP:
1101                 case CONNMAN_SERVICE_SECURITY_PSK:
1102                 case CONNMAN_SERVICE_SECURITY_WPA:
1103                 case CONNMAN_SERVICE_SECURITY_RSN:
1104                         if (service->passphrase == NULL)
1105                                 required = TRUE;
1106                         break;
1107                 case CONNMAN_SERVICE_SECURITY_8021X:
1108                         break;
1109                 }
1110
1111                 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1112                                                 DBUS_TYPE_BOOLEAN, &required);
1113                 /* fall through */
1114         case CONNMAN_SERVICE_TYPE_ETHERNET:
1115         case CONNMAN_SERVICE_TYPE_WIMAX:
1116         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1117                 connman_dbus_dict_append_dict(dict, "Ethernet",
1118                                                 append_ethernet, service);
1119                 break;
1120         }
1121
1122         connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1123
1124         connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1125                                                 append_ipv4config, service);
1126
1127         connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1128
1129         connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1130                                                 append_ipv6config, service);
1131
1132         connman_dbus_dict_append_array(dict, "Nameservers",
1133                                 DBUS_TYPE_STRING, append_dns, service);
1134
1135         connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1136                                 DBUS_TYPE_STRING, append_dnsconfig, service);
1137
1138         connman_dbus_dict_append_array(dict, "Domains",
1139                                 DBUS_TYPE_STRING, append_domain, service);
1140
1141         connman_dbus_dict_append_array(dict, "Domains.Configuration",
1142                                 DBUS_TYPE_STRING, append_domainconfig, service);
1143
1144         connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1145
1146         connman_dbus_dict_append_dict(dict, "Provider", append_provider, service);
1147 }
1148
1149 static void append_struct(gpointer value, gpointer user_data)
1150 {
1151         struct connman_service *service = value;
1152         DBusMessageIter *iter = user_data;
1153         DBusMessageIter entry, dict;
1154
1155         if (service->path == NULL || service->hidden == TRUE)
1156                 return;
1157
1158         dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1159
1160         dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1161                                                         &service->path);
1162
1163         connman_dbus_dict_open(&entry, &dict);
1164         append_properties(&dict, TRUE, service);
1165         connman_dbus_dict_close(&entry, &dict);
1166
1167         dbus_message_iter_close_container(iter, &entry);
1168 }
1169
1170 void __connman_service_list_struct(DBusMessageIter *iter)
1171 {
1172         g_sequence_foreach(service_list, append_struct, iter);
1173 }
1174
1175 int __connman_service_get_index(struct connman_service *service)
1176 {
1177         if (service == NULL)
1178                 return -1;
1179
1180         if (service->network == NULL)
1181                 return -1;
1182
1183         return connman_network_get_index(service->network);
1184 }
1185
1186 void __connman_service_set_domainname(struct connman_service *service,
1187                                                 const char *domainname)
1188 {
1189         if (service == NULL)
1190                 return;
1191
1192         g_free(service->domainname);
1193         service->domainname = g_strdup(domainname);
1194
1195         domain_changed(service);
1196 }
1197
1198 const char *__connman_service_get_domainname(struct connman_service *service)
1199 {
1200         if (service == NULL)
1201                 return NULL;
1202
1203         return service->domainname;
1204 }
1205
1206 const char *__connman_service_get_nameserver(struct connman_service *service)
1207 {
1208         if (service == NULL)
1209                 return NULL;
1210
1211         return service->nameserver;
1212 }
1213
1214 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1215                                                         const char *url)
1216 {
1217         if (service == NULL)
1218                 return;
1219
1220         if (__connman_ipconfig_set_proxy_autoconfig(service->ipconfig,
1221                                                                 url) < 0)
1222                 return;
1223
1224         proxy_changed(service);
1225 }
1226
1227 static DBusMessage *get_properties(DBusConnection *conn,
1228                                         DBusMessage *msg, void *user_data)
1229 {
1230         struct connman_service *service = user_data;
1231         DBusMessage *reply;
1232         DBusMessageIter array, dict;
1233         dbus_bool_t limited = TRUE;
1234
1235         DBG("service %p", service);
1236
1237         if (__connman_security_check_privilege(msg,
1238                                 CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
1239                 limited = FALSE;
1240
1241         reply = dbus_message_new_method_return(msg);
1242         if (reply == NULL)
1243                 return NULL;
1244
1245         dbus_message_iter_init_append(reply, &array);
1246
1247         connman_dbus_dict_open(&array, &dict);
1248         append_properties(&dict, limited, service);
1249         connman_dbus_dict_close(&array, &dict);
1250
1251         return reply;
1252 }
1253
1254 static DBusMessage *set_property(DBusConnection *conn,
1255                                         DBusMessage *msg, void *user_data)
1256 {
1257         struct connman_service *service = user_data;
1258         DBusMessageIter iter, value;
1259         const char *name;
1260         int type;
1261
1262         DBG("service %p", service);
1263
1264         if (dbus_message_iter_init(msg, &iter) == FALSE)
1265                 return __connman_error_invalid_arguments(msg);
1266
1267         dbus_message_iter_get_basic(&iter, &name);
1268         dbus_message_iter_next(&iter);
1269         dbus_message_iter_recurse(&iter, &value);
1270
1271         if (__connman_security_check_privilege(msg,
1272                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
1273                 return __connman_error_permission_denied(msg);
1274
1275         type = dbus_message_iter_get_arg_type(&value);
1276
1277         if (g_str_has_prefix(name, "AutoConnect") == TRUE) {
1278                 connman_bool_t autoconnect;
1279
1280                 if (type != DBUS_TYPE_BOOLEAN)
1281                         return __connman_error_invalid_arguments(msg);
1282
1283                 if (service->favorite == FALSE)
1284                         return __connman_error_invalid_service(msg);
1285
1286                 dbus_message_iter_get_basic(&value, &autoconnect);
1287
1288                 if (service->autoconnect == autoconnect)
1289                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1290
1291                 service->autoconnect = autoconnect;
1292
1293                 autoconnect_changed(service);
1294
1295                 __connman_storage_save_service(service);
1296         } else if (g_str_equal(name, "Passphrase") == TRUE) {
1297                 const char *passphrase;
1298
1299                 if (type != DBUS_TYPE_STRING)
1300                         return __connman_error_invalid_arguments(msg);
1301
1302                 if (service->immutable == TRUE)
1303                         return __connman_error_not_supported(msg);
1304
1305                 if (__connman_security_check_privilege(msg,
1306                                         CONNMAN_SECURITY_PRIVILEGE_SECRET) < 0)
1307                         return __connman_error_permission_denied(msg);
1308
1309                 dbus_message_iter_get_basic(&value, &passphrase);
1310
1311                 g_free(service->passphrase);
1312                 service->passphrase = g_strdup(passphrase);
1313
1314                 passphrase_changed(service);
1315
1316                 if (service->network != NULL)
1317                         connman_network_set_string(service->network,
1318                                 "WiFi.Passphrase", service->passphrase);
1319
1320                 __connman_storage_save_service(service);
1321         } else if (g_str_equal(name, "APN") == TRUE) {
1322                 const char *apn;
1323
1324                 if (type != DBUS_TYPE_STRING)
1325                         return __connman_error_invalid_arguments(msg);
1326
1327                 if (service->immutable == TRUE)
1328                         return __connman_error_not_supported(msg);
1329
1330                 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
1331                         return __connman_error_invalid_service(msg);
1332
1333                 dbus_message_iter_get_basic(&value, &apn);
1334
1335                 g_free(service->apn);
1336                 service->apn = g_strdup(apn);
1337
1338                 apn_changed(service);
1339
1340                 if (service->network != NULL)
1341                         connman_network_set_string(service->network,
1342                                                 "Cellular.APN", service->apn);
1343
1344                 __connman_storage_save_service(service);
1345         } else if (g_str_equal(name, "Username") == TRUE) {
1346                 const char *username;
1347
1348                 if (type != DBUS_TYPE_STRING)
1349                         return __connman_error_invalid_arguments(msg);
1350
1351                 if (service->immutable == TRUE)
1352                         return __connman_error_not_supported(msg);
1353
1354                 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
1355                         return __connman_error_invalid_service(msg);
1356
1357                 dbus_message_iter_get_basic(&value, &username);
1358
1359                 g_free(service->username);
1360                 service->username = g_strdup(username);
1361
1362                 if (service->network != NULL)
1363                         connman_network_set_string(service->network,
1364                                         "Cellular.Username", service->username);
1365
1366                 __connman_storage_save_service(service);
1367         } else if (g_str_equal(name, "Password") == TRUE) {
1368                 const char *password;
1369
1370                 if (type != DBUS_TYPE_STRING)
1371                         return __connman_error_invalid_arguments(msg);
1372
1373                 if (service->immutable == TRUE)
1374                         return __connman_error_not_supported(msg);
1375
1376                 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
1377                         return __connman_error_invalid_service(msg);
1378
1379                 dbus_message_iter_get_basic(&value, &password);
1380
1381                 g_free(service->password);
1382                 service->password = g_strdup(password);
1383
1384                 if (service->network != NULL)
1385                         connman_network_set_string(service->network,
1386                                         "Cellular.Password", service->password);
1387
1388                 __connman_storage_save_service(service);
1389         } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
1390                 DBusMessageIter entry;
1391                 GString *str;
1392                 int index;
1393                 const char *gw;
1394
1395                 if (type != DBUS_TYPE_ARRAY)
1396                         return __connman_error_invalid_arguments(msg);
1397
1398                 str = g_string_new(NULL);
1399                 if (str == NULL)
1400                         return __connman_error_invalid_arguments(msg);
1401
1402                 index = connman_network_get_index(service->network);
1403                 gw = __connman_ipconfig_get_gateway(index);
1404
1405                 if (gw && strlen(gw))
1406                         __connman_service_nameserver_del_routes(service);
1407
1408                 dbus_message_iter_recurse(&value, &entry);
1409
1410                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1411                         const char *val;
1412                         dbus_message_iter_get_basic(&entry, &val);
1413                         dbus_message_iter_next(&entry);
1414                         if (str->len > 0)
1415                                 g_string_append_printf(str, " %s", val);
1416                         else
1417                                 g_string_append(str, val);
1418                 }
1419
1420                 g_strfreev(service->nameservers);
1421
1422                 if (str->len > 0)
1423                         service->nameservers = g_strsplit_set(str->str, " ", 0);
1424                 else
1425                         service->nameservers = NULL;
1426
1427                 g_string_free(str, TRUE);
1428
1429                 if (gw && strlen(gw))
1430                         __connman_service_nameserver_add_routes(service, gw);
1431
1432                 update_nameservers(service);
1433                 dns_configuration_changed(service);
1434
1435                 __connman_storage_save_service(service);
1436         } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
1437                 DBusMessageIter entry;
1438                 GString *str;
1439
1440                 if (type != DBUS_TYPE_ARRAY)
1441                         return __connman_error_invalid_arguments(msg);
1442
1443                 str = g_string_new(NULL);
1444                 if (str == NULL)
1445                         return __connman_error_invalid_arguments(msg);
1446
1447                 dbus_message_iter_recurse(&value, &entry);
1448
1449                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1450                         const char *val;
1451                         dbus_message_iter_get_basic(&entry, &val);
1452                         dbus_message_iter_next(&entry);
1453                         if (str->len > 0)
1454                                 g_string_append_printf(str, " %s", val);
1455                         else
1456                                 g_string_append(str, val);
1457                 }
1458
1459                 g_strfreev(service->domains);
1460
1461                 if (str->len > 0)
1462                         service->domains = g_strsplit_set(str->str, " ", 0);
1463                 else
1464                         service->domains = NULL;
1465
1466                 g_string_free(str, TRUE);
1467
1468                 //update_domains(service);
1469                 domain_configuration_changed(service);
1470
1471                 __connman_storage_save_service(service);
1472         } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
1473                         g_str_equal(name, "IPv6.Configuration")) {
1474
1475                 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1476                 int err = 0;
1477                 struct connman_ipconfig *ipv6config;
1478
1479                 DBG("%s", name);
1480
1481                 ipv6config = connman_ipconfig_get_ipv6config(
1482                                                 service->ipconfig);
1483                 if (service->ipconfig == NULL)
1484                         return __connman_error_invalid_property(msg);
1485
1486                 if (is_connecting(service) ||
1487                                 is_connected(service)) {
1488                         __connman_network_clear_ipconfig(service->network,
1489                                                         service->ipconfig);
1490                         __connman_network_clear_ipconfig(service->network,
1491                                                                 ipv6config);
1492                 }
1493
1494                 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
1495                         type = CONNMAN_IPCONFIG_TYPE_IPV4;
1496                         err = __connman_ipconfig_set_config(
1497                                         service->ipconfig, type, &value);
1498                 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
1499                         type = CONNMAN_IPCONFIG_TYPE_IPV6;
1500                         err = __connman_ipconfig_set_config(
1501                                                 ipv6config, type, &value);
1502                 }
1503
1504                 if (err < 0) {
1505                         if (is_connected(service) ||
1506                                         is_connecting(service))
1507                                 __connman_network_set_ipconfig(
1508                                                         service->network,
1509                                                         service->ipconfig);
1510                         return __connman_error_failed(msg, -err);
1511                 }
1512
1513                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1514                         ipv4_configuration_changed(service);
1515                 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1516                         ipv6_configuration_changed(service);
1517
1518                 if (is_connecting(service) ||
1519                                 is_connected(service))
1520                         __connman_network_set_ipconfig(service->network,
1521                                                         service->ipconfig);
1522
1523                 __connman_storage_save_service(service);
1524         } else
1525                 return __connman_error_invalid_property(msg);
1526
1527         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1528 }
1529
1530 static void set_idle(struct connman_service *service)
1531 {
1532         service->state = CONNMAN_SERVICE_STATE_IDLE;
1533         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
1534         state_changed(service);
1535 }
1536
1537 static DBusMessage *clear_property(DBusConnection *conn,
1538                                         DBusMessage *msg, void *user_data)
1539 {
1540         struct connman_service *service = user_data;
1541         const char *name;
1542
1543         DBG("service %p", service);
1544
1545         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
1546                                                         DBUS_TYPE_INVALID);
1547
1548         if (__connman_security_check_privilege(msg,
1549                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
1550                 return __connman_error_permission_denied(msg);
1551
1552         if (g_str_equal(name, "Error") == TRUE) {
1553                 set_idle(service);
1554
1555                 g_get_current_time(&service->modified);
1556                 __connman_storage_save_service(service);
1557         } else if (g_str_equal(name, "Passphrase") == TRUE) {
1558                 if (service->immutable == TRUE)
1559                         return __connman_error_not_supported(msg);
1560
1561                 g_free(service->passphrase);
1562                 service->passphrase = NULL;
1563
1564                 passphrase_changed(service);
1565
1566                 __connman_storage_save_service(service);
1567         } else
1568                 return __connman_error_invalid_property(msg);
1569
1570         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1571 }
1572
1573 static connman_bool_t is_ignore(struct connman_service *service)
1574 {
1575         if (service->autoconnect == FALSE)
1576                 return TRUE;
1577
1578         if (service->roaming == TRUE)
1579                 return TRUE;
1580
1581         if (service->ignore == TRUE)
1582                 return TRUE;
1583
1584         if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
1585                 return TRUE;
1586
1587         return FALSE;
1588 }
1589
1590 void __connman_service_auto_connect(void)
1591 {
1592         struct connman_service *service = NULL;
1593         GSequenceIter *iter;
1594
1595         DBG("");
1596
1597         iter = g_sequence_get_begin_iter(service_list);
1598
1599         while (g_sequence_iter_is_end(iter) == FALSE) {
1600                 service = g_sequence_get(iter);
1601
1602                 if (service->pending != NULL)
1603                         return;
1604
1605                 if (is_connecting(service) == TRUE)
1606                         return;
1607
1608                 if (service->favorite == FALSE)
1609                         return;
1610
1611                 if (is_connected(service) == TRUE)
1612                         return;
1613
1614                 if (is_ignore(service) == FALSE &&
1615                                 service->state == CONNMAN_SERVICE_STATE_IDLE)
1616                         break;
1617
1618                 service = NULL;
1619
1620                 iter = g_sequence_iter_next(iter);
1621         }
1622
1623         if (service != NULL) {
1624                 service->userconnect = FALSE;
1625                 __connman_service_connect(service);
1626         }
1627 }
1628
1629 static void remove_timeout(struct connman_service *service)
1630 {
1631         if (service->timeout > 0) {
1632                 g_source_remove(service->timeout);
1633                 service->timeout = 0;
1634         }
1635 }
1636
1637 static void reply_pending(struct connman_service *service, int error)
1638 {
1639         remove_timeout(service);
1640
1641         if (service->pending != NULL) {
1642                 if (error > 0) {
1643                         DBusMessage *reply;
1644
1645                         reply = __connman_error_failed(service->pending,
1646                                                                 error);
1647                         if (reply != NULL)
1648                                 g_dbus_send_message(connection, reply);
1649                 } else
1650                         g_dbus_send_reply(connection, service->pending,
1651                                                         DBUS_TYPE_INVALID);
1652
1653                 dbus_message_unref(service->pending);
1654                 service->pending = NULL;
1655         }
1656 }
1657
1658 static gboolean connect_timeout(gpointer user_data)
1659 {
1660         struct connman_service *service = user_data;
1661         connman_bool_t autoconnect = FALSE;
1662
1663         DBG("service %p", service);
1664
1665         service->timeout = 0;
1666
1667         if (service->network != NULL)
1668                 __connman_network_disconnect(service->network);
1669
1670         __connman_ipconfig_disable(service->ipconfig);
1671
1672         if (service->pending != NULL) {
1673                 DBusMessage *reply;
1674
1675                 reply = __connman_error_operation_timeout(service->pending);
1676                 if (reply != NULL)
1677                         g_dbus_send_message(connection, reply);
1678
1679                 dbus_message_unref(service->pending);
1680                 service->pending = NULL;
1681         } else
1682                 autoconnect = TRUE;
1683
1684         __connman_service_indicate_state(service,
1685                                         CONNMAN_SERVICE_STATE_FAILURE);
1686
1687         if (autoconnect == TRUE && service->userconnect == FALSE)
1688                 __connman_service_auto_connect();
1689
1690         return FALSE;
1691 }
1692
1693 static void set_reconnect_state(struct connman_service *service,
1694                                                 connman_bool_t reconnect)
1695 {
1696         struct connman_device *device;
1697
1698         if (service->network == NULL)
1699                 return;
1700
1701         device = connman_network_get_device(service->network);
1702         if (device == NULL)
1703                 return;
1704
1705         __connman_device_set_reconnect(device, reconnect);
1706 }
1707
1708 static connman_bool_t get_reconnect_state(struct connman_service *service)
1709 {
1710         struct connman_device *device;
1711
1712         if (service->network == NULL)
1713                 return FALSE;
1714
1715         device = connman_network_get_device(service->network);
1716         if (device == NULL)
1717                 return FALSE;
1718
1719         return __connman_device_get_reconnect(device);
1720 }
1721
1722 struct connman_service *
1723 __connman_service_connect_type(enum connman_service_type type)
1724 {
1725         struct connman_service *service;
1726         GSequenceIter *iter;
1727         int err;
1728
1729         DBG("type %d", type);
1730
1731         /*
1732          * We go through the already sorted service list.
1733          * We pick the first one matching our type, or just
1734          * the first available one if we have no type.
1735          */
1736         iter = g_sequence_get_begin_iter(service_list);
1737         if (g_sequence_iter_is_end(iter))
1738                 return NULL;
1739         service = g_sequence_get(iter);
1740
1741         /*
1742          * If the first service is connected or about to be
1743          * connected, we return it, regardless of the type.
1744          */
1745         if ((g_sequence_iter_is_end(iter) == FALSE) &&
1746                 (is_connecting(service) == TRUE ||
1747                         is_connected(service) == TRUE))
1748                 return service;
1749
1750         while (g_sequence_iter_is_end(iter) == FALSE) {
1751                 if (service->type == type ||
1752                         type == CONNMAN_SERVICE_TYPE_UNKNOWN)
1753                         break;
1754
1755                 iter = g_sequence_iter_next(iter);
1756                 service = g_sequence_get(iter);
1757         }
1758
1759         if (g_sequence_iter_is_end(iter))
1760                 return NULL;
1761
1762         service->ignore = FALSE;
1763
1764         service->userconnect = TRUE;
1765
1766         set_reconnect_state(service, FALSE);
1767
1768         err = __connman_service_connect(service);
1769         if (err < 0) {
1770                 if (err == -ENOKEY)
1771                         if (__connman_agent_request_passphrase(service,
1772                                                                 NULL, NULL))
1773                                 return service;
1774
1775                 if (err != -EINPROGRESS)
1776                         return NULL;
1777         }
1778
1779         return service;
1780 }
1781
1782 static DBusMessage *connect_service(DBusConnection *conn,
1783                                         DBusMessage *msg, void *user_data)
1784 {
1785         struct connman_service *service = user_data;
1786         GSequenceIter *iter;
1787         int err;
1788
1789         DBG("service %p", service);
1790
1791         if (service->pending != NULL)
1792                 return __connman_error_in_progress(msg);
1793
1794         iter = g_sequence_get_begin_iter(service_list);
1795
1796         while (g_sequence_iter_is_end(iter) == FALSE) {
1797                 struct connman_service *temp = g_sequence_get(iter);
1798
1799                 if (service->type == temp->type &&
1800                                         is_connecting(temp) == TRUE)
1801                         return __connman_error_in_progress(msg);
1802
1803                 iter = g_sequence_iter_next(iter);
1804         }
1805
1806         service->ignore = FALSE;
1807
1808         service->userconnect = TRUE;
1809
1810         service->pending = dbus_message_ref(msg);
1811
1812         set_reconnect_state(service, FALSE);
1813
1814         err = __connman_service_connect(service);
1815         if (err < 0) {
1816                 if (err == -ENOKEY) {
1817                         if (__connman_agent_request_passphrase(service,
1818                                                         NULL, NULL) == 0)
1819                                 return NULL;
1820                 }
1821
1822                 if (err != -EINPROGRESS) {
1823                         dbus_message_unref(service->pending);
1824                         service->pending = NULL;
1825
1826                         return __connman_error_failed(msg, -err);
1827                 }
1828
1829                 return NULL;
1830         }
1831
1832         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1833 }
1834
1835 static DBusMessage *disconnect_service(DBusConnection *conn,
1836                                         DBusMessage *msg, void *user_data)
1837 {
1838         struct connman_service *service = user_data;
1839         int err;
1840
1841         DBG("service %p", service);
1842
1843         reply_pending(service, ECONNABORTED);
1844
1845         service->ignore = TRUE;
1846
1847         set_reconnect_state(service, FALSE);
1848
1849         err = __connman_service_disconnect(service);
1850         if (err < 0) {
1851                 if (err != -EINPROGRESS)
1852                         return __connman_error_failed(msg, -err);
1853
1854                 return NULL;
1855         }
1856
1857         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1858 }
1859
1860 static DBusMessage *remove_service(DBusConnection *conn,
1861                                         DBusMessage *msg, void *user_data)
1862 {
1863         struct connman_service *service = user_data;
1864
1865         DBG("service %p", service);
1866
1867         if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
1868                 return __connman_error_not_supported(msg);
1869
1870         if (service->immutable == TRUE)
1871                 return __connman_error_not_supported(msg);
1872
1873         if (service->favorite == FALSE &&
1874                         service->state != CONNMAN_SERVICE_STATE_FAILURE)
1875                 return __connman_error_not_supported(msg);
1876
1877         if (service->network != NULL) {
1878                 set_reconnect_state(service, FALSE);
1879
1880                 __connman_network_disconnect(service->network);
1881         }
1882
1883         g_free(service->passphrase);
1884         service->passphrase = NULL;
1885
1886         passphrase_changed(service);
1887
1888         g_free(service->apn);
1889         service->apn = NULL;
1890
1891         g_free(service->username);
1892         service->username = NULL;
1893
1894         g_free(service->password);
1895         service->password = NULL;
1896
1897         apn_changed(service);
1898
1899         set_idle(service);
1900
1901         __connman_service_set_favorite(service, FALSE);
1902         __connman_storage_save_service(service);
1903
1904         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1905 }
1906
1907 static DBusMessage *move_service(DBusConnection *conn,
1908                                         DBusMessage *msg, void *user_data,
1909                                                                 gboolean before)
1910 {
1911         struct connman_service *service = user_data;
1912         struct connman_service *target;
1913         const char *path;
1914         GSequenceIter *src, *dst;
1915
1916         DBG("service %p", service);
1917
1918         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
1919                                                         DBUS_TYPE_INVALID);
1920
1921         if (service->favorite == FALSE)
1922                 return __connman_error_not_supported(msg);
1923
1924         target = find_service(path);
1925         if (target == NULL || target->favorite == FALSE || target == service)
1926                 return __connman_error_invalid_service(msg);
1927
1928         DBG("target %s", target->identifier);
1929
1930         if (target->state != service->state)
1931                 return __connman_error_invalid_service(msg);
1932
1933         g_get_current_time(&service->modified);
1934         __connman_storage_save_service(service);
1935
1936         src = g_hash_table_lookup(service_hash, service->identifier);
1937         dst = g_hash_table_lookup(service_hash, target->identifier);
1938
1939         before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
1940
1941         __connman_profile_changed(FALSE);
1942
1943         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1944 }
1945
1946 static DBusMessage *move_before(DBusConnection *conn,
1947                                         DBusMessage *msg, void *user_data)
1948 {
1949         return move_service(conn, msg, user_data, TRUE);
1950 }
1951
1952 static DBusMessage *move_after(DBusConnection *conn,
1953                                         DBusMessage *msg, void *user_data)
1954 {
1955         return move_service(conn, msg, user_data, FALSE);
1956 }
1957
1958 static DBusMessage *reset_counters(DBusConnection *conn,
1959                                         DBusMessage *msg, void *user_data)
1960 {
1961         struct connman_service *service = user_data;
1962
1963         reset_stats(service);
1964
1965         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1966 }
1967
1968 static GDBusMethodTable service_methods[] = {
1969         { "GetProperties", "",   "a{sv}", get_properties     },
1970         { "SetProperty",   "sv", "",      set_property       },
1971         { "ClearProperty", "s",  "",      clear_property     },
1972         { "Connect",       "",   "",      connect_service,
1973                                                 G_DBUS_METHOD_FLAG_ASYNC },
1974         { "Disconnect",    "",   "",      disconnect_service },
1975         { "Remove",        "",   "",      remove_service     },
1976         { "MoveBefore",    "o",  "",      move_before        },
1977         { "MoveAfter",     "o",  "",      move_after         },
1978         { "ResetCounters", "",   "",      reset_counters     },
1979         { },
1980 };
1981
1982 static GDBusSignalTable service_signals[] = {
1983         { "PropertyChanged", "sv" },
1984         { },
1985 };
1986
1987 static void service_free(gpointer user_data)
1988 {
1989         struct connman_service *service = user_data;
1990         char *path = service->path;
1991
1992         DBG("service %p", service);
1993
1994         reply_pending(service, ENOENT);
1995
1996         g_hash_table_remove(service_hash, service->identifier);
1997
1998         stats_stop(service);
1999         __connman_storage_save_service(service);
2000
2001         service->path = NULL;
2002
2003         if (path != NULL) {
2004                 __connman_profile_changed(FALSE);
2005
2006                 g_dbus_unregister_interface(connection, path,
2007                                                 CONNMAN_SERVICE_INTERFACE);
2008                 g_free(path);
2009         }
2010
2011         if (service->network != NULL)
2012                 connman_network_unref(service->network);
2013
2014         if (service->provider != NULL)
2015                 connman_provider_unref(service->provider);
2016
2017         if (service->ipconfig != NULL) {
2018                 connman_ipconfig_unref(service->ipconfig);
2019                 service->ipconfig = NULL;
2020         }
2021
2022         if (service->location != NULL)
2023                 connman_location_unref(service->location);
2024
2025         g_strfreev(service->nameservers);
2026         g_strfreev(service->domains);
2027
2028         g_free(service->nameserver);
2029         g_free(service->domainname);
2030         g_free(service->mcc);
2031         g_free(service->mnc);
2032         g_free(service->apn);
2033         g_free(service->username);
2034         g_free(service->password);
2035         g_free(service->profile);
2036         g_free(service->name);
2037         g_free(service->passphrase);
2038         g_free(service->identifier);
2039         g_free(service->eap);
2040         g_free(service->identity);
2041         g_free(service->ca_cert_file);
2042         g_free(service->client_cert_file);
2043         g_free(service->private_key_file);
2044         g_free(service->private_key_passphrase);
2045         g_free(service->phase2);
2046
2047         if (service->stats.timer != NULL)
2048                 g_timer_destroy(service->stats.timer);
2049
2050         g_free(service);
2051 }
2052
2053 /**
2054  * __connman_service_put:
2055  * @service: service structure
2056  *
2057  * Release service if no longer needed
2058  */
2059 void __connman_service_put(struct connman_service *service)
2060 {
2061         DBG("service %p", service);
2062
2063         if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
2064                 GSequenceIter *iter;
2065
2066                 iter = g_hash_table_lookup(service_hash, service->identifier);
2067                 if (iter != NULL) {
2068                         reply_pending(service, ECONNABORTED);
2069
2070                         __connman_service_disconnect(service);
2071
2072                         g_sequence_remove(iter);
2073                 } else
2074                         service_free(service);
2075         }
2076 }
2077
2078 static void service_initialize(struct connman_service *service)
2079 {
2080         DBG("service %p", service);
2081
2082         service->refcount = 1;
2083
2084         service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
2085         service->mode     = CONNMAN_SERVICE_MODE_UNKNOWN;
2086         service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
2087         service->state    = CONNMAN_SERVICE_STATE_UNKNOWN;
2088
2089         service->favorite  = FALSE;
2090         service->immutable = FALSE;
2091         service->hidden = FALSE;
2092
2093         service->ignore = FALSE;
2094
2095         service->userconnect = FALSE;
2096
2097         service->order = 0;
2098
2099         service->stats.valid = FALSE;
2100         service->stats.enabled = FALSE;
2101         service->stats.timer = g_timer_new();
2102
2103         service->provider = NULL;
2104 }
2105
2106 /**
2107  * connman_service_create:
2108  *
2109  * Allocate a new service.
2110  *
2111  * Returns: a newly-allocated #connman_service structure
2112  */
2113 struct connman_service *connman_service_create(void)
2114 {
2115         struct connman_service *service;
2116
2117         service = g_try_new0(struct connman_service, 1);
2118         if (service == NULL)
2119                 return NULL;
2120
2121         DBG("service %p", service);
2122
2123         service_initialize(service);
2124
2125         service->location = __connman_location_create(service);
2126
2127         return service;
2128 }
2129
2130 struct connman_location *__connman_service_get_location(struct connman_service *service)
2131 {
2132         return service->location;
2133 }
2134
2135 /**
2136  * connman_service_ref:
2137  * @service: service structure
2138  *
2139  * Increase reference counter of service
2140  */
2141 struct connman_service *connman_service_ref(struct connman_service *service)
2142 {
2143         g_atomic_int_inc(&service->refcount);
2144
2145         return service;
2146 }
2147
2148 /**
2149  * connman_service_unref:
2150  * @service: service structure
2151  *
2152  * Decrease reference counter of service
2153  */
2154 void connman_service_unref(struct connman_service *service)
2155 {
2156         __connman_service_put(service);
2157 }
2158
2159 static gint service_compare(gconstpointer a, gconstpointer b,
2160                                                         gpointer user_data)
2161 {
2162         struct connman_service *service_a = (void *) a;
2163         struct connman_service *service_b = (void *) b;
2164
2165         if (service_a->state != service_b->state) {
2166                 if (is_connected(service_a) == TRUE)
2167                         return -1;
2168                 if (is_connected(service_b) == TRUE)
2169                         return 1;
2170         }
2171
2172         if (service_a->order > service_b->order)
2173                 return -1;
2174
2175         if (service_a->order < service_b->order)
2176                 return 1;
2177
2178         if (service_a->favorite == TRUE && service_b->favorite == FALSE)
2179                 return -1;
2180
2181         if (service_a->favorite == FALSE && service_b->favorite == TRUE)
2182                 return 1;
2183
2184         if (service_a->type != service_b->type) {
2185                 switch (service_a->type) {
2186                 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2187                 case CONNMAN_SERVICE_TYPE_SYSTEM:
2188                 case CONNMAN_SERVICE_TYPE_ETHERNET:
2189                 case CONNMAN_SERVICE_TYPE_GPS:
2190                 case CONNMAN_SERVICE_TYPE_VPN:
2191                         break;
2192                 case CONNMAN_SERVICE_TYPE_WIFI:
2193                         return 1;
2194                 case CONNMAN_SERVICE_TYPE_WIMAX:
2195                 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2196                 case CONNMAN_SERVICE_TYPE_CELLULAR:
2197                         return -1;
2198                 }
2199         }
2200
2201         return (gint) service_b->strength - (gint) service_a->strength;
2202 }
2203
2204 /**
2205  * connman_service_get_type:
2206  * @service: service structure
2207  *
2208  * Get the type of service
2209  */
2210 enum connman_service_type connman_service_get_type(struct connman_service *service)
2211 {
2212         if (service == NULL)
2213                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
2214
2215         return service->type;
2216 }
2217
2218 /**
2219  * connman_service_get_interface:
2220  * @service: service structure
2221  *
2222  * Get network interface of service
2223  */
2224 char *connman_service_get_interface(struct connman_service *service)
2225 {
2226         int index;
2227
2228         if (service == NULL)
2229                 return NULL;
2230
2231         if (service->network == NULL)
2232                 return NULL;
2233
2234         index = connman_network_get_index(service->network);
2235
2236         return connman_inet_ifname(index);
2237 }
2238
2239 /**
2240  * connman_service_get_network:
2241  * @service: service structure
2242  *
2243  * Get the service network
2244  */
2245 struct connman_network *
2246 __connman_service_get_network(struct connman_service *service)
2247 {
2248         if (service == NULL)
2249                 return NULL;
2250
2251         return service->network;
2252 }
2253
2254 struct connman_ipconfig *__connman_service_get_ipconfig(struct connman_service *service)
2255 {
2256         if (service == NULL)
2257                 return NULL;
2258
2259         return service->ipconfig;
2260 }
2261
2262 /**
2263  * __connman_service_set_favorite:
2264  * @service: service structure
2265  * @favorite: favorite value
2266  *
2267  * Change the favorite setting of service
2268  */
2269 int __connman_service_set_favorite(struct connman_service *service,
2270                                                 connman_bool_t favorite)
2271 {
2272         GSequenceIter *iter;
2273
2274         iter = g_hash_table_lookup(service_hash, service->identifier);
2275         if (iter == NULL)
2276                 return -ENOENT;
2277
2278         if (service->favorite == favorite)
2279                 return -EALREADY;
2280
2281         service->favorite = favorite;
2282
2283         favorite_changed(service);
2284
2285         g_sequence_sort_changed(iter, service_compare, NULL);
2286
2287         __connman_profile_changed(FALSE);
2288
2289         return 0;
2290 }
2291
2292 int __connman_service_set_immutable(struct connman_service *service,
2293                                                 connman_bool_t immutable)
2294 {
2295         service->immutable = immutable;
2296
2297         immutable_changed(service);
2298
2299         return 0;
2300 }
2301
2302 void __connman_service_set_string(struct connman_service *service,
2303                                   const char *key, const char *value)
2304 {
2305         if (g_str_equal(key, "EAP") == TRUE) {
2306                 g_free(service->eap);
2307                 service->eap = g_strdup(value);
2308         } else if (g_str_equal(key, "Identity") == TRUE) {
2309                 g_free(service->identity);
2310                 service->identity = g_strdup(value);
2311         } else if (g_str_equal(key, "CACertFile") == TRUE) {
2312                 g_free(service->ca_cert_file);
2313                 service->ca_cert_file = g_strdup(value);
2314         } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
2315                 g_free(service->client_cert_file);
2316                 service->client_cert_file = g_strdup(value);
2317         } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
2318                 g_free(service->private_key_file);
2319                 service->private_key_file = g_strdup(value);
2320         } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
2321                 g_free(service->private_key_passphrase);
2322                 service->private_key_passphrase = g_strdup(value);
2323         } else if (g_str_equal(key, "Phase2") == TRUE) {
2324                 g_free(service->phase2);
2325                 service->phase2 = g_strdup(value);
2326         } else if (g_str_equal(key, "Passphrase") == TRUE) {
2327                 g_free(service->passphrase);
2328                 service->passphrase = g_strdup(value);
2329         }
2330 }
2331
2332 int __connman_service_indicate_state(struct connman_service *service,
2333                                         enum connman_service_state state)
2334 {
2335         GSequenceIter *iter;
2336
2337         DBG("service %p state %d", service, state);
2338
2339         if (service == NULL)
2340                 return -EINVAL;
2341
2342         if (service->state == state)
2343                 return -EALREADY;
2344
2345         if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
2346                                 state == CONNMAN_SERVICE_STATE_IDLE)
2347                 return -EINVAL;
2348
2349         if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
2350                                 state == CONNMAN_SERVICE_STATE_DISCONNECT)
2351                 return -EINVAL;
2352
2353         if (state == CONNMAN_SERVICE_STATE_IDLE &&
2354                         service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
2355                 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
2356                 state_changed(service);
2357
2358                 reply_pending(service, ECONNABORTED);
2359
2360                 __connman_service_disconnect(service);
2361         }
2362
2363         if (state == CONNMAN_SERVICE_STATE_CONFIGURATION)
2364                 __connman_ipconfig_enable(service->ipconfig);
2365
2366         service->state = state;
2367         state_changed(service);
2368
2369         if (state == CONNMAN_SERVICE_STATE_ONLINE) {
2370                 if (service->login_required == TRUE) {
2371                         service->login_required = FALSE;
2372                         login_changed(service);
2373                 }
2374
2375                 connman_timeserver_sync();
2376         }
2377
2378         if (state == CONNMAN_SERVICE_STATE_IDLE) {
2379                 connman_bool_t reconnect;
2380
2381                 reconnect = get_reconnect_state(service);
2382                 if (reconnect == TRUE)
2383                         __connman_service_auto_connect();
2384         }
2385
2386         if (state == CONNMAN_SERVICE_STATE_READY) {
2387                 set_reconnect_state(service, TRUE);
2388
2389                 __connman_service_set_favorite(service, TRUE);
2390
2391                 reply_pending(service, 0);
2392
2393                 service->userconnect = FALSE;
2394
2395                 g_get_current_time(&service->modified);
2396                 __connman_storage_save_service(service);
2397
2398                 update_nameservers(service);
2399                 dns_changed(service);
2400
2401                 __connman_wpad_start(service);
2402
2403                 __connman_notifier_connect(service->type);
2404
2405                 default_changed();
2406         } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
2407                 __connman_location_finish(service);
2408
2409                 default_changed();
2410
2411                 __connman_wpad_stop(service);
2412
2413                 update_nameservers(service);
2414                 dns_changed(service);
2415
2416                 __connman_notifier_disconnect(service->type);
2417         }
2418
2419         if (state == CONNMAN_SERVICE_STATE_FAILURE) {
2420                 reply_pending(service, EIO);
2421
2422                 if (service->userconnect == FALSE)
2423                         __connman_service_auto_connect();
2424
2425                 g_get_current_time(&service->modified);
2426                 __connman_storage_save_service(service);
2427         } else
2428                 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2429
2430         iter = g_hash_table_lookup(service_hash, service->identifier);
2431         if (iter != NULL)
2432                 g_sequence_sort_changed(iter, service_compare, NULL);
2433
2434         __connman_profile_changed(FALSE);
2435
2436         if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
2437                         service->state == CONNMAN_SERVICE_STATE_FAILURE)
2438                 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
2439
2440         return 0;
2441 }
2442
2443 int __connman_service_indicate_error(struct connman_service *service,
2444                                         enum connman_service_error error)
2445 {
2446         DBG("service %p error %d", service, error);
2447
2448         if (service == NULL)
2449                 return -EINVAL;
2450
2451         service->error = error;
2452
2453         return __connman_service_indicate_state(service,
2454                                         CONNMAN_SERVICE_STATE_FAILURE);
2455 }
2456
2457 int __connman_service_indicate_default(struct connman_service *service)
2458 {
2459         DBG("service %p", service);
2460
2461         default_changed();
2462
2463         __connman_location_detect(service);
2464
2465         return 0;
2466 }
2467
2468 int __connman_service_request_login(struct connman_service *service)
2469 {
2470         DBG("service %p", service);
2471
2472         if (service == NULL)
2473                 return -EINVAL;
2474
2475         service->login_required = TRUE;
2476         login_changed(service);
2477
2478         return 0;
2479 }
2480
2481 static connman_bool_t prepare_network(struct connman_service *service)
2482 {
2483         enum connman_network_type type;
2484         unsigned int ssid_len;
2485
2486         type = connman_network_get_type(service->network);
2487
2488         switch (type) {
2489         case CONNMAN_NETWORK_TYPE_UNKNOWN:
2490         case CONNMAN_NETWORK_TYPE_VENDOR:
2491                 return FALSE;
2492         case CONNMAN_NETWORK_TYPE_WIFI:
2493                 if (connman_network_get_blob(service->network, "WiFi.SSID",
2494                                                         &ssid_len) == NULL)
2495                         return FALSE;
2496
2497                 connman_network_set_string(service->network,
2498                                 "WiFi.Passphrase", service->passphrase);
2499                 break;
2500         case CONNMAN_NETWORK_TYPE_ETHERNET:
2501         case CONNMAN_NETWORK_TYPE_WIMAX:
2502         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
2503         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
2504                 break;
2505         case CONNMAN_NETWORK_TYPE_CELLULAR:
2506                 connman_network_set_string(service->network,
2507                                                 "Cellular.APN", service->apn);
2508
2509                 connman_network_set_string(service->network,
2510                                         "Cellular.Username", service->username);
2511                 connman_network_set_string(service->network,
2512                                         "Cellular.Password", service->password);
2513                 break;
2514         }
2515
2516         return TRUE;
2517 }
2518
2519 static void prepare_8021x(struct connman_service *service)
2520 {
2521         if (service->eap != NULL)
2522                 connman_network_set_string(service->network, "WiFi.EAP",
2523                                                                 service->eap);
2524
2525         if (service->identity != NULL)
2526                 connman_network_set_string(service->network, "WiFi.Identity",
2527                                                         service->identity);
2528
2529         if (service->ca_cert_file != NULL)
2530                 connman_network_set_string(service->network, "WiFi.CACertFile",
2531                                                         service->ca_cert_file);
2532
2533         if (service->client_cert_file != NULL)
2534                 connman_network_set_string(service->network,
2535                                                 "WiFi.ClientCertFile",
2536                                                 service->client_cert_file);
2537
2538         if (service->private_key_file != NULL)
2539                 connman_network_set_string(service->network,
2540                                                 "WiFi.PrivateKeyFile",
2541                                                 service->private_key_file);
2542
2543         if (service->private_key_passphrase != NULL)
2544                 connman_network_set_string(service->network,
2545                                         "WiFi.PrivateKeyPassphrase",
2546                                         service->private_key_passphrase);
2547
2548         if (service->phase2 != NULL)
2549                 connman_network_set_string(service->network, "WiFi.Phase2",
2550                                                         service->phase2);
2551 }
2552
2553 int __connman_service_connect(struct connman_service *service)
2554 {
2555         int err;
2556
2557         DBG("service %p", service);
2558
2559         if (is_connected(service) == TRUE)
2560                 return -EISCONN;
2561
2562         if (is_connecting(service) == TRUE)
2563                 return -EALREADY;
2564
2565         switch (service->type) {
2566         case CONNMAN_SERVICE_TYPE_UNKNOWN:
2567         case CONNMAN_SERVICE_TYPE_SYSTEM:
2568         case CONNMAN_SERVICE_TYPE_GPS:
2569                 return -EINVAL;
2570         case CONNMAN_SERVICE_TYPE_ETHERNET:
2571         case CONNMAN_SERVICE_TYPE_WIMAX:
2572         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2573         case CONNMAN_SERVICE_TYPE_VPN:
2574                 break;
2575         case CONNMAN_SERVICE_TYPE_CELLULAR:
2576                 if (service->apn == NULL)
2577                         return -EINVAL;
2578                 break;
2579         case CONNMAN_SERVICE_TYPE_WIFI:
2580                 switch (service->security) {
2581                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2582                 case CONNMAN_SERVICE_SECURITY_NONE:
2583                         break;
2584                 case CONNMAN_SERVICE_SECURITY_WEP:
2585                 case CONNMAN_SERVICE_SECURITY_PSK:
2586                 case CONNMAN_SERVICE_SECURITY_WPA:
2587                 case CONNMAN_SERVICE_SECURITY_RSN:
2588                         if (service->passphrase == NULL)
2589                                 return -ENOKEY;
2590                         break;
2591                 case CONNMAN_SERVICE_SECURITY_8021X:
2592                         break;
2593                 }
2594                 break;
2595         }
2596
2597         if (service->network != NULL) {
2598                 if (prepare_network(service) == FALSE)
2599                         return -EINVAL;
2600
2601                 switch (service->security) {
2602                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2603                 case CONNMAN_SERVICE_SECURITY_NONE:
2604                 case CONNMAN_SERVICE_SECURITY_WEP:
2605                 case CONNMAN_SERVICE_SECURITY_PSK:
2606                 case CONNMAN_SERVICE_SECURITY_WPA:
2607                 case CONNMAN_SERVICE_SECURITY_RSN:
2608                         break;
2609                 case CONNMAN_SERVICE_SECURITY_8021X:
2610                         prepare_8021x(service);
2611                         break;
2612                 }
2613
2614                 __connman_ipconfig_enable(service->ipconfig);
2615
2616                 err = __connman_network_connect(service->network);
2617         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
2618                                         service->provider != NULL)
2619                 err = __connman_provider_connect(service->provider);
2620         else
2621                 return -EOPNOTSUPP;
2622
2623         if (err < 0) {
2624                 if (err != -EINPROGRESS) {
2625                         __connman_ipconfig_disable(service->ipconfig);
2626                         return err;
2627                 }
2628
2629                 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
2630                                                 connect_timeout, service);
2631
2632                 return -EINPROGRESS;
2633         }
2634
2635         return 0;
2636 }
2637
2638 int __connman_service_disconnect(struct connman_service *service)
2639 {
2640         struct connman_ipconfig *ipv6config;
2641         int err;
2642
2643         DBG("service %p", service);
2644
2645         if (service->network != NULL) {
2646                 err = __connman_network_disconnect(service->network);
2647         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
2648                                         service->provider != NULL)
2649                 err = __connman_provider_disconnect(service->provider);
2650         else
2651                 return -EOPNOTSUPP;
2652
2653         __connman_ipconfig_clear_address(service->ipconfig);
2654
2655         ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
2656
2657         __connman_ipconfig_clear_address(ipv6config);
2658
2659         __connman_ipconfig_disable(service->ipconfig);
2660
2661         if (err < 0) {
2662                 if (err != -EINPROGRESS)
2663                         return err;
2664
2665                 return -EINPROGRESS;
2666         }
2667
2668         return 0;
2669 }
2670
2671 /**
2672  * __connman_service_lookup:
2673  * @pattern: search pattern
2674  * @path: return object path
2675  *
2676  * Look up a service path from a search pattern
2677  */
2678 int __connman_service_lookup(const char *pattern, const char **path)
2679 {
2680         GHashTableIter iter;
2681         gpointer key, value;
2682
2683         g_hash_table_iter_init(&iter, service_hash);
2684
2685         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
2686                 GSequenceIter *iter = value;
2687                 struct connman_service *service = g_sequence_get(iter);
2688
2689                 if (g_strcmp0(service->identifier, pattern) == 0 ||
2690                                 g_strcmp0(service->name, pattern) == 0) {
2691                         *path = (const char *) service->path;
2692                         return 0;
2693                 }
2694         }
2695
2696         return -ENXIO;
2697 }
2698
2699 /**
2700  * lookup_by_identifier:
2701  * @identifier: service identifier
2702  *
2703  * Look up a service by identifier (reference count will not be increased)
2704  */
2705 static struct connman_service *lookup_by_identifier(const char *identifier)
2706 {
2707         GSequenceIter *iter;
2708
2709         iter = g_hash_table_lookup(service_hash, identifier);
2710         if (iter != NULL)
2711                 return g_sequence_get(iter);
2712
2713         return NULL;
2714 }
2715
2716 static struct connman_network *create_hidden_wifi(struct connman_device *device,
2717                 const char *ssid, const char *mode, const char *security)
2718 {
2719         struct connman_network *network;
2720         char *name;
2721         int index;
2722         unsigned int i, ssid_len;
2723
2724         ssid_len = strlen(ssid);
2725         if (ssid_len < 1)
2726                 return NULL;
2727
2728         network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
2729         if (network == NULL)
2730                 return NULL;
2731
2732         connman_network_set_blob(network, "WiFi.SSID",
2733                                         (unsigned char *) ssid, ssid_len);
2734
2735         connman_network_set_string(network, "WiFi.Mode", mode);
2736         connman_network_set_string(network, "WiFi.Security", security);
2737
2738         name = g_try_malloc0(ssid_len + 1);
2739         if (name == NULL) {
2740                 connman_network_unref(network);
2741                 return NULL;
2742         }
2743
2744         for (i = 0; i < ssid_len; i++) {
2745                 if (g_ascii_isprint(ssid[i]))
2746                         name[i] = ssid[i];
2747                 else
2748                         name[i] = ' ';
2749         }
2750
2751         connman_network_set_name(network, name);
2752
2753         g_free(name);
2754
2755         index = connman_device_get_index(device);
2756         connman_network_set_index(network, index);
2757
2758         connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
2759
2760         if (connman_device_add_network(device, network) < 0) {
2761                 connman_network_unref(network);
2762                 return NULL;
2763         }
2764
2765         connman_network_set_available(network, TRUE);
2766
2767         return network;
2768 }
2769
2770 int __connman_service_create_and_connect(DBusMessage *msg)
2771 {
2772         struct connman_service *service;
2773         struct connman_network *network;
2774         struct connman_device *device;
2775         DBusMessageIter iter, array;
2776         const char *mode = "managed", *security = "none", *group_security;
2777         const char *type = NULL, *ssid = NULL, *passphrase = NULL;
2778         unsigned int ssid_len = 0;
2779         const char *ident;
2780         char *name, *group;
2781         gboolean created = FALSE;
2782         int err;
2783
2784         dbus_message_iter_init(msg, &iter);
2785         dbus_message_iter_recurse(&iter, &array);
2786
2787         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
2788                 DBusMessageIter entry, value;
2789                 const char *key;
2790
2791                 dbus_message_iter_recurse(&array, &entry);
2792                 dbus_message_iter_get_basic(&entry, &key);
2793
2794                 dbus_message_iter_next(&entry);
2795                 dbus_message_iter_recurse(&entry, &value);
2796
2797                 switch (dbus_message_iter_get_arg_type(&value)) {
2798                 case DBUS_TYPE_STRING:
2799                         if (g_str_equal(key, "Type") == TRUE)
2800                                 dbus_message_iter_get_basic(&value, &type);
2801                         else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
2802                                         g_str_equal(key, "Mode") == TRUE)
2803                                 dbus_message_iter_get_basic(&value, &mode);
2804                         else if (g_str_equal(key, "WiFi.Security") == TRUE ||
2805                                         g_str_equal(key, "Security") == TRUE)
2806                                 dbus_message_iter_get_basic(&value, &security);
2807                         else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
2808                                         g_str_equal(key, "Passphrase") == TRUE)
2809                                 dbus_message_iter_get_basic(&value, &passphrase);
2810                         else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
2811                                         g_str_equal(key, "SSID") == TRUE)
2812                                 dbus_message_iter_get_basic(&value, &ssid);
2813                 }
2814
2815                 dbus_message_iter_next(&array);
2816         }
2817
2818         if (type == NULL)
2819                 return -EINVAL;
2820
2821         if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
2822                 return -EOPNOTSUPP;
2823
2824         if (ssid == NULL)
2825                 return -EINVAL;
2826
2827         ssid_len = strlen(ssid);
2828         if (ssid_len < 1)
2829                 return -EINVAL;
2830
2831         if (g_strcmp0(security, "none") != 0 &&
2832                                 g_strcmp0(security, "wep") != 0 &&
2833                                 g_strcmp0(security, "psk") != 0 &&
2834                                 g_strcmp0(security, "wpa") != 0 &&
2835                                 g_strcmp0(security, "rsn") != 0 &&
2836                                 g_strcmp0(security, "ieee8021x") != 0)
2837                 return -EINVAL;
2838
2839         device = __connman_element_find_device(CONNMAN_DEVICE_TYPE_WIFI);
2840         if (device == NULL)
2841                 return -EOPNOTSUPP;
2842
2843         ident = __connman_device_get_ident(device);
2844         if (ident == NULL)
2845                 return -EOPNOTSUPP;
2846
2847
2848         if (!g_strcmp0(security, "wpa") ||
2849                 !g_strcmp0(security, "rsn"))
2850                 group_security = "psk";
2851         else
2852                 group_security = security;
2853
2854         group = connman_wifi_build_group_name((unsigned char *) ssid,
2855                                                 ssid_len, mode, group_security);
2856         if (group == NULL)
2857                 return -EINVAL;
2858
2859         name = g_strdup_printf("%s_%s_%s", type, ident, group);
2860
2861         service = lookup_by_identifier(name);
2862
2863         if (service != NULL)
2864                 goto done;
2865
2866         network = create_hidden_wifi(device, ssid, mode, security);
2867         if (network != NULL) {
2868                 connman_network_set_group(network, group);
2869                 created = TRUE;
2870         }
2871
2872         service = lookup_by_identifier(name);
2873
2874 done:
2875         g_free(name);
2876         g_free(group);
2877
2878         if (service == NULL) {
2879                 err = -EOPNOTSUPP;
2880                 goto failed;
2881         }
2882
2883         set_reconnect_state(service, FALSE);
2884
2885         __connman_device_disconnect(device);
2886
2887         if (passphrase != NULL) {
2888                 g_free(service->passphrase);
2889                 service->passphrase = g_strdup(passphrase);
2890         }
2891
2892         service->userconnect = TRUE;
2893
2894         err = __connman_service_connect(service);
2895         if (err < 0 && err != -EINPROGRESS)
2896                 goto failed;
2897
2898         g_dbus_send_reply(connection, msg,
2899                                 DBUS_TYPE_OBJECT_PATH, &service->path,
2900                                                         DBUS_TYPE_INVALID);
2901
2902         return 0;
2903
2904 failed:
2905         if (service != NULL && created == TRUE) {
2906                 struct connman_network *network = service->network;
2907
2908                 if (network != NULL) {
2909                         connman_network_set_available(network, FALSE);
2910                         __connman_device_cleanup_networks(device);
2911                 } else
2912                         __connman_service_put(service);
2913         }
2914
2915         return err;
2916 }
2917
2918 /**
2919  * __connman_service_get:
2920  * @identifier: service identifier
2921  *
2922  * Look up a service by identifier or create a new one if not found
2923  */
2924 static struct connman_service *service_get(const char *identifier)
2925 {
2926         struct connman_service *service;
2927         GSequenceIter *iter;
2928
2929         iter = g_hash_table_lookup(service_hash, identifier);
2930         if (iter != NULL) {
2931                 service = g_sequence_get(iter);
2932                 if (service != NULL)
2933                         g_atomic_int_inc(&service->refcount);
2934                 return service;
2935         }
2936
2937         service = connman_service_create();
2938         if (service == NULL)
2939                 return NULL;
2940
2941         DBG("service %p", service);
2942
2943         service->identifier = g_strdup(identifier);
2944
2945         service->profile = g_strdup(__connman_profile_active_ident());
2946
2947         iter = g_sequence_insert_sorted(service_list, service,
2948                                                 service_compare, NULL);
2949
2950         g_hash_table_insert(service_hash, service->identifier, iter);
2951
2952         return service;
2953 }
2954
2955 static int service_register(struct connman_service *service)
2956 {
2957         const char *path = __connman_profile_active_path();
2958         GSequenceIter *iter;
2959
2960         DBG("service %p", service);
2961
2962         if (service->path != NULL)
2963                 return -EALREADY;
2964
2965         service->path = g_strdup_printf("%s/%s", path, service->identifier);
2966
2967         DBG("path %s", service->path);
2968
2969         __connman_config_provision_service(service);
2970
2971         __connman_storage_load_service(service);
2972
2973         g_dbus_register_interface(connection, service->path,
2974                                         CONNMAN_SERVICE_INTERFACE,
2975                                         service_methods, service_signals,
2976                                                         NULL, service, NULL);
2977
2978         iter = g_hash_table_lookup(service_hash, service->identifier);
2979         if (iter != NULL)
2980                 g_sequence_sort_changed(iter, service_compare, NULL);
2981
2982         __connman_profile_changed(TRUE);
2983
2984         return 0;
2985 }
2986
2987 static void service_up(struct connman_ipconfig *ipconfig)
2988 {
2989         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
2990
2991         connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
2992
2993         service->stats.valid = FALSE;
2994 }
2995
2996 static void service_down(struct connman_ipconfig *ipconfig)
2997 {
2998         connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
2999 }
3000
3001 static void service_lower_up(struct connman_ipconfig *ipconfig)
3002 {
3003         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3004
3005         connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
3006
3007         stats_start(service);
3008 }
3009
3010 static void service_lower_down(struct connman_ipconfig *ipconfig)
3011 {
3012         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3013
3014         connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
3015
3016         stats_stop(service);
3017         __connman_storage_save_service(service);
3018 }
3019
3020 static void service_ip_bound(struct connman_ipconfig *ipconfig)
3021 {
3022         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3023
3024         connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
3025
3026         settings_changed(service);
3027 }
3028
3029 static void service_ip_release(struct connman_ipconfig *ipconfig)
3030 {
3031         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3032
3033         connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
3034
3035         settings_changed(service);
3036 }
3037
3038 static const struct connman_ipconfig_ops service_ops = {
3039         .up             = service_up,
3040         .down           = service_down,
3041         .lower_up       = service_lower_up,
3042         .lower_down     = service_lower_down,
3043         .ip_bound       = service_ip_bound,
3044         .ip_release     = service_ip_release,
3045 };
3046
3047 static void setup_ipconfig(struct connman_service *service, int index)
3048 {
3049         if (index < 0)
3050                 return;
3051
3052         service->ipconfig = connman_ipconfig_create(index);
3053         if (service->ipconfig == NULL)
3054                 return;
3055
3056         connman_ipconfig_set_method(service->ipconfig,
3057                                         CONNMAN_IPCONFIG_METHOD_DHCP);
3058
3059         connman_ipconfig_set_data(service->ipconfig, service);
3060
3061         connman_ipconfig_set_ops(service->ipconfig, &service_ops);
3062 }
3063
3064 void __connman_service_create_ipconfig(struct connman_service *service,
3065                                                                 int index)
3066 {
3067         struct connman_ipconfig *ipv6config;
3068         const char *ident = service->profile;
3069         GKeyFile *keyfile;
3070
3071         if (service->ipconfig != NULL)
3072                 return;
3073
3074         setup_ipconfig(service, index);
3075
3076         if (ident == NULL)
3077                 return;
3078
3079         keyfile = __connman_storage_open_profile(ident);
3080         if (keyfile == NULL)
3081                 return;
3082
3083
3084         ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
3085         if (ipv6config != NULL)
3086                 __connman_ipconfig_load(ipv6config, keyfile,
3087                                         service->identifier, "IPv6.");
3088
3089         __connman_ipconfig_load(service->ipconfig, keyfile,
3090                                         service->identifier, "IPv4.");
3091         g_key_file_free(keyfile);
3092 }
3093
3094 /**
3095  * __connman_service_lookup_from_network:
3096  * @network: network structure
3097  *
3098  * Look up a service by network (reference count will not be increased)
3099  */
3100 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
3101 {
3102         struct connman_service *service;
3103         const char *ident, *group;
3104         char *name;
3105
3106         DBG("network %p", network);
3107
3108         ident = __connman_network_get_ident(network);
3109         if (ident == NULL)
3110                 return NULL;
3111
3112         group = connman_network_get_group(network);
3113         if (group == NULL)
3114                 return NULL;
3115
3116         name = g_strdup_printf("%s_%s_%s",
3117                         __connman_network_get_type(network), ident, group);
3118         service = lookup_by_identifier(name);
3119         g_free(name);
3120
3121         return service;
3122 }
3123
3124 const char *__connman_service_get_path(struct connman_service *service)
3125 {
3126         return service->path;
3127 }
3128
3129 unsigned int __connman_service_get_order(struct connman_service *service)
3130 {
3131         GSequenceIter *iter;
3132
3133         if (service == NULL)
3134                 return 0;
3135
3136         if (service->favorite == FALSE) {
3137                 service->order = 0;
3138                 goto done;
3139         }
3140
3141         iter = g_hash_table_lookup(service_hash, service->identifier);
3142         if (iter != NULL) {
3143                 if (g_sequence_iter_get_position(iter) == 0)
3144                         service->order = 1;
3145                 else
3146                         service->order = 0;
3147         }
3148
3149 done:
3150         return service->order;
3151 }
3152
3153 static enum connman_service_type convert_network_type(struct connman_network *network)
3154 {
3155         enum connman_network_type type = connman_network_get_type(network);
3156
3157         switch (type) {
3158         case CONNMAN_NETWORK_TYPE_UNKNOWN:
3159         case CONNMAN_NETWORK_TYPE_VENDOR:
3160                 break;
3161         case CONNMAN_NETWORK_TYPE_ETHERNET:
3162                 return CONNMAN_SERVICE_TYPE_ETHERNET;
3163         case CONNMAN_NETWORK_TYPE_WIFI:
3164                 return CONNMAN_SERVICE_TYPE_WIFI;
3165         case CONNMAN_NETWORK_TYPE_WIMAX:
3166                 return CONNMAN_SERVICE_TYPE_WIMAX;
3167         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3168         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3169                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
3170         case CONNMAN_NETWORK_TYPE_CELLULAR:
3171                 return CONNMAN_SERVICE_TYPE_CELLULAR;
3172         }
3173
3174         return CONNMAN_SERVICE_TYPE_UNKNOWN;
3175 }
3176
3177 static enum connman_service_mode convert_wifi_mode(const char *mode)
3178 {
3179         if (mode == NULL)
3180                 return CONNMAN_SERVICE_MODE_UNKNOWN;
3181         else if (g_str_equal(mode, "managed") == TRUE)
3182                 return CONNMAN_SERVICE_MODE_MANAGED;
3183         else if (g_str_equal(mode, "adhoc") == TRUE)
3184                 return CONNMAN_SERVICE_MODE_ADHOC;
3185         else
3186                 return CONNMAN_SERVICE_MODE_UNKNOWN;
3187 }
3188
3189 static enum connman_service_security convert_wifi_security(const char *security)
3190 {
3191         if (security == NULL)
3192                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3193         else if (g_str_equal(security, "none") == TRUE)
3194                 return CONNMAN_SERVICE_SECURITY_NONE;
3195         else if (g_str_equal(security, "wep") == TRUE)
3196                 return CONNMAN_SERVICE_SECURITY_WEP;
3197         else if (g_str_equal(security, "psk") == TRUE)
3198                 return CONNMAN_SERVICE_SECURITY_PSK;
3199         else if (g_str_equal(security, "ieee8021x") == TRUE)
3200                 return CONNMAN_SERVICE_SECURITY_8021X;
3201         else if (g_str_equal(security, "wpa") == TRUE)
3202                 return CONNMAN_SERVICE_SECURITY_WPA;
3203         else if (g_str_equal(security, "rsn") == TRUE)
3204                 return CONNMAN_SERVICE_SECURITY_RSN;
3205         else
3206                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3207 }
3208
3209 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
3210 {
3211         switch (mode) {
3212         case 0:
3213         case 1:
3214                 return CONNMAN_SERVICE_MODE_GPRS;
3215         case 3:
3216                 return CONNMAN_SERVICE_MODE_EDGE;
3217         case 2:
3218         case 4:
3219         case 5:
3220         case 6:
3221                 return CONNMAN_SERVICE_MODE_UMTS;
3222         }
3223
3224         return CONNMAN_SERVICE_MODE_UNKNOWN;
3225 }
3226
3227 static void update_from_network(struct connman_service *service,
3228                                         struct connman_network *network)
3229 {
3230         connman_uint8_t strength = service->strength;
3231         GSequenceIter *iter;
3232         const char *str;
3233
3234         DBG("service %p network %p", service, network);
3235
3236         if (is_connected(service) == TRUE)
3237                 return;
3238
3239         if (is_connecting(service) == TRUE)
3240                 return;
3241
3242         str = connman_network_get_string(network, "Name");
3243         if (str != NULL) {
3244                 g_free(service->name);
3245                 service->name = g_strdup(str);
3246                 service->hidden = FALSE;
3247         } else {
3248                 g_free(service->name);
3249                 service->name = NULL;
3250                 service->hidden = TRUE;
3251         }
3252
3253         service->strength = connman_network_get_uint8(network, "Strength");
3254         service->roaming = connman_network_get_bool(network, "Roaming");
3255
3256         if (service->strength == 0) {
3257                 /*
3258                  * Filter out 0-values; it's unclear what they mean
3259                  * and they cause anomalous sorting of the priority list.
3260                  */
3261                 service->strength = strength;
3262         }
3263
3264         str = connman_network_get_string(network, "WiFi.Mode");
3265         service->mode = convert_wifi_mode(str);
3266
3267         str = connman_network_get_string(network, "WiFi.Security");
3268         service->security = convert_wifi_security(str);
3269
3270         str = connman_network_get_string(network, "Cellular.MCC");
3271         g_free(service->mcc);
3272         service->mcc = g_strdup(str);
3273
3274         str = connman_network_get_string(network, "Cellular.MNC");
3275         g_free(service->mnc);
3276         service->mnc = g_strdup(str);
3277
3278         if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
3279                 connman_uint8_t value = connman_network_get_uint8(network,
3280                                                         "Cellular.Mode");
3281
3282                 service->mode = convert_cellular_mode(value);
3283         }
3284
3285         if (service->strength > strength && service->network != NULL) {
3286                 connman_network_unref(service->network);
3287                 service->network = connman_network_ref(network);
3288
3289                 strength_changed(service);
3290         }
3291
3292         if (service->network == NULL)
3293                 service->network = connman_network_ref(network);
3294
3295         iter = g_hash_table_lookup(service_hash, service->identifier);
3296         if (iter != NULL)
3297                 g_sequence_sort_changed(iter, service_compare, NULL);
3298 }
3299
3300 /**
3301  * __connman_service_create_from_network:
3302  * @network: network structure
3303  *
3304  * Look up service by network and if not found, create one
3305  */
3306 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
3307 {
3308         struct connman_service *service;
3309         const char *ident, *group;
3310         char *name;
3311
3312         DBG("network %p", network);
3313
3314         ident = __connman_network_get_ident(network);
3315         if (ident == NULL)
3316                 return NULL;
3317
3318         group = connman_network_get_group(network);
3319         if (group == NULL)
3320                 return NULL;
3321
3322         name = g_strdup_printf("%s_%s_%s",
3323                         __connman_network_get_type(network), ident, group);
3324         service = service_get(name);
3325         g_free(name);
3326
3327         if (service == NULL)
3328                 return NULL;
3329
3330         if (__connman_network_get_weakness(network) == TRUE)
3331                 return service;
3332
3333         if (service->path != NULL) {
3334                 update_from_network(service, network);
3335                 __connman_profile_changed(TRUE);
3336                 return service;
3337         }
3338
3339         service->type = convert_network_type(network);
3340
3341         switch (service->type) {
3342         case CONNMAN_SERVICE_TYPE_UNKNOWN:
3343         case CONNMAN_SERVICE_TYPE_SYSTEM:
3344         case CONNMAN_SERVICE_TYPE_ETHERNET:
3345         case CONNMAN_SERVICE_TYPE_WIMAX:
3346         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3347         case CONNMAN_SERVICE_TYPE_GPS:
3348         case CONNMAN_SERVICE_TYPE_VPN:
3349                 service->autoconnect = FALSE;
3350                 break;
3351         case CONNMAN_SERVICE_TYPE_WIFI:
3352         case CONNMAN_SERVICE_TYPE_CELLULAR:
3353                 service->autoconnect = TRUE;
3354                 break;
3355         }
3356
3357         service->state = CONNMAN_SERVICE_STATE_IDLE;
3358
3359         update_from_network(service, network);
3360
3361         setup_ipconfig(service, connman_network_get_index(network));
3362
3363         service_register(service);
3364
3365         if (service->favorite == TRUE)
3366                 __connman_service_auto_connect();
3367
3368         return service;
3369 }
3370
3371 void __connman_service_update_from_network(struct connman_network *network)
3372 {
3373         struct connman_service *service;
3374         enum connman_service_mode mode;
3375         connman_uint8_t strength, value;
3376         connman_bool_t roaming;
3377         GSequenceIter *iter;
3378         const char *name;
3379
3380         DBG("network %p", network);
3381
3382         service = __connman_service_lookup_from_network(network);
3383         if (service == NULL)
3384                 return;
3385
3386         if (service->network == NULL)
3387                 return;
3388
3389         name = connman_network_get_string(service->network, "Name");
3390         if (g_strcmp0(service->name, name) != 0) {
3391                 g_free(service->name);
3392                 service->name = g_strdup(name);
3393                 connman_dbus_property_changed_basic(service->path,
3394                                 CONNMAN_SERVICE_INTERFACE, "Name",
3395                                 DBUS_TYPE_STRING, &service->name);
3396         }
3397
3398         strength = connman_network_get_uint8(service->network, "Strength");
3399         if (strength == service->strength)
3400                 goto roaming;
3401
3402         service->strength = strength;
3403
3404         strength_changed(service);
3405
3406 roaming:
3407         roaming = connman_network_get_bool(service->network, "Roaming");
3408         if (roaming == service->roaming)
3409                 goto done;
3410
3411         service->roaming = roaming;
3412
3413         roaming_changed(service);
3414
3415         iter = g_hash_table_lookup(service_hash, service->identifier);
3416         if (iter != NULL)
3417                 g_sequence_sort_changed(iter, service_compare, NULL);
3418
3419 done:
3420         if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
3421                 return;
3422
3423         value = connman_network_get_uint8(service->network, "Cellular.Mode");
3424         mode = convert_cellular_mode(value);
3425
3426         if (mode == service->mode)
3427                 return;
3428
3429         service->mode = mode;
3430
3431         mode_changed(service);
3432 }
3433
3434 void __connman_service_remove_from_network(struct connman_network *network)
3435 {
3436         struct connman_service *service;
3437
3438         DBG("network %p", network);
3439
3440         service = __connman_service_lookup_from_network(network);
3441         if (service == NULL)
3442                 return;
3443
3444         __connman_service_put(service);
3445 }
3446
3447 /**
3448  * __connman_service_create_from_provider:
3449  * @provider: provider structure
3450  *
3451  * Look up service by provider and if not found, create one
3452  */
3453 struct connman_service *
3454 __connman_service_create_from_provider(struct connman_provider *provider)
3455 {
3456         struct connman_service *service;
3457         const char *ident, *str;
3458         char *name;
3459         int index = connman_provider_get_index(provider);
3460
3461         DBG("provider %p", provider);
3462
3463         ident = __connman_provider_get_ident(provider);
3464         if (ident == NULL)
3465                 return NULL;
3466
3467         name = g_strdup_printf("vpn_%s", ident);
3468         service = service_get(name);
3469         g_free(name);
3470
3471         if (service == NULL)
3472                 return NULL;
3473
3474         service->type = CONNMAN_SERVICE_TYPE_VPN;
3475         service->provider = connman_provider_ref(provider);
3476         service->autoconnect = FALSE;
3477
3478         service->state = CONNMAN_SERVICE_STATE_IDLE;
3479
3480         str = connman_provider_get_string(provider, "Name");
3481         if (str != NULL) {
3482                 g_free(service->name);
3483                 service->name = g_strdup(str);
3484                 service->hidden = FALSE;
3485         } else {
3486                 g_free(service->name);
3487                 service->name = NULL;
3488                 service->hidden = TRUE;
3489         }
3490
3491         service->strength = 0;
3492
3493         service->ipconfig = connman_ipconfig_create(index);
3494         if (service->ipconfig == NULL)
3495                 return service;
3496
3497         connman_ipconfig_set_method(service->ipconfig,
3498                                         CONNMAN_IPCONFIG_METHOD_MANUAL);
3499
3500         connman_ipconfig_set_data(service->ipconfig, service);
3501
3502         connman_ipconfig_set_ops(service->ipconfig, &service_ops);
3503
3504         service_register(service);
3505
3506         return service;
3507 }
3508
3509 void __connman_service_stats_update(struct connman_service *service,
3510                                 unsigned int rx_packets, unsigned int tx_packets,
3511                                 unsigned int rx_bytes, unsigned int tx_bytes,
3512                                 unsigned int rx_errors, unsigned int tx_errors,
3513                                 unsigned int rx_dropped, unsigned int tx_dropped)
3514 {
3515         unsigned int seconds;
3516         struct connman_stats *stats = &service->stats;
3517
3518         DBG("service %p", service);
3519
3520         if (is_connected(service) == FALSE)
3521                 return;
3522
3523         if (stats->valid == TRUE) {
3524                 stats->rx_packets +=
3525                         rx_packets - stats->rx_packets_last;
3526                 stats->tx_packets +=
3527                         tx_packets - stats->tx_packets_last;
3528                 stats->rx_bytes +=
3529                         rx_bytes - stats->rx_bytes_last;
3530                 stats->tx_bytes +=
3531                         tx_bytes - stats->tx_bytes_last;
3532                 stats->rx_errors +=
3533                         rx_errors - stats->rx_errors_last;
3534                 stats->tx_errors +=
3535                         tx_errors - stats->tx_errors_last;
3536                 stats->rx_dropped +=
3537                         rx_dropped - stats->rx_dropped_last;
3538                 stats->tx_dropped +=
3539                         tx_dropped - stats->tx_dropped_last;
3540         } else {
3541                 stats->valid = TRUE;
3542         }
3543
3544         stats->rx_packets_last = rx_packets;
3545         stats->tx_packets_last = tx_packets;
3546         stats->rx_bytes_last = rx_bytes;
3547         stats->tx_bytes_last = tx_bytes;
3548         stats->rx_errors_last = rx_errors;
3549         stats->tx_errors_last = tx_errors;
3550         stats->rx_dropped_last = rx_dropped;
3551         stats->tx_dropped_last = tx_dropped;
3552
3553         seconds = g_timer_elapsed(stats->timer, NULL);
3554         stats->time = stats->time_start + seconds;
3555 }
3556
3557 static int service_load(struct connman_service *service)
3558 {
3559         const char *ident = service->profile;
3560         GKeyFile *keyfile;
3561         GError *error = NULL;
3562         gchar *pathname, *data = NULL;
3563         gsize length;
3564         gchar *str;
3565         connman_bool_t autoconnect;
3566         unsigned int ssid_len;
3567         int err = 0;
3568
3569         DBG("service %p", service);
3570
3571         if (ident == NULL)
3572                 return -EINVAL;
3573
3574         pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
3575         if (pathname == NULL)
3576                 return -ENOMEM;
3577
3578         keyfile = g_key_file_new();
3579
3580         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
3581                 g_free(pathname);
3582                 return -ENOENT;
3583         }
3584
3585         g_free(pathname);
3586
3587         if (g_key_file_load_from_data(keyfile, data, length,
3588                                                         0, NULL) == FALSE) {
3589                 g_free(data);
3590                 return -EILSEQ;
3591         }
3592
3593         g_free(data);
3594
3595         switch (service->type) {
3596         case CONNMAN_SERVICE_TYPE_UNKNOWN:
3597         case CONNMAN_SERVICE_TYPE_SYSTEM:
3598         case CONNMAN_SERVICE_TYPE_ETHERNET:
3599         case CONNMAN_SERVICE_TYPE_GPS:
3600         case CONNMAN_SERVICE_TYPE_VPN:
3601                 break;
3602         case CONNMAN_SERVICE_TYPE_WIFI:
3603                 if (service->name == NULL) {
3604                         gchar *name;
3605
3606                         name = g_key_file_get_string(keyfile,
3607                                         service->identifier, "Name", NULL);
3608                         if (name != NULL) {
3609                                 g_free(service->name);
3610                                 service->name = name;
3611                         }
3612
3613                         if (service->network != NULL)
3614                                 connman_network_set_name(service->network,
3615                                                                         name);
3616                 }
3617
3618                 if (service->network &&
3619                                 connman_network_get_blob(service->network,
3620                                         "WiFi.SSID", &ssid_len) == NULL) {
3621                         gchar *hex_ssid;
3622
3623                         hex_ssid = g_key_file_get_string(keyfile,
3624                                                         service->identifier,
3625                                                                 "SSID", NULL);
3626
3627                         if (hex_ssid != NULL) {
3628                                 gchar *ssid;
3629                                 unsigned int i, j = 0, hex;
3630                                 size_t hex_ssid_len = strlen(hex_ssid);
3631
3632                                 ssid = g_try_malloc0(hex_ssid_len / 2);
3633                                 if (ssid == NULL) {
3634                                         g_free(hex_ssid);
3635                                         err = -ENOMEM;
3636                                         goto done;
3637                                 }
3638
3639                                 for (i = 0; i < hex_ssid_len; i += 2) {
3640                                         sscanf(hex_ssid + i, "%02x", &hex);
3641                                         ssid[j++] = hex;
3642                                 }
3643
3644                                 connman_network_set_blob(service->network,
3645                                         "WiFi.SSID", ssid, hex_ssid_len / 2);
3646                         }
3647
3648                         g_free(hex_ssid);
3649                 }
3650                 /* fall through */
3651
3652         case CONNMAN_SERVICE_TYPE_WIMAX:
3653         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3654         case CONNMAN_SERVICE_TYPE_CELLULAR:
3655                 service->apn = g_key_file_get_string(keyfile,
3656                                         service->identifier, "APN", NULL);
3657
3658                 service->username = g_key_file_get_string(keyfile,
3659                                         service->identifier, "Username", NULL);
3660
3661                 service->password = g_key_file_get_string(keyfile,
3662                                         service->identifier, "Password", NULL);
3663
3664                 service->favorite = g_key_file_get_boolean(keyfile,
3665                                 service->identifier, "Favorite", NULL);
3666
3667                 autoconnect = g_key_file_get_boolean(keyfile,
3668                                 service->identifier, "AutoConnect", &error);
3669                 if (error == NULL)
3670                         service->autoconnect = autoconnect;
3671                 g_clear_error(&error);
3672
3673                 str = g_key_file_get_string(keyfile,
3674                                 service->identifier, "Failure", NULL);
3675                 if (str != NULL) {
3676                         service->state = CONNMAN_SERVICE_STATE_FAILURE;
3677                         service->error = string2error(str);
3678                 }
3679                 break;
3680         }
3681
3682         str = g_key_file_get_string(keyfile,
3683                                 service->identifier, "Modified", NULL);
3684         if (str != NULL) {
3685                 g_time_val_from_iso8601(str, &service->modified);
3686                 g_free(str);
3687         }
3688
3689         str = g_key_file_get_string(keyfile,
3690                                 service->identifier, "Passphrase", NULL);
3691         if (str != NULL) {
3692                 g_free(service->passphrase);
3693                 service->passphrase = str;
3694         }
3695
3696         if (service->ipconfig != NULL) {
3697                 struct connman_ipconfig *ipv6config;
3698
3699                 ipv6config = connman_ipconfig_get_ipv6config(
3700                                                 service->ipconfig);
3701                 if (ipv6config != NULL)
3702                         __connman_ipconfig_load(ipv6config, keyfile,
3703                                         service->identifier, "IPv6.");
3704
3705                 __connman_ipconfig_load(service->ipconfig, keyfile,
3706                                         service->identifier, "IPv4.");
3707         }
3708
3709         service->nameservers = g_key_file_get_string_list(keyfile,
3710                         service->identifier, "Nameservers", &length, NULL);
3711         if (service->nameservers != NULL && length == 0) {
3712                 g_strfreev(service->nameservers);
3713                 service->nameservers = NULL;
3714         }
3715
3716         service->domains = g_key_file_get_string_list(keyfile,
3717                         service->identifier, "Domains", &length, NULL);
3718         if (service->domains != NULL && length == 0) {
3719                 g_strfreev(service->domains);
3720                 service->domains = NULL;
3721         }
3722
3723         stats_load(service, keyfile, service->identifier);
3724 done:
3725         g_key_file_free(keyfile);
3726
3727         return err;
3728 }
3729
3730 static int service_save(struct connman_service *service)
3731 {
3732         const char *ident = service->profile;
3733         GKeyFile *keyfile;
3734         gchar *pathname, *data = NULL;
3735         gsize length;
3736         gchar *str;
3737         int err = 0;
3738
3739         DBG("service %p", service);
3740
3741         if (ident == NULL)
3742                 return -EINVAL;
3743
3744         pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
3745         if (pathname == NULL)
3746                 return -ENOMEM;
3747
3748         keyfile = g_key_file_new();
3749
3750         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
3751                 goto update;
3752
3753         if (length > 0) {
3754                 if (g_key_file_load_from_data(keyfile, data, length,
3755                                                         0, NULL) == FALSE)
3756                         goto done;
3757         }
3758
3759         g_free(data);
3760
3761 update:
3762         if (service->name != NULL)
3763                 g_key_file_set_string(keyfile, service->identifier,
3764                                                 "Name", service->name);
3765
3766         switch (service->type) {
3767         case CONNMAN_SERVICE_TYPE_UNKNOWN:
3768         case CONNMAN_SERVICE_TYPE_SYSTEM:
3769         case CONNMAN_SERVICE_TYPE_ETHERNET:
3770         case CONNMAN_SERVICE_TYPE_GPS:
3771         case CONNMAN_SERVICE_TYPE_VPN:
3772                 break;
3773         case CONNMAN_SERVICE_TYPE_WIFI:
3774                 if (service->network) {
3775                         const unsigned char *ssid;
3776                         unsigned int ssid_len = 0;
3777
3778                         ssid = connman_network_get_blob(service->network,
3779                                                         "WiFi.SSID", &ssid_len);
3780
3781                         if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
3782                                 char *identifier = service->identifier;
3783                                 GString *str;
3784                                 unsigned int i;
3785
3786                                 str = g_string_sized_new(ssid_len * 2);
3787                                 if (str == NULL) {
3788                                         err = -ENOMEM;
3789                                         goto done;
3790                                 }
3791
3792                                 for (i = 0; i < ssid_len; i++)
3793                                         g_string_append_printf(str,
3794                                                         "%02x", ssid[i]);
3795
3796                                 g_key_file_set_string(keyfile, identifier,
3797                                                         "SSID", str->str);
3798
3799                                 g_string_free(str, TRUE);
3800                         }
3801                 }
3802                 /* fall through */
3803
3804         case CONNMAN_SERVICE_TYPE_WIMAX:
3805         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3806         case CONNMAN_SERVICE_TYPE_CELLULAR:
3807                 if (service->apn != NULL)
3808                         g_key_file_set_string(keyfile, service->identifier,
3809                                                         "APN", service->apn);
3810
3811                 if (service->username != NULL)
3812                         g_key_file_set_string(keyfile, service->identifier,
3813                                                 "Username", service->username);
3814
3815                 if (service->password != NULL)
3816                         g_key_file_set_string(keyfile, service->identifier,
3817                                                 "Password", service->password);
3818
3819                 g_key_file_set_boolean(keyfile, service->identifier,
3820                                         "Favorite", service->favorite);
3821
3822                 if (service->favorite == TRUE)
3823                         g_key_file_set_boolean(keyfile, service->identifier,
3824                                         "AutoConnect", service->autoconnect);
3825
3826                 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
3827                         const char *failure = error2string(service->error);
3828                         if (failure != NULL)
3829                                 g_key_file_set_string(keyfile,
3830                                                         service->identifier,
3831                                                         "Failure", failure);
3832                 } else {
3833                         g_key_file_remove_key(keyfile, service->identifier,
3834                                                         "Failure", NULL);
3835                 }
3836                 break;
3837         }
3838
3839         str = g_time_val_to_iso8601(&service->modified);
3840         if (str != NULL) {
3841                 g_key_file_set_string(keyfile, service->identifier,
3842                                                         "Modified", str);
3843                 g_free(str);
3844         }
3845
3846         if (service->passphrase != NULL && strlen(service->passphrase) > 0)
3847                 g_key_file_set_string(keyfile, service->identifier,
3848                                         "Passphrase", service->passphrase);
3849         else
3850                 g_key_file_remove_key(keyfile, service->identifier,
3851                                                         "Passphrase", NULL);
3852
3853         if (service->ipconfig != NULL) {
3854                 struct connman_ipconfig *ipv6config;
3855
3856                 ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
3857                 if (ipv6config != NULL)
3858                         __connman_ipconfig_save(ipv6config, keyfile,
3859                                                 service->identifier, "IPv6.");
3860
3861                 __connman_ipconfig_save(service->ipconfig, keyfile,
3862                                         service->identifier, "IPv4.");
3863         }
3864
3865         if (service->nameservers != NULL) {
3866                 guint len = g_strv_length(service->nameservers);
3867
3868                 g_key_file_set_string_list(keyfile, service->identifier,
3869                                                                 "Nameservers",
3870                                 (const gchar **) service->nameservers, len);
3871         } else
3872                 g_key_file_remove_key(keyfile, service->identifier,
3873                                                         "Nameservers", NULL);
3874
3875         if (service->domains != NULL) {
3876                 guint len = g_strv_length(service->domains);
3877
3878                 g_key_file_set_string_list(keyfile, service->identifier,
3879                                                                 "Domains",
3880                                 (const gchar **) service->domains, len);
3881         } else
3882                 g_key_file_remove_key(keyfile, service->identifier,
3883                                                         "Domains", NULL);
3884
3885         stats_save(service, keyfile, service->identifier);
3886
3887         data = g_key_file_to_data(keyfile, &length, NULL);
3888
3889         if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
3890                 connman_error("Failed to store service information");
3891
3892 done:
3893         g_free(data);
3894
3895         g_key_file_free(keyfile);
3896
3897         g_free(pathname);
3898
3899         return err;
3900 }
3901
3902 static struct connman_storage service_storage = {
3903         .name           = "service",
3904         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
3905         .service_load   = service_load,
3906         .service_save   = service_save,
3907 };
3908
3909 int __connman_service_init(void)
3910 {
3911         DBG("");
3912
3913         connection = connman_dbus_get_connection();
3914
3915         if (connman_storage_register(&service_storage) < 0)
3916                 connman_error("Failed to register service storage");
3917
3918         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
3919                                                                 NULL, NULL);
3920
3921         service_list = g_sequence_new(service_free);
3922
3923         return 0;
3924 }
3925
3926 void __connman_service_cleanup(void)
3927 {
3928         DBG("");
3929
3930         g_sequence_free(service_list);
3931         service_list = NULL;
3932
3933         g_hash_table_destroy(service_hash);
3934         service_hash = NULL;
3935
3936         connman_storage_unregister(&service_storage);
3937
3938         dbus_connection_unref(connection);
3939 }