Track and report VPN providers states
[platform/upstream/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 __connman_service_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 __connman_service_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 __connman_service_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 __connman_service_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 __connman_service_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_stats(DBusConnection *conn,
1959                                         DBusMessage *msg, void *user_data)
1960 {
1961         struct connman_service *service = user_data;
1962
1963         __connman_service_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_stats        },
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         __connman_service_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->ipconfig != NULL) {
2015                 connman_ipconfig_unref(service->ipconfig);
2016                 service->ipconfig = NULL;
2017         }
2018
2019         if (service->location != NULL)
2020                 connman_location_unref(service->location);
2021
2022         g_strfreev(service->nameservers);
2023         g_strfreev(service->domains);
2024
2025         g_free(service->nameserver);
2026         g_free(service->domainname);
2027         g_free(service->mcc);
2028         g_free(service->mnc);
2029         g_free(service->apn);
2030         g_free(service->username);
2031         g_free(service->password);
2032         g_free(service->profile);
2033         g_free(service->name);
2034         g_free(service->passphrase);
2035         g_free(service->identifier);
2036         g_free(service->eap);
2037         g_free(service->identity);
2038         g_free(service->ca_cert_file);
2039         g_free(service->client_cert_file);
2040         g_free(service->private_key_file);
2041         g_free(service->private_key_passphrase);
2042         g_free(service->phase2);
2043
2044         if (service->stats.timer != NULL)
2045                 g_timer_destroy(service->stats.timer);
2046
2047         g_free(service);
2048 }
2049
2050 /**
2051  * __connman_service_put:
2052  * @service: service structure
2053  *
2054  * Release service if no longer needed
2055  */
2056 void __connman_service_put(struct connman_service *service)
2057 {
2058         DBG("service %p", service);
2059
2060         if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
2061                 GSequenceIter *iter;
2062
2063                 iter = g_hash_table_lookup(service_hash, service->identifier);
2064                 if (iter != NULL) {
2065                         reply_pending(service, ECONNABORTED);
2066
2067                         __connman_service_disconnect(service);
2068
2069                         g_sequence_remove(iter);
2070                 } else
2071                         service_free(service);
2072         }
2073 }
2074
2075 static void __connman_service_initialize(struct connman_service *service)
2076 {
2077         DBG("service %p", service);
2078
2079         service->refcount = 1;
2080
2081         service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
2082         service->mode     = CONNMAN_SERVICE_MODE_UNKNOWN;
2083         service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
2084         service->state    = CONNMAN_SERVICE_STATE_UNKNOWN;
2085
2086         service->favorite  = FALSE;
2087         service->immutable = FALSE;
2088         service->hidden = FALSE;
2089
2090         service->ignore = FALSE;
2091
2092         service->userconnect = FALSE;
2093
2094         service->order = 0;
2095
2096         service->stats.valid = FALSE;
2097         service->stats.enabled = FALSE;
2098         service->stats.timer = g_timer_new();
2099
2100         service->provider = NULL;
2101 }
2102
2103 /**
2104  * connman_service_create:
2105  *
2106  * Allocate a new service.
2107  *
2108  * Returns: a newly-allocated #connman_service structure
2109  */
2110 struct connman_service *connman_service_create(void)
2111 {
2112         struct connman_service *service;
2113
2114         service = g_try_new0(struct connman_service, 1);
2115         if (service == NULL)
2116                 return NULL;
2117
2118         DBG("service %p", service);
2119
2120         __connman_service_initialize(service);
2121
2122         service->location = __connman_location_create(service);
2123
2124         return service;
2125 }
2126
2127 struct connman_location *__connman_service_get_location(struct connman_service *service)
2128 {
2129         return service->location;
2130 }
2131
2132 /**
2133  * connman_service_ref:
2134  * @service: service structure
2135  *
2136  * Increase reference counter of service
2137  */
2138 struct connman_service *connman_service_ref(struct connman_service *service)
2139 {
2140         g_atomic_int_inc(&service->refcount);
2141
2142         return service;
2143 }
2144
2145 /**
2146  * connman_service_unref:
2147  * @service: service structure
2148  *
2149  * Decrease reference counter of service
2150  */
2151 void connman_service_unref(struct connman_service *service)
2152 {
2153         __connman_service_put(service);
2154 }
2155
2156 static gint service_compare(gconstpointer a, gconstpointer b,
2157                                                         gpointer user_data)
2158 {
2159         struct connman_service *service_a = (void *) a;
2160         struct connman_service *service_b = (void *) b;
2161
2162         if (service_a->state != service_b->state) {
2163                 if (is_connected(service_a) == TRUE)
2164                         return -1;
2165                 if (is_connected(service_b) == TRUE)
2166                         return 1;
2167         }
2168
2169         if (service_a->order > service_b->order)
2170                 return -1;
2171
2172         if (service_a->order < service_b->order)
2173                 return 1;
2174
2175         if (service_a->favorite == TRUE && service_b->favorite == FALSE)
2176                 return -1;
2177
2178         if (service_a->favorite == FALSE && service_b->favorite == TRUE)
2179                 return 1;
2180
2181         if (service_a->type != service_b->type) {
2182                 switch (service_a->type) {
2183                 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2184                 case CONNMAN_SERVICE_TYPE_SYSTEM:
2185                 case CONNMAN_SERVICE_TYPE_ETHERNET:
2186                 case CONNMAN_SERVICE_TYPE_GPS:
2187                 case CONNMAN_SERVICE_TYPE_VPN:
2188                         break;
2189                 case CONNMAN_SERVICE_TYPE_WIFI:
2190                         return 1;
2191                 case CONNMAN_SERVICE_TYPE_WIMAX:
2192                 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2193                 case CONNMAN_SERVICE_TYPE_CELLULAR:
2194                         return -1;
2195                 }
2196         }
2197
2198         return (gint) service_b->strength - (gint) service_a->strength;
2199 }
2200
2201 /**
2202  * connman_service_get_type:
2203  * @service: service structure
2204  *
2205  * Get the type of service
2206  */
2207 enum connman_service_type connman_service_get_type(struct connman_service *service)
2208 {
2209         if (service == NULL)
2210                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
2211
2212         return service->type;
2213 }
2214
2215 /**
2216  * connman_service_get_interface:
2217  * @service: service structure
2218  *
2219  * Get network interface of service
2220  */
2221 char *connman_service_get_interface(struct connman_service *service)
2222 {
2223         int index;
2224
2225         if (service == NULL)
2226                 return NULL;
2227
2228         if (service->network == NULL)
2229                 return NULL;
2230
2231         index = connman_network_get_index(service->network);
2232
2233         return connman_inet_ifname(index);
2234 }
2235
2236 /**
2237  * connman_service_get_network:
2238  * @service: service structure
2239  *
2240  * Get the service network
2241  */
2242 struct connman_network *
2243 __connman_service_get_network(struct connman_service *service)
2244 {
2245         if (service == NULL)
2246                 return NULL;
2247
2248         return service->network;
2249 }
2250
2251 struct connman_ipconfig *__connman_service_get_ipconfig(struct connman_service *service)
2252 {
2253         if (service == NULL)
2254                 return NULL;
2255
2256         return service->ipconfig;
2257 }
2258
2259 /**
2260  * __connman_service_set_favorite:
2261  * @service: service structure
2262  * @favorite: favorite value
2263  *
2264  * Change the favorite setting of service
2265  */
2266 int __connman_service_set_favorite(struct connman_service *service,
2267                                                 connman_bool_t favorite)
2268 {
2269         GSequenceIter *iter;
2270
2271         iter = g_hash_table_lookup(service_hash, service->identifier);
2272         if (iter == NULL)
2273                 return -ENOENT;
2274
2275         if (service->favorite == favorite)
2276                 return -EALREADY;
2277
2278         service->favorite = favorite;
2279
2280         favorite_changed(service);
2281
2282         g_sequence_sort_changed(iter, service_compare, NULL);
2283
2284         __connman_profile_changed(FALSE);
2285
2286         return 0;
2287 }
2288
2289 int __connman_service_set_immutable(struct connman_service *service,
2290                                                 connman_bool_t immutable)
2291 {
2292         service->immutable = immutable;
2293
2294         immutable_changed(service);
2295
2296         return 0;
2297 }
2298
2299 void __connman_service_set_string(struct connman_service *service,
2300                                   const char *key, const char *value)
2301 {
2302         if (g_str_equal(key, "EAP") == TRUE) {
2303                 g_free(service->eap);
2304                 service->eap = g_strdup(value);
2305         } else if (g_str_equal(key, "Identity") == TRUE) {
2306                 g_free(service->identity);
2307                 service->identity = g_strdup(value);
2308         } else if (g_str_equal(key, "CACertFile") == TRUE) {
2309                 g_free(service->ca_cert_file);
2310                 service->ca_cert_file = g_strdup(value);
2311         } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
2312                 g_free(service->client_cert_file);
2313                 service->client_cert_file = g_strdup(value);
2314         } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
2315                 g_free(service->private_key_file);
2316                 service->private_key_file = g_strdup(value);
2317         } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
2318                 g_free(service->private_key_passphrase);
2319                 service->private_key_passphrase = g_strdup(value);
2320         } else if (g_str_equal(key, "Phase2") == TRUE) {
2321                 g_free(service->phase2);
2322                 service->phase2 = g_strdup(value);
2323         } else if (g_str_equal(key, "Passphrase") == TRUE) {
2324                 g_free(service->passphrase);
2325                 service->passphrase = g_strdup(value);
2326         }
2327 }
2328
2329 int __connman_service_indicate_state(struct connman_service *service,
2330                                         enum connman_service_state state)
2331 {
2332         GSequenceIter *iter;
2333
2334         DBG("service %p state %d", service, state);
2335
2336         if (service == NULL)
2337                 return -EINVAL;
2338
2339         if (service->state == state)
2340                 return -EALREADY;
2341
2342         if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
2343                                 state == CONNMAN_SERVICE_STATE_IDLE)
2344                 return -EINVAL;
2345
2346         if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
2347                                 state == CONNMAN_SERVICE_STATE_DISCONNECT)
2348                 return -EINVAL;
2349
2350         if (state == CONNMAN_SERVICE_STATE_IDLE &&
2351                         service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
2352                 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
2353                 state_changed(service);
2354
2355                 reply_pending(service, ECONNABORTED);
2356
2357                 __connman_service_disconnect(service);
2358         }
2359
2360         if (state == CONNMAN_SERVICE_STATE_CONFIGURATION)
2361                 __connman_ipconfig_enable(service->ipconfig);
2362
2363         service->state = state;
2364         state_changed(service);
2365
2366         if (state == CONNMAN_SERVICE_STATE_ONLINE) {
2367                 if (service->login_required == TRUE) {
2368                         service->login_required = FALSE;
2369                         login_changed(service);
2370                 }
2371
2372                 connman_timeserver_sync();
2373         }
2374
2375         if (state == CONNMAN_SERVICE_STATE_IDLE) {
2376                 connman_bool_t reconnect;
2377
2378                 reconnect = get_reconnect_state(service);
2379                 if (reconnect == TRUE)
2380                         __connman_service_auto_connect();
2381         }
2382
2383         if (state == CONNMAN_SERVICE_STATE_READY) {
2384                 set_reconnect_state(service, TRUE);
2385
2386                 __connman_service_set_favorite(service, TRUE);
2387
2388                 reply_pending(service, 0);
2389
2390                 service->userconnect = FALSE;
2391
2392                 g_get_current_time(&service->modified);
2393                 __connman_storage_save_service(service);
2394
2395                 update_nameservers(service);
2396                 dns_changed(service);
2397
2398                 __connman_wpad_start(service);
2399
2400                 __connman_notifier_connect(service->type);
2401
2402                 default_changed();
2403         } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
2404                 __connman_location_finish(service);
2405
2406                 default_changed();
2407
2408                 __connman_wpad_stop(service);
2409
2410                 update_nameservers(service);
2411                 dns_changed(service);
2412
2413                 __connman_notifier_disconnect(service->type);
2414         }
2415
2416         if (state == CONNMAN_SERVICE_STATE_FAILURE) {
2417                 reply_pending(service, EIO);
2418
2419                 if (service->userconnect == FALSE)
2420                         __connman_service_auto_connect();
2421
2422                 g_get_current_time(&service->modified);
2423                 __connman_storage_save_service(service);
2424         } else
2425                 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2426
2427         iter = g_hash_table_lookup(service_hash, service->identifier);
2428         if (iter != NULL)
2429                 g_sequence_sort_changed(iter, service_compare, NULL);
2430
2431         __connman_profile_changed(FALSE);
2432
2433         if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
2434                         service->state == CONNMAN_SERVICE_STATE_FAILURE)
2435                 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
2436
2437         return 0;
2438 }
2439
2440 int __connman_service_indicate_error(struct connman_service *service,
2441                                         enum connman_service_error error)
2442 {
2443         DBG("service %p error %d", service, error);
2444
2445         if (service == NULL)
2446                 return -EINVAL;
2447
2448         service->error = error;
2449
2450         return __connman_service_indicate_state(service,
2451                                         CONNMAN_SERVICE_STATE_FAILURE);
2452 }
2453
2454 int __connman_service_indicate_default(struct connman_service *service)
2455 {
2456         DBG("service %p", service);
2457
2458         default_changed();
2459
2460         __connman_location_detect(service);
2461
2462         return 0;
2463 }
2464
2465 int __connman_service_request_login(struct connman_service *service)
2466 {
2467         DBG("service %p", service);
2468
2469         if (service == NULL)
2470                 return -EINVAL;
2471
2472         service->login_required = TRUE;
2473         login_changed(service);
2474
2475         return 0;
2476 }
2477
2478 static connman_bool_t prepare_network(struct connman_service *service)
2479 {
2480         enum connman_network_type type;
2481         unsigned int ssid_len;
2482
2483         type = connman_network_get_type(service->network);
2484
2485         switch (type) {
2486         case CONNMAN_NETWORK_TYPE_UNKNOWN:
2487         case CONNMAN_NETWORK_TYPE_VENDOR:
2488                 return FALSE;
2489         case CONNMAN_NETWORK_TYPE_WIFI:
2490                 if (connman_network_get_blob(service->network, "WiFi.SSID",
2491                                                         &ssid_len) == NULL)
2492                         return FALSE;
2493
2494                 connman_network_set_string(service->network,
2495                                 "WiFi.Passphrase", service->passphrase);
2496                 break;
2497         case CONNMAN_NETWORK_TYPE_ETHERNET:
2498         case CONNMAN_NETWORK_TYPE_WIMAX:
2499         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
2500         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
2501                 break;
2502         case CONNMAN_NETWORK_TYPE_CELLULAR:
2503                 connman_network_set_string(service->network,
2504                                                 "Cellular.APN", service->apn);
2505
2506                 connman_network_set_string(service->network,
2507                                         "Cellular.Username", service->username);
2508                 connman_network_set_string(service->network,
2509                                         "Cellular.Password", service->password);
2510                 break;
2511         }
2512
2513         return TRUE;
2514 }
2515
2516 static void prepare_8021x(struct connman_service *service)
2517 {
2518         if (service->eap != NULL)
2519                 connman_network_set_string(service->network, "WiFi.EAP",
2520                                                                 service->eap);
2521
2522         if (service->identity != NULL)
2523                 connman_network_set_string(service->network, "WiFi.Identity",
2524                                                         service->identity);
2525
2526         if (service->ca_cert_file != NULL)
2527                 connman_network_set_string(service->network, "WiFi.CACertFile",
2528                                                         service->ca_cert_file);
2529
2530         if (service->client_cert_file != NULL)
2531                 connman_network_set_string(service->network,
2532                                                 "WiFi.ClientCertFile",
2533                                                 service->client_cert_file);
2534
2535         if (service->private_key_file != NULL)
2536                 connman_network_set_string(service->network,
2537                                                 "WiFi.PrivateKeyFile",
2538                                                 service->private_key_file);
2539
2540         if (service->private_key_passphrase != NULL)
2541                 connman_network_set_string(service->network,
2542                                         "WiFi.PrivateKeyPassphrase",
2543                                         service->private_key_passphrase);
2544
2545         if (service->phase2 != NULL)
2546                 connman_network_set_string(service->network, "WiFi.Phase2",
2547                                                         service->phase2);
2548 }
2549
2550 int __connman_service_connect(struct connman_service *service)
2551 {
2552         int err;
2553
2554         DBG("service %p", service);
2555
2556         if (is_connected(service) == TRUE)
2557                 return -EISCONN;
2558
2559         if (is_connecting(service) == TRUE)
2560                 return -EALREADY;
2561
2562         switch (service->type) {
2563         case CONNMAN_SERVICE_TYPE_UNKNOWN:
2564         case CONNMAN_SERVICE_TYPE_SYSTEM:
2565         case CONNMAN_SERVICE_TYPE_GPS:
2566                 return -EINVAL;
2567         case CONNMAN_SERVICE_TYPE_ETHERNET:
2568         case CONNMAN_SERVICE_TYPE_WIMAX:
2569         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2570         case CONNMAN_SERVICE_TYPE_VPN:
2571                 break;
2572         case CONNMAN_SERVICE_TYPE_CELLULAR:
2573                 if (service->apn == NULL)
2574                         return -EINVAL;
2575                 break;
2576         case CONNMAN_SERVICE_TYPE_WIFI:
2577                 switch (service->security) {
2578                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2579                 case CONNMAN_SERVICE_SECURITY_NONE:
2580                         break;
2581                 case CONNMAN_SERVICE_SECURITY_WEP:
2582                 case CONNMAN_SERVICE_SECURITY_PSK:
2583                 case CONNMAN_SERVICE_SECURITY_WPA:
2584                 case CONNMAN_SERVICE_SECURITY_RSN:
2585                         if (service->passphrase == NULL)
2586                                 return -ENOKEY;
2587                         break;
2588                 case CONNMAN_SERVICE_SECURITY_8021X:
2589                         break;
2590                 }
2591                 break;
2592         }
2593
2594         if (service->network != NULL) {
2595                 if (prepare_network(service) == FALSE)
2596                         return -EINVAL;
2597
2598                 switch (service->security) {
2599                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2600                 case CONNMAN_SERVICE_SECURITY_NONE:
2601                 case CONNMAN_SERVICE_SECURITY_WEP:
2602                 case CONNMAN_SERVICE_SECURITY_PSK:
2603                 case CONNMAN_SERVICE_SECURITY_WPA:
2604                 case CONNMAN_SERVICE_SECURITY_RSN:
2605                         break;
2606                 case CONNMAN_SERVICE_SECURITY_8021X:
2607                         prepare_8021x(service);
2608                         break;
2609                 }
2610
2611                 __connman_ipconfig_enable(service->ipconfig);
2612
2613                 err = __connman_network_connect(service->network);
2614         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
2615                                         service->provider != NULL)
2616                 err = __connman_provider_connect(service->provider);
2617         else
2618                 return -EOPNOTSUPP;
2619
2620         if (err < 0) {
2621                 if (err != -EINPROGRESS) {
2622                         __connman_ipconfig_disable(service->ipconfig);
2623                         return err;
2624                 }
2625
2626                 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
2627                                                 connect_timeout, service);
2628
2629                 return -EINPROGRESS;
2630         }
2631
2632         return 0;
2633 }
2634
2635 int __connman_service_disconnect(struct connman_service *service)
2636 {
2637         struct connman_ipconfig *ipv6config;
2638         int err;
2639
2640         DBG("service %p", service);
2641
2642         if (service->network != NULL) {
2643                 err = __connman_network_disconnect(service->network);
2644         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
2645                                         service->provider != NULL)
2646                 err = __connman_provider_disconnect(service->provider);
2647         else
2648                 return -EOPNOTSUPP;
2649
2650         __connman_ipconfig_clear_address(service->ipconfig);
2651
2652         ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
2653
2654         __connman_ipconfig_clear_address(ipv6config);
2655
2656         __connman_ipconfig_disable(service->ipconfig);
2657
2658         if (err < 0) {
2659                 if (err != -EINPROGRESS)
2660                         return err;
2661
2662                 return -EINPROGRESS;
2663         }
2664
2665         return 0;
2666 }
2667
2668 /**
2669  * __connman_service_lookup:
2670  * @pattern: search pattern
2671  * @path: return object path
2672  *
2673  * Look up a service path from a search pattern
2674  */
2675 int __connman_service_lookup(const char *pattern, const char **path)
2676 {
2677         GHashTableIter iter;
2678         gpointer key, value;
2679
2680         g_hash_table_iter_init(&iter, service_hash);
2681
2682         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
2683                 GSequenceIter *iter = value;
2684                 struct connman_service *service = g_sequence_get(iter);
2685
2686                 if (g_strcmp0(service->identifier, pattern) == 0 ||
2687                                 g_strcmp0(service->name, pattern) == 0) {
2688                         *path = (const char *) service->path;
2689                         return 0;
2690                 }
2691         }
2692
2693         return -ENXIO;
2694 }
2695
2696 /**
2697  * lookup_by_identifier:
2698  * @identifier: service identifier
2699  *
2700  * Look up a service by identifier (reference count will not be increased)
2701  */
2702 static struct connman_service *lookup_by_identifier(const char *identifier)
2703 {
2704         GSequenceIter *iter;
2705
2706         iter = g_hash_table_lookup(service_hash, identifier);
2707         if (iter != NULL)
2708                 return g_sequence_get(iter);
2709
2710         return NULL;
2711 }
2712
2713 static struct connman_network *create_hidden_wifi(struct connman_device *device,
2714                 const char *ssid, const char *mode, const char *security)
2715 {
2716         struct connman_network *network;
2717         char *name;
2718         int index;
2719         unsigned int i, ssid_len;
2720
2721         ssid_len = strlen(ssid);
2722         if (ssid_len < 1)
2723                 return NULL;
2724
2725         network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
2726         if (network == NULL)
2727                 return NULL;
2728
2729         connman_network_set_blob(network, "WiFi.SSID",
2730                                         (unsigned char *) ssid, ssid_len);
2731
2732         connman_network_set_string(network, "WiFi.Mode", mode);
2733         connman_network_set_string(network, "WiFi.Security", security);
2734
2735         name = g_try_malloc0(ssid_len + 1);
2736         if (name == NULL) {
2737                 connman_network_unref(network);
2738                 return NULL;
2739         }
2740
2741         for (i = 0; i < ssid_len; i++) {
2742                 if (g_ascii_isprint(ssid[i]))
2743                         name[i] = ssid[i];
2744                 else
2745                         name[i] = ' ';
2746         }
2747
2748         connman_network_set_name(network, name);
2749
2750         g_free(name);
2751
2752         index = connman_device_get_index(device);
2753         connman_network_set_index(network, index);
2754
2755         connman_network_set_protocol(network, CONNMAN_NETWORK_PROTOCOL_IP);
2756
2757         if (connman_device_add_network(device, network) < 0) {
2758                 connman_network_unref(network);
2759                 return NULL;
2760         }
2761
2762         connman_network_set_available(network, TRUE);
2763
2764         return network;
2765 }
2766
2767 int __connman_service_create_and_connect(DBusMessage *msg)
2768 {
2769         struct connman_service *service;
2770         struct connman_network *network;
2771         struct connman_device *device;
2772         DBusMessageIter iter, array;
2773         const char *mode = "managed", *security = "none", *group_security;
2774         const char *type = NULL, *ssid = NULL, *passphrase = NULL;
2775         unsigned int ssid_len = 0;
2776         const char *ident;
2777         char *name, *group;
2778         gboolean created = FALSE;
2779         int err;
2780
2781         dbus_message_iter_init(msg, &iter);
2782         dbus_message_iter_recurse(&iter, &array);
2783
2784         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
2785                 DBusMessageIter entry, value;
2786                 const char *key;
2787
2788                 dbus_message_iter_recurse(&array, &entry);
2789                 dbus_message_iter_get_basic(&entry, &key);
2790
2791                 dbus_message_iter_next(&entry);
2792                 dbus_message_iter_recurse(&entry, &value);
2793
2794                 switch (dbus_message_iter_get_arg_type(&value)) {
2795                 case DBUS_TYPE_STRING:
2796                         if (g_str_equal(key, "Type") == TRUE)
2797                                 dbus_message_iter_get_basic(&value, &type);
2798                         else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
2799                                         g_str_equal(key, "Mode") == TRUE)
2800                                 dbus_message_iter_get_basic(&value, &mode);
2801                         else if (g_str_equal(key, "WiFi.Security") == TRUE ||
2802                                         g_str_equal(key, "Security") == TRUE)
2803                                 dbus_message_iter_get_basic(&value, &security);
2804                         else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
2805                                         g_str_equal(key, "Passphrase") == TRUE)
2806                                 dbus_message_iter_get_basic(&value, &passphrase);
2807                         else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
2808                                         g_str_equal(key, "SSID") == TRUE)
2809                                 dbus_message_iter_get_basic(&value, &ssid);
2810                 }
2811
2812                 dbus_message_iter_next(&array);
2813         }
2814
2815         if (type == NULL)
2816                 return -EINVAL;
2817
2818         if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
2819                 return -EOPNOTSUPP;
2820
2821         if (ssid == NULL)
2822                 return -EINVAL;
2823
2824         ssid_len = strlen(ssid);
2825         if (ssid_len < 1)
2826                 return -EINVAL;
2827
2828         if (g_strcmp0(security, "none") != 0 &&
2829                                 g_strcmp0(security, "wep") != 0 &&
2830                                 g_strcmp0(security, "psk") != 0 &&
2831                                 g_strcmp0(security, "wpa") != 0 &&
2832                                 g_strcmp0(security, "rsn") != 0 &&
2833                                 g_strcmp0(security, "ieee8021x") != 0)
2834                 return -EINVAL;
2835
2836         device = __connman_element_find_device(CONNMAN_DEVICE_TYPE_WIFI);
2837         if (device == NULL)
2838                 return -EOPNOTSUPP;
2839
2840         ident = __connman_device_get_ident(device);
2841         if (ident == NULL)
2842                 return -EOPNOTSUPP;
2843
2844
2845         if (!g_strcmp0(security, "wpa") ||
2846                 !g_strcmp0(security, "rsn"))
2847                 group_security = "psk";
2848         else
2849                 group_security = security;
2850
2851         group = connman_wifi_build_group_name((unsigned char *) ssid,
2852                                                 ssid_len, mode, group_security);
2853         if (group == NULL)
2854                 return -EINVAL;
2855
2856         name = g_strdup_printf("%s_%s_%s", type, ident, group);
2857
2858         service = lookup_by_identifier(name);
2859
2860         if (service != NULL)
2861                 goto done;
2862
2863         network = create_hidden_wifi(device, ssid, mode, security);
2864         if (network != NULL) {
2865                 connman_network_set_group(network, group);
2866                 created = TRUE;
2867         }
2868
2869         service = lookup_by_identifier(name);
2870
2871 done:
2872         g_free(name);
2873         g_free(group);
2874
2875         if (service == NULL) {
2876                 err = -EOPNOTSUPP;
2877                 goto failed;
2878         }
2879
2880         set_reconnect_state(service, FALSE);
2881
2882         __connman_device_disconnect(device);
2883
2884         if (passphrase != NULL) {
2885                 g_free(service->passphrase);
2886                 service->passphrase = g_strdup(passphrase);
2887         }
2888
2889         service->userconnect = TRUE;
2890
2891         err = __connman_service_connect(service);
2892         if (err < 0 && err != -EINPROGRESS)
2893                 goto failed;
2894
2895         g_dbus_send_reply(connection, msg,
2896                                 DBUS_TYPE_OBJECT_PATH, &service->path,
2897                                                         DBUS_TYPE_INVALID);
2898
2899         return 0;
2900
2901 failed:
2902         if (service != NULL && created == TRUE) {
2903                 struct connman_network *network = service->network;
2904
2905                 if (network != NULL) {
2906                         connman_network_set_available(network, FALSE);
2907                         __connman_device_cleanup_networks(device);
2908                 } else
2909                         __connman_service_put(service);
2910         }
2911
2912         return err;
2913 }
2914
2915 /**
2916  * __connman_service_get:
2917  * @identifier: service identifier
2918  *
2919  * Look up a service by identifier or create a new one if not found
2920  */
2921 static struct connman_service *__connman_service_get(const char *identifier)
2922 {
2923         struct connman_service *service;
2924         GSequenceIter *iter;
2925
2926         iter = g_hash_table_lookup(service_hash, identifier);
2927         if (iter != NULL) {
2928                 service = g_sequence_get(iter);
2929                 if (service != NULL)
2930                         g_atomic_int_inc(&service->refcount);
2931                 return service;
2932         }
2933
2934         service = connman_service_create();
2935         if (service == NULL)
2936                 return NULL;
2937
2938         DBG("service %p", service);
2939
2940         service->identifier = g_strdup(identifier);
2941
2942         service->profile = g_strdup(__connman_profile_active_ident());
2943
2944         iter = g_sequence_insert_sorted(service_list, service,
2945                                                 service_compare, NULL);
2946
2947         g_hash_table_insert(service_hash, service->identifier, iter);
2948
2949         return service;
2950 }
2951
2952 static int service_register(struct connman_service *service)
2953 {
2954         const char *path = __connman_profile_active_path();
2955         GSequenceIter *iter;
2956
2957         DBG("service %p", service);
2958
2959         if (service->path != NULL)
2960                 return -EALREADY;
2961
2962         service->path = g_strdup_printf("%s/%s", path, service->identifier);
2963
2964         DBG("path %s", service->path);
2965
2966         __connman_config_provision_service(service);
2967
2968         __connman_storage_load_service(service);
2969
2970         g_dbus_register_interface(connection, service->path,
2971                                         CONNMAN_SERVICE_INTERFACE,
2972                                         service_methods, service_signals,
2973                                                         NULL, service, NULL);
2974
2975         iter = g_hash_table_lookup(service_hash, service->identifier);
2976         if (iter != NULL)
2977                 g_sequence_sort_changed(iter, service_compare, NULL);
2978
2979         __connman_profile_changed(TRUE);
2980
2981         return 0;
2982 }
2983
2984 static void service_up(struct connman_ipconfig *ipconfig)
2985 {
2986         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
2987
2988         connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
2989
2990         service->stats.valid = FALSE;
2991 }
2992
2993 static void service_down(struct connman_ipconfig *ipconfig)
2994 {
2995         connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
2996 }
2997
2998 static void service_lower_up(struct connman_ipconfig *ipconfig)
2999 {
3000         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3001
3002         connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
3003
3004         __connman_service_stats_start(service);
3005 }
3006
3007 static void service_lower_down(struct connman_ipconfig *ipconfig)
3008 {
3009         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3010
3011         connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
3012
3013         __connman_service_stats_stop(service);
3014         __connman_storage_save_service(service);
3015 }
3016
3017 static void service_ip_bound(struct connman_ipconfig *ipconfig)
3018 {
3019         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3020
3021         connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
3022
3023         settings_changed(service);
3024 }
3025
3026 static void service_ip_release(struct connman_ipconfig *ipconfig)
3027 {
3028         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3029
3030         connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
3031
3032         settings_changed(service);
3033 }
3034
3035 static const struct connman_ipconfig_ops service_ops = {
3036         .up             = service_up,
3037         .down           = service_down,
3038         .lower_up       = service_lower_up,
3039         .lower_down     = service_lower_down,
3040         .ip_bound       = service_ip_bound,
3041         .ip_release     = service_ip_release,
3042 };
3043
3044 static void setup_ipconfig(struct connman_service *service, int index)
3045 {
3046         if (index < 0)
3047                 return;
3048
3049         service->ipconfig = connman_ipconfig_create(index);
3050         if (service->ipconfig == NULL)
3051                 return;
3052
3053         connman_ipconfig_set_method(service->ipconfig,
3054                                         CONNMAN_IPCONFIG_METHOD_DHCP);
3055
3056         connman_ipconfig_set_data(service->ipconfig, service);
3057
3058         connman_ipconfig_set_ops(service->ipconfig, &service_ops);
3059 }
3060
3061 void __connman_service_create_ipconfig(struct connman_service *service,
3062                                                                 int index)
3063 {
3064         struct connman_ipconfig *ipv6config;
3065         const char *ident = service->profile;
3066         GKeyFile *keyfile;
3067
3068         if (service->ipconfig != NULL)
3069                 return;
3070
3071         setup_ipconfig(service, index);
3072
3073         if (ident == NULL)
3074                 return;
3075
3076         keyfile = __connman_storage_open_profile(ident);
3077         if (keyfile == NULL)
3078                 return;
3079
3080
3081         ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
3082         if (ipv6config != NULL)
3083                 __connman_ipconfig_load(ipv6config, keyfile,
3084                                         service->identifier, "IPv6.");
3085
3086         __connman_ipconfig_load(service->ipconfig, keyfile,
3087                                         service->identifier, "IPv4.");
3088         g_key_file_free(keyfile);
3089 }
3090
3091 /**
3092  * __connman_service_lookup_from_network:
3093  * @network: network structure
3094  *
3095  * Look up a service by network (reference count will not be increased)
3096  */
3097 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
3098 {
3099         struct connman_service *service;
3100         const char *ident, *group;
3101         char *name;
3102
3103         DBG("network %p", network);
3104
3105         ident = __connman_network_get_ident(network);
3106         if (ident == NULL)
3107                 return NULL;
3108
3109         group = connman_network_get_group(network);
3110         if (group == NULL)
3111                 return NULL;
3112
3113         name = g_strdup_printf("%s_%s_%s",
3114                         __connman_network_get_type(network), ident, group);
3115         service = lookup_by_identifier(name);
3116         g_free(name);
3117
3118         return service;
3119 }
3120
3121 const char *__connman_service_get_path(struct connman_service *service)
3122 {
3123         return service->path;
3124 }
3125
3126 unsigned int __connman_service_get_order(struct connman_service *service)
3127 {
3128         GSequenceIter *iter;
3129
3130         if (service == NULL)
3131                 return 0;
3132
3133         if (service->favorite == FALSE) {
3134                 service->order = 0;
3135                 goto done;
3136         }
3137
3138         iter = g_hash_table_lookup(service_hash, service->identifier);
3139         if (iter != NULL) {
3140                 if (g_sequence_iter_get_position(iter) == 0)
3141                         service->order = 1;
3142                 else
3143                         service->order = 0;
3144         }
3145
3146 done:
3147         return service->order;
3148 }
3149
3150 static enum connman_service_type convert_network_type(struct connman_network *network)
3151 {
3152         enum connman_network_type type = connman_network_get_type(network);
3153
3154         switch (type) {
3155         case CONNMAN_NETWORK_TYPE_UNKNOWN:
3156         case CONNMAN_NETWORK_TYPE_VENDOR:
3157                 break;
3158         case CONNMAN_NETWORK_TYPE_ETHERNET:
3159                 return CONNMAN_SERVICE_TYPE_ETHERNET;
3160         case CONNMAN_NETWORK_TYPE_WIFI:
3161                 return CONNMAN_SERVICE_TYPE_WIFI;
3162         case CONNMAN_NETWORK_TYPE_WIMAX:
3163                 return CONNMAN_SERVICE_TYPE_WIMAX;
3164         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3165         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3166                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
3167         case CONNMAN_NETWORK_TYPE_CELLULAR:
3168                 return CONNMAN_SERVICE_TYPE_CELLULAR;
3169         }
3170
3171         return CONNMAN_SERVICE_TYPE_UNKNOWN;
3172 }
3173
3174 static enum connman_service_mode convert_wifi_mode(const char *mode)
3175 {
3176         if (mode == NULL)
3177                 return CONNMAN_SERVICE_MODE_UNKNOWN;
3178         else if (g_str_equal(mode, "managed") == TRUE)
3179                 return CONNMAN_SERVICE_MODE_MANAGED;
3180         else if (g_str_equal(mode, "adhoc") == TRUE)
3181                 return CONNMAN_SERVICE_MODE_ADHOC;
3182         else
3183                 return CONNMAN_SERVICE_MODE_UNKNOWN;
3184 }
3185
3186 static enum connman_service_security convert_wifi_security(const char *security)
3187 {
3188         if (security == NULL)
3189                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3190         else if (g_str_equal(security, "none") == TRUE)
3191                 return CONNMAN_SERVICE_SECURITY_NONE;
3192         else if (g_str_equal(security, "wep") == TRUE)
3193                 return CONNMAN_SERVICE_SECURITY_WEP;
3194         else if (g_str_equal(security, "psk") == TRUE)
3195                 return CONNMAN_SERVICE_SECURITY_PSK;
3196         else if (g_str_equal(security, "ieee8021x") == TRUE)
3197                 return CONNMAN_SERVICE_SECURITY_8021X;
3198         else if (g_str_equal(security, "wpa") == TRUE)
3199                 return CONNMAN_SERVICE_SECURITY_WPA;
3200         else if (g_str_equal(security, "rsn") == TRUE)
3201                 return CONNMAN_SERVICE_SECURITY_RSN;
3202         else
3203                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3204 }
3205
3206 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
3207 {
3208         switch (mode) {
3209         case 0:
3210         case 1:
3211                 return CONNMAN_SERVICE_MODE_GPRS;
3212         case 3:
3213                 return CONNMAN_SERVICE_MODE_EDGE;
3214         case 2:
3215         case 4:
3216         case 5:
3217         case 6:
3218                 return CONNMAN_SERVICE_MODE_UMTS;
3219         }
3220
3221         return CONNMAN_SERVICE_MODE_UNKNOWN;
3222 }
3223
3224 static void update_from_network(struct connman_service *service,
3225                                         struct connman_network *network)
3226 {
3227         connman_uint8_t strength = service->strength;
3228         GSequenceIter *iter;
3229         const char *str;
3230
3231         DBG("service %p network %p", service, network);
3232
3233         if (is_connected(service) == TRUE)
3234                 return;
3235
3236         if (is_connecting(service) == TRUE)
3237                 return;
3238
3239         str = connman_network_get_string(network, "Name");
3240         if (str != NULL) {
3241                 g_free(service->name);
3242                 service->name = g_strdup(str);
3243                 service->hidden = FALSE;
3244         } else {
3245                 g_free(service->name);
3246                 service->name = NULL;
3247                 service->hidden = TRUE;
3248         }
3249
3250         service->strength = connman_network_get_uint8(network, "Strength");
3251         service->roaming = connman_network_get_bool(network, "Roaming");
3252
3253         if (service->strength == 0) {
3254                 /*
3255                  * Filter out 0-values; it's unclear what they mean
3256                  * and they cause anomalous sorting of the priority list.
3257                  */
3258                 service->strength = strength;
3259         }
3260
3261         str = connman_network_get_string(network, "WiFi.Mode");
3262         service->mode = convert_wifi_mode(str);
3263
3264         str = connman_network_get_string(network, "WiFi.Security");
3265         service->security = convert_wifi_security(str);
3266
3267         str = connman_network_get_string(network, "Cellular.MCC");
3268         g_free(service->mcc);
3269         service->mcc = g_strdup(str);
3270
3271         str = connman_network_get_string(network, "Cellular.MNC");
3272         g_free(service->mnc);
3273         service->mnc = g_strdup(str);
3274
3275         if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
3276                 connman_uint8_t value = connman_network_get_uint8(network,
3277                                                         "Cellular.Mode");
3278
3279                 service->mode = convert_cellular_mode(value);
3280         }
3281
3282         if (service->strength > strength && service->network != NULL) {
3283                 connman_network_unref(service->network);
3284                 service->network = connman_network_ref(network);
3285
3286                 strength_changed(service);
3287         }
3288
3289         if (service->network == NULL)
3290                 service->network = connman_network_ref(network);
3291
3292         iter = g_hash_table_lookup(service_hash, service->identifier);
3293         if (iter != NULL)
3294                 g_sequence_sort_changed(iter, service_compare, NULL);
3295 }
3296
3297 /**
3298  * __connman_service_create_from_network:
3299  * @network: network structure
3300  *
3301  * Look up service by network and if not found, create one
3302  */
3303 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
3304 {
3305         struct connman_service *service;
3306         const char *ident, *group;
3307         char *name;
3308
3309         DBG("network %p", network);
3310
3311         ident = __connman_network_get_ident(network);
3312         if (ident == NULL)
3313                 return NULL;
3314
3315         group = connman_network_get_group(network);
3316         if (group == NULL)
3317                 return NULL;
3318
3319         name = g_strdup_printf("%s_%s_%s",
3320                         __connman_network_get_type(network), ident, group);
3321         service = __connman_service_get(name);
3322         g_free(name);
3323
3324         if (service == NULL)
3325                 return NULL;
3326
3327         if (__connman_network_get_weakness(network) == TRUE)
3328                 return service;
3329
3330         if (service->path != NULL) {
3331                 update_from_network(service, network);
3332                 __connman_profile_changed(TRUE);
3333                 return service;
3334         }
3335
3336         service->type = convert_network_type(network);
3337
3338         switch (service->type) {
3339         case CONNMAN_SERVICE_TYPE_UNKNOWN:
3340         case CONNMAN_SERVICE_TYPE_SYSTEM:
3341         case CONNMAN_SERVICE_TYPE_ETHERNET:
3342         case CONNMAN_SERVICE_TYPE_WIMAX:
3343         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3344         case CONNMAN_SERVICE_TYPE_GPS:
3345         case CONNMAN_SERVICE_TYPE_VPN:
3346                 service->autoconnect = FALSE;
3347                 break;
3348         case CONNMAN_SERVICE_TYPE_WIFI:
3349         case CONNMAN_SERVICE_TYPE_CELLULAR:
3350                 service->autoconnect = TRUE;
3351                 break;
3352         }
3353
3354         service->state = CONNMAN_SERVICE_STATE_IDLE;
3355
3356         update_from_network(service, network);
3357
3358         setup_ipconfig(service, connman_network_get_index(network));
3359
3360         service_register(service);
3361
3362         if (service->favorite == TRUE)
3363                 __connman_service_auto_connect();
3364
3365         return service;
3366 }
3367
3368 void __connman_service_update_from_network(struct connman_network *network)
3369 {
3370         struct connman_service *service;
3371         enum connman_service_mode mode;
3372         connman_uint8_t strength, value;
3373         connman_bool_t roaming;
3374         GSequenceIter *iter;
3375         const char *name;
3376
3377         DBG("network %p", network);
3378
3379         service = __connman_service_lookup_from_network(network);
3380         if (service == NULL)
3381                 return;
3382
3383         if (service->network == NULL)
3384                 return;
3385
3386         name = connman_network_get_string(service->network, "Name");
3387         if (g_strcmp0(service->name, name) != 0) {
3388                 g_free(service->name);
3389                 service->name = g_strdup(name);
3390                 connman_dbus_property_changed_basic(service->path,
3391                                 CONNMAN_SERVICE_INTERFACE, "Name",
3392                                 DBUS_TYPE_STRING, &service->name);
3393         }
3394
3395         strength = connman_network_get_uint8(service->network, "Strength");
3396         if (strength == service->strength)
3397                 goto roaming;
3398
3399         service->strength = strength;
3400
3401         strength_changed(service);
3402
3403 roaming:
3404         roaming = connman_network_get_bool(service->network, "Roaming");
3405         if (roaming == service->roaming)
3406                 goto done;
3407
3408         service->roaming = roaming;
3409
3410         roaming_changed(service);
3411
3412         iter = g_hash_table_lookup(service_hash, service->identifier);
3413         if (iter != NULL)
3414                 g_sequence_sort_changed(iter, service_compare, NULL);
3415
3416 done:
3417         if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
3418                 return;
3419
3420         value = connman_network_get_uint8(service->network, "Cellular.Mode");
3421         mode = convert_cellular_mode(value);
3422
3423         if (mode == service->mode)
3424                 return;
3425
3426         service->mode = mode;
3427
3428         mode_changed(service);
3429 }
3430
3431 void __connman_service_remove_from_network(struct connman_network *network)
3432 {
3433         struct connman_service *service;
3434
3435         DBG("network %p", network);
3436
3437         service = __connman_service_lookup_from_network(network);
3438         if (service == NULL)
3439                 return;
3440
3441         __connman_service_put(service);
3442 }
3443
3444 /**
3445  * __connman_service_create_from_provider:
3446  * @provider: provider structure
3447  *
3448  * Look up service by provider and if not found, create one
3449  */
3450 struct connman_service *
3451 __connman_service_create_from_provider(struct connman_provider *provider)
3452 {
3453         struct connman_service *service;
3454         const char *ident, *str;
3455         char *name;
3456         int index = connman_provider_get_index(provider);
3457
3458         DBG("provider %p", provider);
3459
3460         ident = __connman_provider_get_ident(provider);
3461         if (ident == NULL)
3462                 return NULL;
3463
3464         name = g_strdup_printf("vpn_%s", ident);
3465         service = __connman_service_get(name);
3466         g_free(name);
3467
3468         if (service == NULL)
3469                 return NULL;
3470
3471         service->type = CONNMAN_SERVICE_TYPE_VPN;
3472         service->provider = provider;
3473         service->autoconnect = FALSE;
3474
3475         service->state = CONNMAN_SERVICE_STATE_IDLE;
3476
3477         str = connman_provider_get_string(provider, "Name");
3478         if (str != NULL) {
3479                 g_free(service->name);
3480                 service->name = g_strdup(str);
3481                 service->hidden = FALSE;
3482         } else {
3483                 g_free(service->name);
3484                 service->name = NULL;
3485                 service->hidden = TRUE;
3486         }
3487
3488         service->strength = 0;
3489
3490         service->ipconfig = connman_ipconfig_create(index);
3491         if (service->ipconfig == NULL)
3492                 return service;
3493
3494         connman_ipconfig_set_method(service->ipconfig,
3495                                         CONNMAN_IPCONFIG_METHOD_MANUAL);
3496
3497         connman_ipconfig_set_data(service->ipconfig, service);
3498
3499         connman_ipconfig_set_ops(service->ipconfig, &service_ops);
3500
3501         service_register(service);
3502
3503         return service;
3504 }
3505
3506 void __connman_service_stats_update(struct connman_service *service,
3507                                 unsigned int rx_packets, unsigned int tx_packets,
3508                                 unsigned int rx_bytes, unsigned int tx_bytes,
3509                                 unsigned int rx_errors, unsigned int tx_errors,
3510                                 unsigned int rx_dropped, unsigned int tx_dropped)
3511 {
3512         unsigned int seconds;
3513         struct connman_stats *stats = &service->stats;
3514
3515         DBG("service %p", service);
3516
3517         if (is_connected(service) == FALSE)
3518                 return;
3519
3520         if (stats->valid == TRUE) {
3521                 stats->rx_packets +=
3522                         rx_packets - stats->rx_packets_last;
3523                 stats->tx_packets +=
3524                         tx_packets - stats->tx_packets_last;
3525                 stats->rx_bytes +=
3526                         rx_bytes - stats->rx_bytes_last;
3527                 stats->tx_bytes +=
3528                         tx_bytes - stats->tx_bytes_last;
3529                 stats->rx_errors +=
3530                         rx_errors - stats->rx_errors_last;
3531                 stats->tx_errors +=
3532                         tx_errors - stats->tx_errors_last;
3533                 stats->rx_dropped +=
3534                         rx_dropped - stats->rx_dropped_last;
3535                 stats->tx_dropped +=
3536                         tx_dropped - stats->tx_dropped_last;
3537         } else {
3538                 stats->valid = TRUE;
3539         }
3540
3541         stats->rx_packets_last = rx_packets;
3542         stats->tx_packets_last = tx_packets;
3543         stats->rx_bytes_last = rx_bytes;
3544         stats->tx_bytes_last = tx_bytes;
3545         stats->rx_errors_last = rx_errors;
3546         stats->tx_errors_last = tx_errors;
3547         stats->rx_dropped_last = rx_dropped;
3548         stats->tx_dropped_last = tx_dropped;
3549
3550         seconds = g_timer_elapsed(stats->timer, NULL);
3551         stats->time = stats->time_start + seconds;
3552 }
3553
3554 static int service_load(struct connman_service *service)
3555 {
3556         const char *ident = service->profile;
3557         GKeyFile *keyfile;
3558         GError *error = NULL;
3559         gchar *pathname, *data = NULL;
3560         gsize length;
3561         gchar *str;
3562         connman_bool_t autoconnect;
3563         unsigned int ssid_len;
3564         int err = 0;
3565
3566         DBG("service %p", service);
3567
3568         if (ident == NULL)
3569                 return -EINVAL;
3570
3571         pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
3572         if (pathname == NULL)
3573                 return -ENOMEM;
3574
3575         keyfile = g_key_file_new();
3576
3577         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
3578                 g_free(pathname);
3579                 return -ENOENT;
3580         }
3581
3582         g_free(pathname);
3583
3584         if (g_key_file_load_from_data(keyfile, data, length,
3585                                                         0, NULL) == FALSE) {
3586                 g_free(data);
3587                 return -EILSEQ;
3588         }
3589
3590         g_free(data);
3591
3592         switch (service->type) {
3593         case CONNMAN_SERVICE_TYPE_UNKNOWN:
3594         case CONNMAN_SERVICE_TYPE_SYSTEM:
3595         case CONNMAN_SERVICE_TYPE_ETHERNET:
3596         case CONNMAN_SERVICE_TYPE_GPS:
3597         case CONNMAN_SERVICE_TYPE_VPN:
3598                 break;
3599         case CONNMAN_SERVICE_TYPE_WIFI:
3600                 if (service->name == NULL) {
3601                         gchar *name;
3602
3603                         name = g_key_file_get_string(keyfile,
3604                                         service->identifier, "Name", NULL);
3605                         if (name != NULL) {
3606                                 g_free(service->name);
3607                                 service->name = name;
3608                         }
3609
3610                         if (service->network != NULL)
3611                                 connman_network_set_name(service->network,
3612                                                                         name);
3613                 }
3614
3615                 if (service->network &&
3616                                 connman_network_get_blob(service->network,
3617                                         "WiFi.SSID", &ssid_len) == NULL) {
3618                         gchar *hex_ssid;
3619
3620                         hex_ssid = g_key_file_get_string(keyfile,
3621                                                         service->identifier,
3622                                                                 "SSID", NULL);
3623
3624                         if (hex_ssid != NULL) {
3625                                 gchar *ssid;
3626                                 unsigned int i, j = 0, hex;
3627                                 size_t hex_ssid_len = strlen(hex_ssid);
3628
3629                                 ssid = g_try_malloc0(hex_ssid_len / 2);
3630                                 if (ssid == NULL) {
3631                                         g_free(hex_ssid);
3632                                         err = -ENOMEM;
3633                                         goto done;
3634                                 }
3635
3636                                 for (i = 0; i < hex_ssid_len; i += 2) {
3637                                         sscanf(hex_ssid + i, "%02x", &hex);
3638                                         ssid[j++] = hex;
3639                                 }
3640
3641                                 connman_network_set_blob(service->network,
3642                                         "WiFi.SSID", ssid, hex_ssid_len / 2);
3643                         }
3644
3645                         g_free(hex_ssid);
3646                 }
3647                 /* fall through */
3648
3649         case CONNMAN_SERVICE_TYPE_WIMAX:
3650         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3651         case CONNMAN_SERVICE_TYPE_CELLULAR:
3652                 service->apn = g_key_file_get_string(keyfile,
3653                                         service->identifier, "APN", NULL);
3654
3655                 service->username = g_key_file_get_string(keyfile,
3656                                         service->identifier, "Username", NULL);
3657
3658                 service->password = g_key_file_get_string(keyfile,
3659                                         service->identifier, "Password", NULL);
3660
3661                 service->favorite = g_key_file_get_boolean(keyfile,
3662                                 service->identifier, "Favorite", NULL);
3663
3664                 autoconnect = g_key_file_get_boolean(keyfile,
3665                                 service->identifier, "AutoConnect", &error);
3666                 if (error == NULL)
3667                         service->autoconnect = autoconnect;
3668                 g_clear_error(&error);
3669
3670                 str = g_key_file_get_string(keyfile,
3671                                 service->identifier, "Failure", NULL);
3672                 if (str != NULL) {
3673                         service->state = CONNMAN_SERVICE_STATE_FAILURE;
3674                         service->error = string2error(str);
3675                 }
3676                 break;
3677         }
3678
3679         str = g_key_file_get_string(keyfile,
3680                                 service->identifier, "Modified", NULL);
3681         if (str != NULL) {
3682                 g_time_val_from_iso8601(str, &service->modified);
3683                 g_free(str);
3684         }
3685
3686         str = g_key_file_get_string(keyfile,
3687                                 service->identifier, "Passphrase", NULL);
3688         if (str != NULL) {
3689                 g_free(service->passphrase);
3690                 service->passphrase = str;
3691         }
3692
3693         if (service->ipconfig != NULL) {
3694                 struct connman_ipconfig *ipv6config;
3695
3696                 ipv6config = connman_ipconfig_get_ipv6config(
3697                                                 service->ipconfig);
3698                 if (ipv6config != NULL)
3699                         __connman_ipconfig_load(ipv6config, keyfile,
3700                                         service->identifier, "IPv6.");
3701
3702                 __connman_ipconfig_load(service->ipconfig, keyfile,
3703                                         service->identifier, "IPv4.");
3704         }
3705
3706         service->nameservers = g_key_file_get_string_list(keyfile,
3707                         service->identifier, "Nameservers", &length, NULL);
3708         if (service->nameservers != NULL && length == 0) {
3709                 g_strfreev(service->nameservers);
3710                 service->nameservers = NULL;
3711         }
3712
3713         service->domains = g_key_file_get_string_list(keyfile,
3714                         service->identifier, "Domains", &length, NULL);
3715         if (service->domains != NULL && length == 0) {
3716                 g_strfreev(service->domains);
3717                 service->domains = NULL;
3718         }
3719
3720         __connman_service_stats_load(service, keyfile, service->identifier);
3721 done:
3722         g_key_file_free(keyfile);
3723
3724         return err;
3725 }
3726
3727 static int service_save(struct connman_service *service)
3728 {
3729         const char *ident = service->profile;
3730         GKeyFile *keyfile;
3731         gchar *pathname, *data = NULL;
3732         gsize length;
3733         gchar *str;
3734         int err = 0;
3735
3736         DBG("service %p", service);
3737
3738         if (ident == NULL)
3739                 return -EINVAL;
3740
3741         pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
3742         if (pathname == NULL)
3743                 return -ENOMEM;
3744
3745         keyfile = g_key_file_new();
3746
3747         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
3748                 goto update;
3749
3750         if (length > 0) {
3751                 if (g_key_file_load_from_data(keyfile, data, length,
3752                                                         0, NULL) == FALSE)
3753                         goto done;
3754         }
3755
3756         g_free(data);
3757
3758 update:
3759         if (service->name != NULL)
3760                 g_key_file_set_string(keyfile, service->identifier,
3761                                                 "Name", service->name);
3762
3763         switch (service->type) {
3764         case CONNMAN_SERVICE_TYPE_UNKNOWN:
3765         case CONNMAN_SERVICE_TYPE_SYSTEM:
3766         case CONNMAN_SERVICE_TYPE_ETHERNET:
3767         case CONNMAN_SERVICE_TYPE_GPS:
3768         case CONNMAN_SERVICE_TYPE_VPN:
3769                 break;
3770         case CONNMAN_SERVICE_TYPE_WIFI:
3771                 if (service->network) {
3772                         const unsigned char *ssid;
3773                         unsigned int ssid_len = 0;
3774
3775                         ssid = connman_network_get_blob(service->network,
3776                                                         "WiFi.SSID", &ssid_len);
3777
3778                         if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
3779                                 char *identifier = service->identifier;
3780                                 GString *str;
3781                                 unsigned int i;
3782
3783                                 str = g_string_sized_new(ssid_len * 2);
3784                                 if (str == NULL) {
3785                                         err = -ENOMEM;
3786                                         goto done;
3787                                 }
3788
3789                                 for (i = 0; i < ssid_len; i++)
3790                                         g_string_append_printf(str,
3791                                                         "%02x", ssid[i]);
3792
3793                                 g_key_file_set_string(keyfile, identifier,
3794                                                         "SSID", str->str);
3795
3796                                 g_string_free(str, TRUE);
3797                         }
3798                 }
3799                 /* fall through */
3800
3801         case CONNMAN_SERVICE_TYPE_WIMAX:
3802         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3803         case CONNMAN_SERVICE_TYPE_CELLULAR:
3804                 if (service->apn != NULL)
3805                         g_key_file_set_string(keyfile, service->identifier,
3806                                                         "APN", service->apn);
3807
3808                 if (service->username != NULL)
3809                         g_key_file_set_string(keyfile, service->identifier,
3810                                                 "Username", service->username);
3811
3812                 if (service->password != NULL)
3813                         g_key_file_set_string(keyfile, service->identifier,
3814                                                 "Password", service->password);
3815
3816                 g_key_file_set_boolean(keyfile, service->identifier,
3817                                         "Favorite", service->favorite);
3818
3819                 if (service->favorite == TRUE)
3820                         g_key_file_set_boolean(keyfile, service->identifier,
3821                                         "AutoConnect", service->autoconnect);
3822
3823                 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
3824                         const char *failure = error2string(service->error);
3825                         if (failure != NULL)
3826                                 g_key_file_set_string(keyfile,
3827                                                         service->identifier,
3828                                                         "Failure", failure);
3829                 } else {
3830                         g_key_file_remove_key(keyfile, service->identifier,
3831                                                         "Failure", NULL);
3832                 }
3833                 break;
3834         }
3835
3836         str = g_time_val_to_iso8601(&service->modified);
3837         if (str != NULL) {
3838                 g_key_file_set_string(keyfile, service->identifier,
3839                                                         "Modified", str);
3840                 g_free(str);
3841         }
3842
3843         if (service->passphrase != NULL && strlen(service->passphrase) > 0)
3844                 g_key_file_set_string(keyfile, service->identifier,
3845                                         "Passphrase", service->passphrase);
3846         else
3847                 g_key_file_remove_key(keyfile, service->identifier,
3848                                                         "Passphrase", NULL);
3849
3850         if (service->ipconfig != NULL) {
3851                 struct connman_ipconfig *ipv6config;
3852
3853                 ipv6config = connman_ipconfig_get_ipv6config(service->ipconfig);
3854                 if (ipv6config != NULL)
3855                         __connman_ipconfig_save(ipv6config, keyfile,
3856                                                 service->identifier, "IPv6.");
3857
3858                 __connman_ipconfig_save(service->ipconfig, keyfile,
3859                                         service->identifier, "IPv4.");
3860         }
3861
3862         if (service->nameservers != NULL) {
3863                 guint len = g_strv_length(service->nameservers);
3864
3865                 g_key_file_set_string_list(keyfile, service->identifier,
3866                                                                 "Nameservers",
3867                                 (const gchar **) service->nameservers, len);
3868         } else
3869                 g_key_file_remove_key(keyfile, service->identifier,
3870                                                         "Nameservers", NULL);
3871
3872         if (service->domains != NULL) {
3873                 guint len = g_strv_length(service->domains);
3874
3875                 g_key_file_set_string_list(keyfile, service->identifier,
3876                                                                 "Domains",
3877                                 (const gchar **) service->domains, len);
3878         } else
3879                 g_key_file_remove_key(keyfile, service->identifier,
3880                                                         "Domains", NULL);
3881
3882         __connman_service_stats_save(service, keyfile, service->identifier);
3883
3884         data = g_key_file_to_data(keyfile, &length, NULL);
3885
3886         if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
3887                 connman_error("Failed to store service information");
3888
3889 done:
3890         g_free(data);
3891
3892         g_key_file_free(keyfile);
3893
3894         g_free(pathname);
3895
3896         return err;
3897 }
3898
3899 static struct connman_storage service_storage = {
3900         .name           = "service",
3901         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
3902         .service_load   = service_load,
3903         .service_save   = service_save,
3904 };
3905
3906 int __connman_service_init(void)
3907 {
3908         DBG("");
3909
3910         connection = connman_dbus_get_connection();
3911
3912         if (connman_storage_register(&service_storage) < 0)
3913                 connman_error("Failed to register service storage");
3914
3915         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
3916                                                                 NULL, NULL);
3917
3918         service_list = g_sequence_new(service_free);
3919
3920         return 0;
3921 }
3922
3923 void __connman_service_cleanup(void)
3924 {
3925         DBG("");
3926
3927         g_sequence_free(service_list);
3928         service_list = NULL;
3929
3930         g_hash_table_destroy(service_hash);
3931         service_hash = NULL;
3932
3933         connman_storage_unregister(&service_storage);
3934
3935         dbus_connection_unref(connection);
3936 }