service: Return connecting status while idle
[framework/connectivity/connman.git] / src / service.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <gdbus.h>
29
30 #include "connman.h"
31
32 #define CONNECT_TIMEOUT         120
33
34 static DBusConnection *connection = NULL;
35
36 static GSequence *service_list = NULL;
37 static GHashTable *service_hash = NULL;
38 static GSList *counter_list = NULL;
39
40 struct connman_stats {
41         connman_bool_t valid;
42         connman_bool_t enabled;
43         struct connman_stats_data data_last;
44         struct connman_stats_data data;
45         GTimer *timer;
46 };
47
48 struct connman_stats_counter {
49         connman_bool_t append_all;
50         struct connman_stats stats;
51         struct connman_stats stats_roaming;
52 };
53
54 struct connman_service {
55         gint refcount;
56         char *identifier;
57         char *path;
58         enum connman_service_type type;
59         enum connman_service_mode mode;
60         enum connman_service_security security;
61         enum connman_service_state state;
62         enum connman_service_error error;
63         connman_uint8_t strength;
64         connman_bool_t favorite;
65         connman_bool_t immutable;
66         connman_bool_t hidden;
67         connman_bool_t ignore;
68         connman_bool_t autoconnect;
69         connman_bool_t userconnect;
70         GTimeVal modified;
71         unsigned int order;
72         char *name;
73         char *passphrase;
74         char *profile;
75         char *apn;
76         char *username;
77         char *password;
78         char *mcc;
79         char *mnc;
80         connman_bool_t roaming;
81         connman_bool_t login_required;
82         struct connman_ipconfig *ipconfig_ipv4;
83         struct connman_ipconfig *ipconfig_ipv6;
84         struct connman_network *network;
85         struct connman_provider *provider;
86         char **nameservers;
87         char *nameserver;
88         char **domains;
89         char *domainname;
90         /* 802.1x settings from the config files */
91         char *eap;
92         char *identity;
93         char *ca_cert_file;
94         char *client_cert_file;
95         char *private_key_file;
96         char *private_key_passphrase;
97         char *phase2;
98         DBusMessage *pending;
99         guint timeout;
100         struct connman_location *location;
101         struct connman_stats stats;
102         struct connman_stats stats_roaming;
103         GHashTable *counter_table;
104         enum connman_service_proxy_method proxy;
105         char **proxies;
106         char **excludes;
107         char *pac;
108 };
109
110 static void append_path(gpointer value, gpointer user_data)
111 {
112         struct connman_service *service = value;
113         DBusMessageIter *iter = user_data;
114
115         if (service->path == NULL || service->hidden == TRUE)
116                 return;
117
118         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
119                                                         &service->path);
120 }
121
122 void __connman_service_list(DBusMessageIter *iter, void *user_data)
123 {
124         g_sequence_foreach(service_list, append_path, iter);
125 }
126
127 struct find_data {
128         const char *path;
129         struct connman_service *service;
130 };
131
132 static void compare_path(gpointer value, gpointer user_data)
133 {
134         struct connman_service *service = value;
135         struct find_data *data = user_data;
136
137         if (data->service != NULL)
138                 return;
139
140         if (g_strcmp0(service->path, data->path) == 0)
141                 data->service = service;
142 }
143
144 static struct connman_service *find_service(const char *path)
145 {
146         struct find_data data = { .path = path, .service = NULL };
147
148         DBG("path %s", path);
149
150         g_sequence_foreach(service_list, compare_path, &data);
151
152         return data.service;
153 }
154
155 const char *__connman_service_type2string(enum connman_service_type type)
156 {
157         switch (type) {
158         case CONNMAN_SERVICE_TYPE_UNKNOWN:
159                 break;
160         case CONNMAN_SERVICE_TYPE_SYSTEM:
161                 return "system";
162         case CONNMAN_SERVICE_TYPE_ETHERNET:
163                 return "ethernet";
164         case CONNMAN_SERVICE_TYPE_WIFI:
165                 return "wifi";
166         case CONNMAN_SERVICE_TYPE_WIMAX:
167                 return "wimax";
168         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
169                 return "bluetooth";
170         case CONNMAN_SERVICE_TYPE_CELLULAR:
171                 return "cellular";
172         case CONNMAN_SERVICE_TYPE_GPS:
173                 return "gps";
174         case CONNMAN_SERVICE_TYPE_VPN:
175                 return "vpn";
176         case CONNMAN_SERVICE_TYPE_GADGET:
177                 return "gadget";
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 const char *proxymethod2string(enum connman_service_proxy_method method)
278 {
279         switch (method) {
280         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
281                 return "direct";
282         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
283                 return "manual";
284         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
285                 return "auto";
286         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
287                 break;
288         }
289
290         return NULL;
291 }
292
293 static enum connman_service_proxy_method string2proxymethod(const char *method)
294 {
295         if (g_strcmp0(method, "direct") == 0)
296                 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
297         else if (g_strcmp0(method, "auto") == 0)
298                 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
299         else if (g_strcmp0(method, "manual") == 0)
300                 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
301         else
302                 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
303 }
304
305 static connman_bool_t is_connecting(struct connman_service *service)
306 {
307         switch (service->state) {
308         case CONNMAN_SERVICE_STATE_UNKNOWN:
309         case CONNMAN_SERVICE_STATE_IDLE:
310         case CONNMAN_SERVICE_STATE_FAILURE:
311                 if (service->network != NULL)
312                         return __connman_network_get_connecting(
313                                                         service->network);
314         case CONNMAN_SERVICE_STATE_DISCONNECT:
315         case CONNMAN_SERVICE_STATE_READY:
316         case CONNMAN_SERVICE_STATE_ONLINE:
317                 break;
318         case CONNMAN_SERVICE_STATE_ASSOCIATION:
319         case CONNMAN_SERVICE_STATE_CONFIGURATION:
320                 return TRUE;
321         }
322
323         return FALSE;
324 }
325
326 static connman_bool_t is_connected(const struct connman_service *service)
327 {
328         switch (service->state) {
329         case CONNMAN_SERVICE_STATE_UNKNOWN:
330         case CONNMAN_SERVICE_STATE_IDLE:
331         case CONNMAN_SERVICE_STATE_ASSOCIATION:
332         case CONNMAN_SERVICE_STATE_CONFIGURATION:
333         case CONNMAN_SERVICE_STATE_DISCONNECT:
334         case CONNMAN_SERVICE_STATE_FAILURE:
335                 break;
336         case CONNMAN_SERVICE_STATE_READY:
337         case CONNMAN_SERVICE_STATE_ONLINE:
338                 return TRUE;
339         }
340
341         return FALSE;
342 }
343
344 static void update_nameservers(struct connman_service *service)
345 {
346         const char *ifname;
347
348         if (service->ipconfig_ipv4)
349                 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
350         else if (service->ipconfig_ipv6)
351                 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
352         else
353                 ifname = NULL;
354
355         if (ifname == NULL)
356                 return;
357
358         switch (service->state) {
359         case CONNMAN_SERVICE_STATE_UNKNOWN:
360         case CONNMAN_SERVICE_STATE_IDLE:
361         case CONNMAN_SERVICE_STATE_ASSOCIATION:
362         case CONNMAN_SERVICE_STATE_CONFIGURATION:
363                 return;
364         case CONNMAN_SERVICE_STATE_FAILURE:
365         case CONNMAN_SERVICE_STATE_DISCONNECT:
366                 connman_resolver_remove_all(ifname);
367                 return;
368         case CONNMAN_SERVICE_STATE_READY:
369         case CONNMAN_SERVICE_STATE_ONLINE:
370                 break;
371         }
372
373         connman_resolver_remove_all(ifname);
374
375         if (service->nameservers != NULL) {
376                 int i;
377
378                 for (i = 0; service->nameservers[i]; i++)
379                         connman_resolver_append(ifname, NULL,
380                                                 service->nameservers[i]);
381         } else if (service->nameserver != NULL)
382                 connman_resolver_append(ifname, NULL, service->nameserver);
383
384         if (service->domains != NULL) {
385                 int i;
386
387                 for (i = 0; service->domains[i]; i++)
388                         connman_resolver_append(ifname, service->domains[i],
389                                                 NULL);
390         } else if (service->domainname != NULL)
391                 connman_resolver_append(ifname, service->domainname, NULL);
392
393         connman_resolver_flush();
394 }
395
396 void __connman_service_append_nameserver(struct connman_service *service,
397                                                 const char *nameserver)
398 {
399         DBG("service %p nameserver %s", service, nameserver);
400
401         if (nameserver == NULL)
402                 return;
403
404         g_free(service->nameserver);
405         service->nameserver = g_strdup(nameserver);
406
407         update_nameservers(service);
408 }
409
410 void __connman_service_remove_nameserver(struct connman_service *service,
411                                                 const char *nameserver)
412 {
413         DBG("service %p nameserver %s", service, nameserver);
414
415         if (nameserver == NULL)
416                 return;
417
418         g_free(service->nameserver);
419         service->nameserver = NULL;
420
421         update_nameservers(service);
422 }
423
424 void __connman_service_nameserver_add_routes(struct connman_service *service,
425                                                 const char *gw)
426 {
427         int index;
428
429         if (service == NULL)
430                 return;
431
432         index = connman_network_get_index(service->network);
433
434         if (service->nameservers != NULL) {
435                 int i;
436
437                 /*
438                  * We add nameservers host routes for nameservers that
439                  * are not on our subnet. For those who are, the subnet
440                  * route will be installed by the time the dns proxy code
441                  * tries to reach them. The subnet route is installed
442                  * when setting the interface IP address.
443                  */
444                 for (i = 0; service->nameservers[i]; i++) {
445                         if (connman_inet_compare_subnet(index,
446                                                         service->nameservers[i]))
447                                 continue;
448
449                         connman_inet_add_host_route(index,
450                                                 service->nameservers[i], gw);
451                 }
452         } else if (service->nameserver != NULL) {
453                 if (connman_inet_compare_subnet(index, service->nameserver))
454                         return;
455
456                 connman_inet_add_host_route(index, service->nameserver, gw);
457         }
458 }
459
460 void __connman_service_nameserver_del_routes(struct connman_service *service)
461 {
462         int index;
463
464         if (service == NULL)
465                 return;
466
467         index = connman_network_get_index(service->network);
468
469         if (service->nameservers != NULL) {
470                 int i;
471
472                 for (i = 0; service->nameservers[i]; i++)
473                         connman_inet_del_host_route(index,
474                                                 service->nameservers[i]);
475         } else if (service->nameserver != NULL) {
476                 connman_inet_del_host_route(index, service->nameserver);
477         }
478 }
479
480 static struct connman_stats *stats_get(struct connman_service *service)
481 {
482         if (service->roaming == TRUE)
483                 return &service->stats_roaming;
484         else
485                 return &service->stats;
486 }
487
488 static connman_bool_t stats_enabled(struct connman_service *service)
489 {
490         struct connman_stats *stats = stats_get(service);
491
492         return stats->enabled;
493 }
494
495 static void stats_start(struct connman_service *service)
496 {
497         struct connman_stats *stats = stats_get(service);
498
499         DBG("service %p", service);
500
501         if (stats->timer == NULL)
502                 return;
503
504         stats->enabled = TRUE;
505         stats->data_last.time = stats->data.time;
506
507         g_timer_start(stats->timer);
508 }
509
510 static void stats_stop(struct connman_service *service)
511 {
512         struct connman_stats *stats = stats_get(service);
513         unsigned int seconds;
514
515         DBG("service %p", service);
516
517         if (stats->timer == NULL)
518                 return;
519
520         if (stats->enabled == FALSE)
521                 return;
522
523         g_timer_stop(stats->timer);
524
525         seconds = g_timer_elapsed(stats->timer, NULL);
526         stats->data.time = stats->data_last.time + seconds;
527
528         stats->enabled = FALSE;
529 }
530
531 static void reset_stats(struct connman_service *service)
532 {
533         DBG("service %p", service);
534
535         /* home */
536         service->stats.valid = FALSE;
537
538         service->stats.data.rx_packets = 0;
539         service->stats.data.tx_packets = 0;
540         service->stats.data.rx_bytes = 0;
541         service->stats.data.tx_bytes = 0;
542         service->stats.data.rx_errors = 0;
543         service->stats.data.tx_errors = 0;
544         service->stats.data.rx_dropped = 0;
545         service->stats.data.tx_dropped = 0;
546         service->stats.data.time = 0;
547         service->stats.data_last.time = 0;
548
549         g_timer_reset(service->stats.timer);
550
551         /* roaming */
552         service->stats_roaming.valid = FALSE;
553
554         service->stats_roaming.data.rx_packets = 0;
555         service->stats_roaming.data.tx_packets = 0;
556         service->stats_roaming.data.rx_bytes = 0;
557         service->stats_roaming.data.tx_bytes = 0;
558         service->stats_roaming.data.rx_errors = 0;
559         service->stats_roaming.data.tx_errors = 0;
560         service->stats_roaming.data.rx_dropped = 0;
561         service->stats_roaming.data.tx_dropped = 0;
562         service->stats_roaming.data.time = 0;
563         service->stats_roaming.data_last.time = 0;
564
565         g_timer_reset(service->stats_roaming.timer);
566 }
567
568 static struct connman_service *get_default(void)
569 {
570         struct connman_service *service;
571         GSequenceIter *iter;
572
573         iter = g_sequence_get_begin_iter(service_list);
574
575         if (g_sequence_iter_is_end(iter) == TRUE)
576                 return NULL;
577
578         service = g_sequence_get(iter);
579
580         if (is_connected(service) == FALSE)
581                 return NULL;
582
583         return service;
584 }
585
586 static void default_changed(void)
587 {
588         struct connman_service *service = get_default();
589
590         __connman_notifier_default_changed(service);
591 }
592
593 const char *__connman_service_default(void)
594 {
595         struct connman_service *service;
596
597         service = get_default();
598         if (service == NULL)
599                 return "";
600
601         return __connman_service_type2string(service->type);
602 }
603
604 static void mode_changed(struct connman_service *service)
605 {
606         const char *str;
607
608         str = mode2string(service->mode);
609         if (str == NULL)
610                 return;
611
612         connman_dbus_property_changed_basic(service->path,
613                                 CONNMAN_SERVICE_INTERFACE, "Mode",
614                                                 DBUS_TYPE_STRING, &str);
615 }
616
617 static void state_changed(struct connman_service *service)
618 {
619         const char *str;
620
621         str = state2string(service->state);
622         if (str == NULL)
623                 return;
624
625         connman_dbus_property_changed_basic(service->path,
626                                 CONNMAN_SERVICE_INTERFACE, "State",
627                                                 DBUS_TYPE_STRING, &str);
628 }
629
630 static void strength_changed(struct connman_service *service)
631 {
632         if (service->strength == 0)
633                 return;
634
635         connman_dbus_property_changed_basic(service->path,
636                                 CONNMAN_SERVICE_INTERFACE, "Strength",
637                                         DBUS_TYPE_BYTE, &service->strength);
638 }
639
640 static void favorite_changed(struct connman_service *service)
641 {
642         if (service->path == NULL)
643                 return;
644
645         connman_dbus_property_changed_basic(service->path,
646                                 CONNMAN_SERVICE_INTERFACE, "Favorite",
647                                         DBUS_TYPE_BOOLEAN, &service->favorite);
648 }
649
650 static void immutable_changed(struct connman_service *service)
651 {
652         if (service->path == NULL)
653                 return;
654
655         connman_dbus_property_changed_basic(service->path,
656                                 CONNMAN_SERVICE_INTERFACE, "Immutable",
657                                         DBUS_TYPE_BOOLEAN, &service->immutable);
658 }
659
660 static void roaming_changed(struct connman_service *service)
661 {
662         if (service->path == NULL)
663                 return;
664
665         connman_dbus_property_changed_basic(service->path,
666                                 CONNMAN_SERVICE_INTERFACE, "Roaming",
667                                         DBUS_TYPE_BOOLEAN, &service->roaming);
668 }
669
670 static void autoconnect_changed(struct connman_service *service)
671 {
672         if (service->path == NULL)
673                 return;
674
675         connman_dbus_property_changed_basic(service->path,
676                                 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
677                                 DBUS_TYPE_BOOLEAN, &service->autoconnect);
678 }
679
680 static void passphrase_changed(struct connman_service *service)
681 {
682         dbus_bool_t required;
683
684         switch (service->type) {
685         case CONNMAN_SERVICE_TYPE_UNKNOWN:
686         case CONNMAN_SERVICE_TYPE_SYSTEM:
687         case CONNMAN_SERVICE_TYPE_ETHERNET:
688         case CONNMAN_SERVICE_TYPE_WIMAX:
689         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
690         case CONNMAN_SERVICE_TYPE_CELLULAR:
691         case CONNMAN_SERVICE_TYPE_GPS:
692         case CONNMAN_SERVICE_TYPE_VPN:
693         case CONNMAN_SERVICE_TYPE_GADGET:
694                 return;
695         case CONNMAN_SERVICE_TYPE_WIFI:
696                 required = FALSE;
697
698                 switch (service->security) {
699                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
700                 case CONNMAN_SERVICE_SECURITY_NONE:
701                         break;
702                 case CONNMAN_SERVICE_SECURITY_WEP:
703                 case CONNMAN_SERVICE_SECURITY_PSK:
704                 case CONNMAN_SERVICE_SECURITY_WPA:
705                 case CONNMAN_SERVICE_SECURITY_RSN:
706                         if (service->passphrase == NULL)
707                                 required = TRUE;
708                         break;
709                 case CONNMAN_SERVICE_SECURITY_8021X:
710                         break;
711                 }
712                 break;
713         }
714
715         connman_dbus_property_changed_basic(service->path,
716                                 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
717                                                 DBUS_TYPE_BOOLEAN, &required);
718 }
719
720 static void login_changed(struct connman_service *service)
721 {
722         dbus_bool_t required = service->login_required;
723
724         if (service->path == NULL)
725                 return;
726
727         connman_dbus_property_changed_basic(service->path,
728                                 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
729                                                 DBUS_TYPE_BOOLEAN, &required);
730 }
731
732 static void apn_changed(struct connman_service *service)
733 {
734         dbus_bool_t required;
735
736         switch (service->type) {
737         case CONNMAN_SERVICE_TYPE_UNKNOWN:
738         case CONNMAN_SERVICE_TYPE_SYSTEM:
739         case CONNMAN_SERVICE_TYPE_ETHERNET:
740         case CONNMAN_SERVICE_TYPE_WIMAX:
741         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
742         case CONNMAN_SERVICE_TYPE_WIFI:
743         case CONNMAN_SERVICE_TYPE_GPS:
744         case CONNMAN_SERVICE_TYPE_VPN:
745         case CONNMAN_SERVICE_TYPE_GADGET:
746                 return;
747         case CONNMAN_SERVICE_TYPE_CELLULAR:
748                 break;
749         }
750
751         required = (service->apn == NULL) ? TRUE : FALSE;
752
753         connman_dbus_property_changed_basic(service->path,
754                                 CONNMAN_SERVICE_INTERFACE, "SetupRequired",
755                                                 DBUS_TYPE_BOOLEAN, &required);
756 }
757
758 static void append_ethernet(DBusMessageIter *iter, void *user_data)
759 {
760         struct connman_service *service = user_data;
761
762         if (service->ipconfig_ipv4 != NULL)
763                 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
764                                                                         iter);
765         else if (service->ipconfig_ipv6 != NULL)
766                 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
767                                                                         iter);
768 }
769
770 static void append_ipv4(DBusMessageIter *iter, void *user_data)
771 {
772         struct connman_service *service = user_data;
773
774         if (is_connected(service) == FALSE)
775                 return;
776
777         if (service->ipconfig_ipv4 != NULL)
778                 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
779 }
780
781 static void append_ipv6(DBusMessageIter *iter, void *user_data)
782 {
783         struct connman_service *service = user_data;
784
785         if (is_connected(service) == FALSE)
786                 return;
787
788         if (service->ipconfig_ipv6 != NULL)
789                 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter);
790 }
791
792 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
793 {
794         struct connman_service *service = user_data;
795
796         if (service->ipconfig_ipv4 != NULL)
797                 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
798                                                         iter);
799 }
800
801 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
802 {
803         struct connman_service *service = user_data;
804
805         if (service->ipconfig_ipv6 != NULL)
806                 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
807                                                         iter);
808 }
809
810 static void append_dns(DBusMessageIter *iter, void *user_data)
811 {
812         struct connman_service *service = user_data;
813
814         if (is_connected(service) == FALSE)
815                 return;
816
817         if (service->nameservers != NULL) {
818                 int i;
819
820                 for (i = 0; service->nameservers[i]; i++)
821                         dbus_message_iter_append_basic(iter,
822                                 DBUS_TYPE_STRING, &service->nameservers[i]);
823
824                 return;
825         }
826
827         if (service->nameserver == NULL)
828                 return;
829
830         dbus_message_iter_append_basic(iter,
831                                 DBUS_TYPE_STRING, &service->nameserver);
832 }
833
834 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
835 {
836         struct connman_service *service = user_data;
837         int i;
838
839         if (service->nameservers == NULL)
840                 return;
841
842         for (i = 0; service->nameservers[i]; i++)
843                 dbus_message_iter_append_basic(iter,
844                                 DBUS_TYPE_STRING, &service->nameservers[i]);
845 }
846
847 static void append_domain(DBusMessageIter *iter, void *user_data)
848 {
849         struct connman_service *service = user_data;
850
851         if (is_connected(service) == FALSE && is_connecting(service) == FALSE)
852                 return;
853
854         if (service->domainname == NULL)
855                 return;
856
857         dbus_message_iter_append_basic(iter,
858                                 DBUS_TYPE_STRING, &service->domainname);
859 }
860
861 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
862 {
863         struct connman_service *service = user_data;
864         int i;
865
866         if (service->domains == NULL)
867                 return;
868
869         for (i = 0; service->domains[i]; i++)
870                 dbus_message_iter_append_basic(iter,
871                                 DBUS_TYPE_STRING, &service->domains[i]);
872 }
873
874 static void append_proxies(DBusMessageIter *iter, void *user_data)
875 {
876         struct connman_service *service = user_data;
877         int i;
878
879         if (service->proxies == NULL)
880                 return;
881
882         for (i = 0; service->proxies[i]; i++)
883                 dbus_message_iter_append_basic(iter,
884                                 DBUS_TYPE_STRING, &service->proxies[i]);
885 }
886
887 static void append_excludes(DBusMessageIter *iter, void *user_data)
888 {
889         struct connman_service *service = user_data;
890         int i;
891
892         if (service->excludes == NULL)
893                 return;
894
895         for (i = 0; service->excludes[i]; i++)
896                 dbus_message_iter_append_basic(iter,
897                                 DBUS_TYPE_STRING, &service->excludes[i]);
898 }
899
900 static void append_proxy(DBusMessageIter *iter, void *user_data)
901 {
902         struct connman_service *service = user_data;
903         const char *pac = NULL;
904         const char *method = proxymethod2string(
905                 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
906
907         DBG("");
908
909         if (is_connected(service) == FALSE)
910                 return;
911
912         switch (service->proxy) {
913         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
914                 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
915                 /* fall through */
916         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
917                 goto done;
918         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
919                 connman_dbus_dict_append_array(iter, "Servers",
920                                         DBUS_TYPE_STRING, append_proxies,
921                                         service);
922
923                 connman_dbus_dict_append_array(iter, "Excludes",
924                                         DBUS_TYPE_STRING, append_excludes,
925                                         service);
926                 break;
927         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
928                 /* Maybe DHCP, or WPAD,  has provided an url for a pac file */
929                 if (service->ipconfig_ipv4 != NULL)
930                         pac = __connman_ipconfig_get_proxy_autoconfig(
931                                 service->ipconfig_ipv4);
932                 else if (service->ipconfig_ipv6 != NULL)
933                         pac = __connman_ipconfig_get_proxy_autoconfig(
934                                 service->ipconfig_ipv6);
935
936                 if (service->pac == NULL && pac == NULL)
937                         goto done;
938
939                 if (service->pac != NULL)
940                         pac = service->pac;
941
942                 connman_dbus_dict_append_basic(iter, "URL",
943                                         DBUS_TYPE_STRING, &pac);
944                 break;
945         }
946
947         method = proxymethod2string(service->proxy);
948
949 done:
950         connman_dbus_dict_append_basic(iter, "Method",
951                                         DBUS_TYPE_STRING, &method);
952 }
953
954 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
955 {
956         struct connman_service *service = user_data;
957         const char *method;
958
959         switch (service->proxy) {
960         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
961                 break;
962         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
963                 if (service->proxies != NULL)
964                         connman_dbus_dict_append_array(iter, "Servers",
965                                                 DBUS_TYPE_STRING,
966                                                 append_proxies, service);
967
968                 if (service->excludes != NULL)
969                         connman_dbus_dict_append_array(iter, "Excludes",
970                                                 DBUS_TYPE_STRING,
971                                                 append_excludes, service);
972                 break;
973         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
974                 if (service->pac != NULL)
975                         connman_dbus_dict_append_basic(iter, "URL",
976                                         DBUS_TYPE_STRING, &service->pac);
977                 break;
978         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
979                 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
980                 break;
981         }
982
983         method = proxymethod2string(service->proxy);
984
985         connman_dbus_dict_append_basic(iter, "Method",
986                                 DBUS_TYPE_STRING, &method);
987 }
988
989 static void append_provider(DBusMessageIter *iter, void *user_data)
990 {
991         struct connman_service *service = user_data;
992
993         DBG("%p %p", service, service->provider);
994
995         if (is_connected(service) == FALSE)
996                 return;
997
998         if (service->provider != NULL)
999                 __connman_provider_append_properties(service->provider, iter);
1000 }
1001
1002
1003 static void settings_changed(struct connman_service *service)
1004 {
1005         connman_dbus_property_changed_dict(service->path,
1006                                         CONNMAN_SERVICE_INTERFACE, "IPv4",
1007                                                         append_ipv4, service);
1008
1009         connman_dbus_property_changed_dict(service->path,
1010                                         CONNMAN_SERVICE_INTERFACE, "IPv6",
1011                                                         append_ipv6, service);
1012 }
1013
1014 static void ipv4_configuration_changed(struct connman_service *service)
1015 {
1016         connman_dbus_property_changed_dict(service->path,
1017                                         CONNMAN_SERVICE_INTERFACE,
1018                                                         "IPv4.Configuration",
1019                                                         append_ipv4config,
1020                                                         service);
1021 }
1022
1023 static void ipv6_configuration_changed(struct connman_service *service)
1024 {
1025         connman_dbus_property_changed_dict(service->path,
1026                                         CONNMAN_SERVICE_INTERFACE,
1027                                                         "IPv6.Configuration",
1028                                                         append_ipv6config,
1029                                                         service);
1030 }
1031
1032 static void dns_changed(struct connman_service *service)
1033 {
1034         connman_dbus_property_changed_array(service->path,
1035                                 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1036                                         DBUS_TYPE_STRING, append_dns, service);
1037 }
1038
1039 static void dns_configuration_changed(struct connman_service *service)
1040 {
1041         connman_dbus_property_changed_array(service->path,
1042                                 CONNMAN_SERVICE_INTERFACE,
1043                                 "Nameservers.Configuration",
1044                                 DBUS_TYPE_STRING, append_dnsconfig, service);
1045
1046         dns_changed(service);
1047 }
1048
1049 static void domain_changed(struct connman_service *service)
1050 {
1051         connman_dbus_property_changed_array(service->path,
1052                                 CONNMAN_SERVICE_INTERFACE, "Domains",
1053                                 DBUS_TYPE_STRING, append_domain, service);
1054 }
1055
1056 static void domain_configuration_changed(struct connman_service *service)
1057 {
1058         connman_dbus_property_changed_array(service->path,
1059                                 CONNMAN_SERVICE_INTERFACE,
1060                                 "Domains.Configuration",
1061                                 DBUS_TYPE_STRING, append_domainconfig, service);
1062 }
1063
1064 static void proxy_changed(struct connman_service *service)
1065 {
1066         connman_dbus_property_changed_dict(service->path,
1067                                         CONNMAN_SERVICE_INTERFACE, "Proxy",
1068                                                         append_proxy, service);
1069 }
1070
1071 static void proxy_configuration_changed(struct connman_service *service)
1072 {
1073         connman_dbus_property_changed_dict(service->path,
1074                         CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1075                                                 append_proxyconfig, service);
1076
1077         proxy_changed(service);
1078 }
1079
1080 static void link_changed(struct connman_service *service)
1081 {
1082         connman_dbus_property_changed_dict(service->path,
1083                                         CONNMAN_SERVICE_INTERFACE, "Ethernet",
1084                                                 append_ethernet, service);
1085 }
1086
1087 static void stats_append_counters(DBusMessageIter *dict,
1088                         struct connman_stats_data *stats,
1089                         struct connman_stats_data *counters,
1090                         connman_bool_t append_all)
1091 {
1092         if (counters->rx_packets != stats->rx_packets || append_all) {
1093                 counters->rx_packets = stats->rx_packets;
1094                 connman_dbus_dict_append_basic(dict, "RX.Packets",
1095                                         DBUS_TYPE_UINT32, &stats->rx_packets);
1096         }
1097
1098         if (counters->tx_packets != stats->tx_packets || append_all) {
1099                 counters->tx_packets = stats->tx_packets;
1100                 connman_dbus_dict_append_basic(dict, "TX.Packets",
1101                                         DBUS_TYPE_UINT32, &stats->tx_packets);
1102         }
1103
1104         if (counters->rx_bytes != stats->rx_bytes || append_all) {
1105                 counters->rx_bytes = stats->rx_bytes;
1106                 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1107                                         DBUS_TYPE_UINT32, &stats->rx_bytes);
1108         }
1109
1110         if (counters->tx_bytes != stats->tx_bytes || append_all) {
1111                 counters->tx_bytes = stats->tx_bytes;
1112                 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1113                                         DBUS_TYPE_UINT32, &stats->tx_bytes);
1114         }
1115
1116         if (counters->rx_errors != stats->rx_errors || append_all) {
1117                 counters->rx_errors = stats->rx_errors;
1118                 connman_dbus_dict_append_basic(dict, "RX.Errors",
1119                                         DBUS_TYPE_UINT32, &stats->rx_errors);
1120         }
1121
1122         if (counters->tx_errors != stats->tx_errors || append_all) {
1123                 counters->tx_errors = stats->tx_errors;
1124                 connman_dbus_dict_append_basic(dict, "TX.Errors",
1125                                         DBUS_TYPE_UINT32, &stats->tx_errors);
1126         }
1127
1128         if (counters->rx_dropped != stats->rx_dropped || append_all) {
1129                 counters->rx_dropped = stats->rx_dropped;
1130                 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1131                                         DBUS_TYPE_UINT32, &stats->rx_dropped);
1132         }
1133
1134         if (counters->tx_dropped != stats->tx_dropped || append_all) {
1135                 counters->tx_dropped = stats->tx_dropped;
1136                 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1137                                         DBUS_TYPE_UINT32, &stats->tx_dropped);
1138         }
1139
1140         if (counters->time != stats->time || append_all) {
1141                 counters->time = stats->time;
1142                 connman_dbus_dict_append_basic(dict, "Time",
1143                                         DBUS_TYPE_UINT32, &stats->time);
1144         }
1145 }
1146
1147 static void stats_append(struct connman_service *service,
1148                                 const char *counter,
1149                                 struct connman_stats_counter *counters,
1150                                 connman_bool_t append_all)
1151 {
1152         DBusMessageIter array, dict;
1153         DBusMessage *msg;
1154
1155         DBG("service %p counter %s", service, counter);
1156
1157         msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1158         if (msg == NULL)
1159                 return;
1160
1161         dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1162                                 &service->path, DBUS_TYPE_INVALID);
1163
1164         dbus_message_iter_init_append(msg, &array);
1165
1166         /* home counter */
1167         connman_dbus_dict_open(&array, &dict);
1168
1169         stats_append_counters(&dict, &service->stats.data,
1170                                 &counters->stats.data, append_all);
1171
1172         connman_dbus_dict_close(&array, &dict);
1173
1174         /* roaming counter */
1175         connman_dbus_dict_open(&array, &dict);
1176
1177         stats_append_counters(&dict, &service->stats_roaming.data,
1178                                 &counters->stats_roaming.data, append_all);
1179
1180         connman_dbus_dict_close(&array, &dict);
1181
1182         __connman_counter_send_usage(counter, msg);
1183 }
1184
1185 static void stats_update(struct connman_service *service,
1186                                 unsigned int rx_packets, unsigned int tx_packets,
1187                                 unsigned int rx_bytes, unsigned int tx_bytes,
1188                                 unsigned int rx_errors, unsigned int tx_errors,
1189                                 unsigned int rx_dropped, unsigned int tx_dropped)
1190 {
1191         struct connman_stats *stats = stats_get(service);
1192         struct connman_stats_data *data_last = &stats->data_last;
1193         struct connman_stats_data *data = &stats->data;
1194         unsigned int seconds;
1195
1196         DBG("service %p", service);
1197
1198         if (stats->valid == TRUE) {
1199                 data->rx_packets +=
1200                         rx_packets - data_last->rx_packets;
1201                 data->tx_packets +=
1202                         tx_packets - data_last->tx_packets;
1203                 data->rx_bytes +=
1204                         rx_bytes - data_last->rx_bytes;
1205                 data->tx_bytes +=
1206                         tx_bytes - data_last->tx_bytes;
1207                 data->rx_errors +=
1208                         rx_errors - data_last->rx_errors;
1209                 data->tx_errors +=
1210                         tx_errors - data_last->tx_errors;
1211                 data->rx_dropped +=
1212                         rx_dropped - data_last->rx_dropped;
1213                 data->tx_dropped +=
1214                         tx_dropped - data_last->tx_dropped;
1215         } else {
1216                 stats->valid = TRUE;
1217         }
1218
1219         data_last->rx_packets = rx_packets;
1220         data_last->tx_packets = tx_packets;
1221         data_last->rx_bytes = rx_bytes;
1222         data_last->tx_bytes = tx_bytes;
1223         data_last->rx_errors = rx_errors;
1224         data_last->tx_errors = tx_errors;
1225         data_last->rx_dropped = rx_dropped;
1226         data_last->tx_dropped = tx_dropped;
1227
1228         seconds = g_timer_elapsed(stats->timer, NULL);
1229         stats->data.time = stats->data_last.time + seconds;
1230 }
1231
1232 void __connman_service_notify(struct connman_service *service,
1233                         unsigned int rx_packets, unsigned int tx_packets,
1234                         unsigned int rx_bytes, unsigned int tx_bytes,
1235                         unsigned int rx_errors, unsigned int tx_errors,
1236                         unsigned int rx_dropped, unsigned int tx_dropped)
1237 {
1238         GHashTableIter iter;
1239         gpointer key, value;
1240         const char *counter;
1241         struct connman_stats_counter *counters;
1242         struct connman_stats_data *data;
1243         int err;
1244
1245         if (service == NULL)
1246                 return;
1247
1248         if (is_connected(service) == FALSE)
1249                 return;
1250
1251         stats_update(service,
1252                 rx_packets, tx_packets,
1253                 rx_bytes, tx_bytes,
1254                 rx_errors, tx_errors,
1255                 rx_dropped, tx_dropped);
1256
1257         data = &stats_get(service)->data;
1258         err = __connman_stats_update(service, service->roaming, data);
1259         if (err < 0)
1260                 connman_error("Failed to store statistics for %s",
1261                                 service->identifier);
1262
1263         g_hash_table_iter_init(&iter, service->counter_table);
1264         while (g_hash_table_iter_next(&iter, &key, &value)) {
1265                 counter = key;
1266                 counters = value;
1267
1268                 stats_append(service, counter, counters, counters->append_all);
1269                 counters->append_all = FALSE;
1270         }
1271 }
1272
1273 int __connman_service_counter_register(const char *counter)
1274 {
1275         struct connman_service *service;
1276         GSequenceIter *iter;
1277         struct connman_stats_counter *counters;
1278
1279         DBG("counter %s", counter);
1280
1281         counter_list = g_slist_append(counter_list, (gpointer)counter);
1282
1283         iter = g_sequence_get_begin_iter(service_list);
1284
1285         while (g_sequence_iter_is_end(iter) == FALSE) {
1286                 service = g_sequence_get(iter);
1287
1288                 counters = g_try_new0(struct connman_stats_counter, 1);
1289                 if (counters == NULL)
1290                         return -ENOMEM;
1291
1292                 counters->append_all = TRUE;
1293
1294                 g_hash_table_replace(service->counter_table, (gpointer)counter,
1295                                         counters);
1296
1297                 iter = g_sequence_iter_next(iter);
1298         }
1299
1300         return 0;
1301 }
1302
1303 void __connman_service_counter_unregister(const char *counter)
1304 {
1305         struct connman_service *service;
1306         GSequenceIter *iter;
1307
1308         DBG("counter %s", counter);
1309
1310         iter = g_sequence_get_begin_iter(service_list);
1311
1312         while (g_sequence_iter_is_end(iter) == FALSE) {
1313                 service = g_sequence_get(iter);
1314
1315                 g_hash_table_remove(service->counter_table, counter);
1316
1317                 iter = g_sequence_iter_next(iter);
1318         }
1319
1320         counter_list = g_slist_remove(counter_list, counter);
1321 }
1322
1323 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1324                                         struct connman_service *service)
1325 {
1326         dbus_bool_t required;
1327         const char *str;
1328
1329         str = __connman_service_type2string(service->type);
1330         if (str != NULL)
1331                 connman_dbus_dict_append_basic(dict, "Type",
1332                                                 DBUS_TYPE_STRING, &str);
1333
1334         str = mode2string(service->mode);
1335         if (str != NULL)
1336                 connman_dbus_dict_append_basic(dict, "Mode",
1337                                                 DBUS_TYPE_STRING, &str);
1338
1339         str = security2string(service->security);
1340         if (str != NULL)
1341                 connman_dbus_dict_append_basic(dict, "Security",
1342                                                 DBUS_TYPE_STRING, &str);
1343
1344         str = state2string(service->state);
1345         if (str != NULL)
1346                 connman_dbus_dict_append_basic(dict, "State",
1347                                                 DBUS_TYPE_STRING, &str);
1348
1349         str = error2string(service->error);
1350         if (str != NULL)
1351                 connman_dbus_dict_append_basic(dict, "Error",
1352                                                 DBUS_TYPE_STRING, &str);
1353
1354         if (service->strength > 0)
1355                 connman_dbus_dict_append_basic(dict, "Strength",
1356                                         DBUS_TYPE_BYTE, &service->strength);
1357
1358         connman_dbus_dict_append_basic(dict, "Favorite",
1359                                         DBUS_TYPE_BOOLEAN, &service->favorite);
1360
1361         connman_dbus_dict_append_basic(dict, "Immutable",
1362                                         DBUS_TYPE_BOOLEAN, &service->immutable);
1363
1364         if (service->favorite == TRUE)
1365                 connman_dbus_dict_append_basic(dict, "AutoConnect",
1366                                 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1367         else
1368                 connman_dbus_dict_append_basic(dict, "AutoConnect",
1369                                         DBUS_TYPE_BOOLEAN, &service->favorite);
1370
1371         if (service->name != NULL)
1372                 connman_dbus_dict_append_basic(dict, "Name",
1373                                         DBUS_TYPE_STRING, &service->name);
1374
1375         connman_dbus_dict_append_basic(dict, "LoginRequired",
1376                                 DBUS_TYPE_BOOLEAN, &service->login_required);
1377
1378         switch (service->type) {
1379         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1380         case CONNMAN_SERVICE_TYPE_SYSTEM:
1381         case CONNMAN_SERVICE_TYPE_GPS:
1382         case CONNMAN_SERVICE_TYPE_VPN:
1383         case CONNMAN_SERVICE_TYPE_GADGET:
1384                 break;
1385         case CONNMAN_SERVICE_TYPE_CELLULAR:
1386                 connman_dbus_dict_append_basic(dict, "Roaming",
1387                                         DBUS_TYPE_BOOLEAN, &service->roaming);
1388
1389                 if (service->mcc != NULL && service->mnc != NULL) {
1390                         connman_dbus_dict_append_basic(dict, "MCC",
1391                                         DBUS_TYPE_STRING, &service->mcc);
1392                         connman_dbus_dict_append_basic(dict, "MNC",
1393                                         DBUS_TYPE_STRING, &service->mnc);
1394                 }
1395
1396                 if (service->apn != NULL) {
1397                         connman_dbus_dict_append_basic(dict, "APN",
1398                                         DBUS_TYPE_STRING, &service->apn);
1399
1400                         if (service->username != NULL)
1401                                 connman_dbus_dict_append_basic(dict,
1402                                         "Username", DBUS_TYPE_STRING,
1403                                                         &service->username);
1404
1405                         if (service->password != NULL)
1406                                 connman_dbus_dict_append_basic(dict,
1407                                         "Password", DBUS_TYPE_STRING,
1408                                                         &service->password);
1409
1410                         required = FALSE;
1411                 } else
1412                         required = TRUE;
1413
1414                 connman_dbus_dict_append_basic(dict, "SetupRequired",
1415                                                 DBUS_TYPE_BOOLEAN, &required);
1416                 connman_dbus_dict_append_dict(dict, "Ethernet",
1417                                                 append_ethernet, service);
1418                 break;
1419         case CONNMAN_SERVICE_TYPE_WIFI:
1420                 if (service->passphrase != NULL && limited == FALSE)
1421                         connman_dbus_dict_append_basic(dict, "Passphrase",
1422                                 DBUS_TYPE_STRING, &service->passphrase);
1423
1424                 required = FALSE;
1425
1426                 switch (service->security) {
1427                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1428                 case CONNMAN_SERVICE_SECURITY_NONE:
1429                         break;
1430                 case CONNMAN_SERVICE_SECURITY_WEP:
1431                 case CONNMAN_SERVICE_SECURITY_PSK:
1432                 case CONNMAN_SERVICE_SECURITY_WPA:
1433                 case CONNMAN_SERVICE_SECURITY_RSN:
1434                         if (service->passphrase == NULL)
1435                                 required = TRUE;
1436                         break;
1437                 case CONNMAN_SERVICE_SECURITY_8021X:
1438                         break;
1439                 }
1440
1441                 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1442                                                 DBUS_TYPE_BOOLEAN, &required);
1443                 /* fall through */
1444         case CONNMAN_SERVICE_TYPE_ETHERNET:
1445         case CONNMAN_SERVICE_TYPE_WIMAX:
1446         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1447                 connman_dbus_dict_append_dict(dict, "Ethernet",
1448                                                 append_ethernet, service);
1449                 break;
1450         }
1451
1452         connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1453
1454         connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1455                                                 append_ipv4config, service);
1456
1457         connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1458
1459         connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1460                                                 append_ipv6config, service);
1461
1462         connman_dbus_dict_append_array(dict, "Nameservers",
1463                                 DBUS_TYPE_STRING, append_dns, service);
1464
1465         connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1466                                 DBUS_TYPE_STRING, append_dnsconfig, service);
1467
1468         connman_dbus_dict_append_array(dict, "Domains",
1469                                 DBUS_TYPE_STRING, append_domain, service);
1470
1471         connman_dbus_dict_append_array(dict, "Domains.Configuration",
1472                                 DBUS_TYPE_STRING, append_domainconfig, service);
1473
1474         connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1475
1476         connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1477                                                 append_proxyconfig, service);
1478
1479         connman_dbus_dict_append_dict(dict, "Provider",
1480                                                 append_provider, service);
1481 }
1482
1483 static void append_struct(gpointer value, gpointer user_data)
1484 {
1485         struct connman_service *service = value;
1486         DBusMessageIter *iter = user_data;
1487         DBusMessageIter entry, dict;
1488
1489         if (service->path == NULL || service->hidden == TRUE)
1490                 return;
1491
1492         dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1493
1494         dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1495                                                         &service->path);
1496
1497         connman_dbus_dict_open(&entry, &dict);
1498         append_properties(&dict, TRUE, service);
1499         connman_dbus_dict_close(&entry, &dict);
1500
1501         dbus_message_iter_close_container(iter, &entry);
1502 }
1503
1504 void __connman_service_list_struct(DBusMessageIter *iter)
1505 {
1506         g_sequence_foreach(service_list, append_struct, iter);
1507 }
1508
1509 int __connman_service_get_index(struct connman_service *service)
1510 {
1511         if (service == NULL)
1512                 return -1;
1513
1514         if (service->network == NULL)
1515                 return -1;
1516
1517         return connman_network_get_index(service->network);
1518 }
1519
1520 void __connman_service_set_domainname(struct connman_service *service,
1521                                                 const char *domainname)
1522 {
1523         if (service == NULL)
1524                 return;
1525
1526         g_free(service->domainname);
1527         service->domainname = g_strdup(domainname);
1528
1529         domain_changed(service);
1530 }
1531
1532 const char *connman_service_get_domainname(struct connman_service *service)
1533 {
1534         if (service == NULL)
1535                 return NULL;
1536
1537         if (service->domains != NULL)
1538                 return service->domains[0];
1539         else
1540                 return service->domainname;
1541 }
1542
1543 const char *connman_service_get_nameserver(struct connman_service *service)
1544 {
1545         if (service == NULL)
1546                 return NULL;
1547
1548         return service->nameserver;
1549 }
1550
1551 enum connman_service_proxy_method connman_service_get_proxy_method(
1552                                         struct connman_service *service)
1553 {
1554         if (service == NULL)
1555                 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1556
1557         return service->proxy;
1558 }
1559
1560 char **connman_service_get_proxy_servers(struct connman_service *service)
1561 {
1562         return g_strdupv(service->proxies);
1563 }
1564
1565 char **connman_service_get_proxy_excludes(struct connman_service *service)
1566 {
1567         return g_strdupv(service->excludes);
1568 }
1569
1570 const char *connman_service_get_proxy_url(struct connman_service *service)
1571 {
1572         if (service == NULL)
1573                 return NULL;
1574
1575         return service->pac;
1576 }
1577
1578 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1579                                                         const char *url)
1580 {
1581         if (service == NULL)
1582                 return;
1583
1584         if (service->ipconfig_ipv4) {
1585                 if (__connman_ipconfig_set_proxy_autoconfig(
1586                             service->ipconfig_ipv4, url) < 0)
1587                         return;
1588         } else if (service->ipconfig_ipv6) {
1589                 if (__connman_ipconfig_set_proxy_autoconfig(
1590                             service->ipconfig_ipv6, url) < 0)
1591                         return;
1592         } else
1593                 return;
1594
1595         proxy_changed(service);
1596 }
1597
1598 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1599 {
1600         if (service == NULL)
1601                 return NULL;
1602
1603         if (service->ipconfig_ipv4)
1604                 return __connman_ipconfig_get_proxy_autoconfig(
1605                                                 service->ipconfig_ipv4);
1606         else if (service->ipconfig_ipv6)
1607                 return __connman_ipconfig_get_proxy_autoconfig(
1608                                                 service->ipconfig_ipv6);
1609         return NULL;
1610 }
1611
1612 void __connman_service_set_passphrase(struct connman_service *service,
1613                                         const char* passphrase)
1614 {
1615         if (service->immutable == TRUE)
1616                 return;
1617
1618         g_free(service->passphrase);
1619         service->passphrase = g_strdup(passphrase);
1620
1621         passphrase_changed(service);
1622
1623         if (service->network != NULL)
1624                 connman_network_set_string(service->network,
1625                                         "WiFi.Passphrase",
1626                                         service->passphrase);
1627
1628         __connman_storage_save_service(service);
1629 }
1630
1631 static DBusMessage *get_properties(DBusConnection *conn,
1632                                         DBusMessage *msg, void *user_data)
1633 {
1634         struct connman_service *service = user_data;
1635         DBusMessage *reply;
1636         DBusMessageIter array, dict;
1637
1638         DBG("service %p", service);
1639
1640         reply = dbus_message_new_method_return(msg);
1641         if (reply == NULL)
1642                 return NULL;
1643
1644         dbus_message_iter_init_append(reply, &array);
1645
1646         connman_dbus_dict_open(&array, &dict);
1647         append_properties(&dict, FALSE, service);
1648         connman_dbus_dict_close(&array, &dict);
1649
1650         return reply;
1651 }
1652
1653 static int update_proxy_configuration(struct connman_service *service,
1654                                 DBusMessageIter *array)
1655 {
1656         DBusMessageIter dict;
1657         enum connman_service_proxy_method method;
1658         GString *servers_str = NULL;
1659         GString *excludes_str = NULL;
1660         const char *url = NULL;
1661
1662         method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1663
1664         dbus_message_iter_recurse(array, &dict);
1665
1666         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1667                 DBusMessageIter entry, variant;
1668                 const char *key;
1669                 int type;
1670
1671                 dbus_message_iter_recurse(&dict, &entry);
1672
1673                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1674                         goto error;
1675
1676                 dbus_message_iter_get_basic(&entry, &key);
1677                 dbus_message_iter_next(&entry);
1678
1679                 if (dbus_message_iter_get_arg_type(&entry) !=
1680                                                         DBUS_TYPE_VARIANT)
1681                         goto error;
1682
1683                 dbus_message_iter_recurse(&entry, &variant);
1684                 type = dbus_message_iter_get_arg_type(&variant);
1685
1686                 if (g_str_equal(key, "Method") == TRUE) {
1687                         const char *val;
1688
1689                         if (type != DBUS_TYPE_STRING)
1690                                 goto error;
1691
1692                         dbus_message_iter_get_basic(&variant, &val);
1693                         method = string2proxymethod(val);
1694                 } else if (g_str_equal(key, "URL") == TRUE) {
1695                         if (type != DBUS_TYPE_STRING)
1696                                 goto error;
1697
1698                         dbus_message_iter_get_basic(&variant, &url);
1699                 } else if (g_str_equal(key, "Servers") == TRUE) {
1700                         DBusMessageIter str_array;
1701
1702                         if (type != DBUS_TYPE_ARRAY)
1703                                 goto error;
1704
1705                         servers_str = g_string_new(NULL);
1706                         if (servers_str == NULL)
1707                                 goto error;
1708
1709                         dbus_message_iter_recurse(&variant, &str_array);
1710
1711                         while (dbus_message_iter_get_arg_type(&str_array) ==
1712                                                         DBUS_TYPE_STRING) {
1713                                 char *val = NULL;
1714
1715                                 dbus_message_iter_get_basic(&str_array, &val);
1716
1717                                 if (servers_str->len > 0)
1718                                         g_string_append_printf(servers_str,
1719                                                         " %s", val);
1720                                 else
1721                                         g_string_append(servers_str, val);
1722
1723                                 dbus_message_iter_next(&str_array);
1724                         }
1725                 } else if (g_str_equal(key, "Excludes") == TRUE) {
1726                         DBusMessageIter str_array;
1727
1728                         if (type != DBUS_TYPE_ARRAY)
1729                                 goto error;
1730
1731                         excludes_str = g_string_new(NULL);
1732                         if (excludes_str == NULL)
1733                                 goto error;
1734
1735                         dbus_message_iter_recurse(&variant, &str_array);
1736
1737                         while (dbus_message_iter_get_arg_type(&str_array) ==
1738                                                         DBUS_TYPE_STRING) {
1739                                 char *val = NULL;
1740
1741                                 dbus_message_iter_get_basic(&str_array, &val);
1742
1743                                 if (excludes_str->len > 0)
1744                                         g_string_append_printf(excludes_str,
1745                                                         " %s", val);
1746                                 else
1747                                         g_string_append(excludes_str, val);
1748
1749                                 dbus_message_iter_next(&str_array);
1750                         }
1751                 }
1752
1753                 dbus_message_iter_next(&dict);
1754         }
1755
1756         switch (method) {
1757         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1758                 break;
1759         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1760                 if (servers_str == NULL && service->proxies == NULL)
1761                         goto error;
1762
1763                 if (servers_str != NULL) {
1764                         g_strfreev(service->proxies);
1765
1766                         if (servers_str->len > 0)
1767                                 service->proxies = g_strsplit_set(
1768                                         servers_str->str, " ", 0);
1769                         else
1770                                 service->proxies = NULL;
1771                 }
1772
1773                 if (excludes_str != NULL) {
1774                         g_strfreev(service->excludes);
1775
1776                         if (excludes_str->len > 0)
1777                                 service->excludes = g_strsplit_set(
1778                                         excludes_str->str, " ", 0);
1779                         else
1780                                 service->excludes = NULL;
1781                 }
1782
1783                 if (service->proxies == NULL)
1784                         method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
1785
1786                 break;
1787         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1788                 g_free(service->pac);
1789
1790                 if (url != NULL && strlen(url) > 0)
1791                         service->pac = g_strdup(url);
1792                 else
1793                         service->pac = NULL;
1794
1795                 /* if we are connected:
1796                    - if service->pac == NULL
1797                    - if __connman_ipconfig_get_proxy_autoconfig(
1798                    service->ipconfig) == NULL
1799                    --> We should start WPAD */
1800
1801                 break;
1802         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1803                 goto error;
1804         }
1805
1806         if (servers_str != NULL)
1807                 g_string_free(servers_str, TRUE);
1808
1809         if (excludes_str != NULL)
1810                 g_string_free(excludes_str, TRUE);
1811
1812         service->proxy = method;
1813
1814         return 0;
1815
1816 error:
1817         if (servers_str != NULL)
1818                 g_string_free(servers_str, TRUE);
1819
1820         if (excludes_str != NULL)
1821                 g_string_free(excludes_str, TRUE);
1822
1823         return -EINVAL;
1824 }
1825
1826 static DBusMessage *set_property(DBusConnection *conn,
1827                                         DBusMessage *msg, void *user_data)
1828 {
1829         struct connman_service *service = user_data;
1830         DBusMessageIter iter, value;
1831         const char *name;
1832         int type;
1833
1834         DBG("service %p", service);
1835
1836         if (dbus_message_iter_init(msg, &iter) == FALSE)
1837                 return __connman_error_invalid_arguments(msg);
1838
1839         dbus_message_iter_get_basic(&iter, &name);
1840         dbus_message_iter_next(&iter);
1841         dbus_message_iter_recurse(&iter, &value);
1842
1843         type = dbus_message_iter_get_arg_type(&value);
1844
1845         if (g_str_has_prefix(name, "AutoConnect") == TRUE) {
1846                 connman_bool_t autoconnect;
1847
1848                 if (type != DBUS_TYPE_BOOLEAN)
1849                         return __connman_error_invalid_arguments(msg);
1850
1851                 if (service->favorite == FALSE)
1852                         return __connman_error_invalid_service(msg);
1853
1854                 dbus_message_iter_get_basic(&value, &autoconnect);
1855
1856                 if (service->autoconnect == autoconnect)
1857                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
1858
1859                 service->autoconnect = autoconnect;
1860
1861                 autoconnect_changed(service);
1862
1863                 __connman_storage_save_service(service);
1864         } else if (g_str_equal(name, "Passphrase") == TRUE) {
1865                 const char *passphrase;
1866
1867                 if (type != DBUS_TYPE_STRING)
1868                         return __connman_error_invalid_arguments(msg);
1869
1870                 if (service->immutable == TRUE)
1871                         return __connman_error_not_supported(msg);
1872
1873                 dbus_message_iter_get_basic(&value, &passphrase);
1874
1875                 __connman_service_set_passphrase(service, passphrase);
1876         } else if (g_str_equal(name, "APN") == TRUE) {
1877                 const char *apn;
1878
1879                 if (type != DBUS_TYPE_STRING)
1880                         return __connman_error_invalid_arguments(msg);
1881
1882                 if (service->immutable == TRUE)
1883                         return __connman_error_not_supported(msg);
1884
1885                 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
1886                         return __connman_error_invalid_service(msg);
1887
1888                 dbus_message_iter_get_basic(&value, &apn);
1889
1890                 g_free(service->apn);
1891                 service->apn = g_strdup(apn);
1892
1893                 apn_changed(service);
1894
1895                 if (service->network != NULL)
1896                         connman_network_set_string(service->network,
1897                                                 "Cellular.APN", service->apn);
1898
1899                 __connman_storage_save_service(service);
1900         } else if (g_str_equal(name, "Username") == TRUE) {
1901                 const char *username;
1902
1903                 if (type != DBUS_TYPE_STRING)
1904                         return __connman_error_invalid_arguments(msg);
1905
1906                 if (service->immutable == TRUE)
1907                         return __connman_error_not_supported(msg);
1908
1909                 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
1910                         return __connman_error_invalid_service(msg);
1911
1912                 dbus_message_iter_get_basic(&value, &username);
1913
1914                 g_free(service->username);
1915                 service->username = g_strdup(username);
1916
1917                 if (service->network != NULL)
1918                         connman_network_set_string(service->network,
1919                                         "Cellular.Username", service->username);
1920
1921                 __connman_storage_save_service(service);
1922         } else if (g_str_equal(name, "Password") == TRUE) {
1923                 const char *password;
1924
1925                 if (type != DBUS_TYPE_STRING)
1926                         return __connman_error_invalid_arguments(msg);
1927
1928                 if (service->immutable == TRUE)
1929                         return __connman_error_not_supported(msg);
1930
1931                 if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
1932                         return __connman_error_invalid_service(msg);
1933
1934                 dbus_message_iter_get_basic(&value, &password);
1935
1936                 g_free(service->password);
1937                 service->password = g_strdup(password);
1938
1939                 if (service->network != NULL)
1940                         connman_network_set_string(service->network,
1941                                         "Cellular.Password", service->password);
1942
1943                 __connman_storage_save_service(service);
1944         } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
1945                 DBusMessageIter entry;
1946                 GString *str;
1947                 int index;
1948                 const char *gw;
1949
1950                 if (type != DBUS_TYPE_ARRAY)
1951                         return __connman_error_invalid_arguments(msg);
1952
1953                 str = g_string_new(NULL);
1954                 if (str == NULL)
1955                         return __connman_error_invalid_arguments(msg);
1956
1957                 index = connman_network_get_index(service->network);
1958                 gw = __connman_ipconfig_get_gateway(index);
1959
1960                 if (gw && strlen(gw))
1961                         __connman_service_nameserver_del_routes(service);
1962
1963                 dbus_message_iter_recurse(&value, &entry);
1964
1965                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
1966                         const char *val;
1967                         dbus_message_iter_get_basic(&entry, &val);
1968                         dbus_message_iter_next(&entry);
1969                         if (str->len > 0)
1970                                 g_string_append_printf(str, " %s", val);
1971                         else
1972                                 g_string_append(str, val);
1973                 }
1974
1975                 g_strfreev(service->nameservers);
1976
1977                 if (str->len > 0)
1978                         service->nameservers = g_strsplit_set(str->str, " ", 0);
1979                 else
1980                         service->nameservers = NULL;
1981
1982                 g_string_free(str, TRUE);
1983
1984                 if (gw && strlen(gw))
1985                         __connman_service_nameserver_add_routes(service, gw);
1986
1987                 update_nameservers(service);
1988                 dns_configuration_changed(service);
1989
1990                 __connman_storage_save_service(service);
1991         } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
1992                 DBusMessageIter entry;
1993                 GString *str;
1994
1995                 if (type != DBUS_TYPE_ARRAY)
1996                         return __connman_error_invalid_arguments(msg);
1997
1998                 str = g_string_new(NULL);
1999                 if (str == NULL)
2000                         return __connman_error_invalid_arguments(msg);
2001
2002                 dbus_message_iter_recurse(&value, &entry);
2003
2004                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2005                         const char *val;
2006                         dbus_message_iter_get_basic(&entry, &val);
2007                         dbus_message_iter_next(&entry);
2008                         if (str->len > 0)
2009                                 g_string_append_printf(str, " %s", val);
2010                         else
2011                                 g_string_append(str, val);
2012                 }
2013
2014                 g_strfreev(service->domains);
2015
2016                 if (str->len > 0)
2017                         service->domains = g_strsplit_set(str->str, " ", 0);
2018                 else
2019                         service->domains = NULL;
2020
2021                 g_string_free(str, TRUE);
2022
2023                 update_nameservers(service);
2024                 domain_configuration_changed(service);
2025
2026                 __connman_storage_save_service(service);
2027         } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2028                 int err;
2029
2030                 if (type != DBUS_TYPE_ARRAY)
2031                         return __connman_error_invalid_arguments(msg);
2032
2033                 err = update_proxy_configuration(service, &value);
2034
2035                 if (err < 0)
2036                         return __connman_error_failed(msg, -err);
2037
2038                 proxy_configuration_changed(service);
2039
2040                 __connman_storage_save_service(service);
2041         } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2042                         g_str_equal(name, "IPv6.Configuration")) {
2043
2044                 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
2045                 int err = 0;
2046
2047                 DBG("%s", name);
2048
2049                 if (service->ipconfig_ipv4 == NULL &&
2050                                         service->ipconfig_ipv6 == NULL)
2051                         return __connman_error_invalid_property(msg);
2052
2053                 if (is_connecting(service) ||
2054                                 is_connected(service)) {
2055                         __connman_network_clear_ipconfig(service->network,
2056                                                         service->ipconfig_ipv4);
2057                         __connman_network_clear_ipconfig(service->network,
2058                                                         service->ipconfig_ipv6);
2059                 }
2060
2061                 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2062                         type = CONNMAN_IPCONFIG_TYPE_IPV4;
2063                         err = __connman_ipconfig_set_config(
2064                                         service->ipconfig_ipv4, type, &value);
2065                 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2066                         type = CONNMAN_IPCONFIG_TYPE_IPV6;
2067                         err = __connman_ipconfig_set_config(
2068                                         service->ipconfig_ipv6, type, &value);
2069                 }
2070
2071                 if (err < 0) {
2072                         if (is_connected(service) ||
2073                                         is_connecting(service))
2074                                 __connman_network_set_ipconfig(
2075                                                 service->network,
2076                                                 service->ipconfig_ipv4,
2077                                                 service->ipconfig_ipv6);
2078                         return __connman_error_failed(msg, -err);
2079                 }
2080
2081                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
2082                         ipv4_configuration_changed(service);
2083                 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
2084                         ipv6_configuration_changed(service);
2085
2086                 if (is_connecting(service) ||
2087                                 is_connected(service))
2088                         __connman_network_set_ipconfig(service->network,
2089                                                 service->ipconfig_ipv4,
2090                                                 service->ipconfig_ipv6);
2091
2092                 __connman_storage_save_service(service);
2093         } else
2094                 return __connman_error_invalid_property(msg);
2095
2096         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2097 }
2098
2099 static void set_idle(struct connman_service *service)
2100 {
2101         service->state = CONNMAN_SERVICE_STATE_IDLE;
2102         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2103         state_changed(service);
2104 }
2105
2106 static DBusMessage *clear_property(DBusConnection *conn,
2107                                         DBusMessage *msg, void *user_data)
2108 {
2109         struct connman_service *service = user_data;
2110         const char *name;
2111
2112         DBG("service %p", service);
2113
2114         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2115                                                         DBUS_TYPE_INVALID);
2116
2117         if (g_str_equal(name, "Error") == TRUE) {
2118                 set_idle(service);
2119
2120                 g_get_current_time(&service->modified);
2121                 __connman_storage_save_service(service);
2122         } else if (g_str_equal(name, "Passphrase") == TRUE) {
2123                 if (service->immutable == TRUE)
2124                         return __connman_error_not_supported(msg);
2125
2126                 g_free(service->passphrase);
2127                 service->passphrase = NULL;
2128
2129                 passphrase_changed(service);
2130
2131                 __connman_storage_save_service(service);
2132         } else
2133                 return __connman_error_invalid_property(msg);
2134
2135         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2136 }
2137
2138 static connman_bool_t is_ignore(struct connman_service *service)
2139 {
2140         if (service->autoconnect == FALSE)
2141                 return TRUE;
2142
2143         if (service->roaming == TRUE)
2144                 return TRUE;
2145
2146         if (service->ignore == TRUE)
2147                 return TRUE;
2148
2149         if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
2150                 return TRUE;
2151
2152         return FALSE;
2153 }
2154
2155 void __connman_service_auto_connect(void)
2156 {
2157         struct connman_service *service = NULL;
2158         GSequenceIter *iter;
2159
2160         DBG("");
2161
2162         iter = g_sequence_get_begin_iter(service_list);
2163
2164         while (g_sequence_iter_is_end(iter) == FALSE) {
2165                 service = g_sequence_get(iter);
2166
2167                 if (service->pending != NULL)
2168                         return;
2169
2170                 if (is_connecting(service) == TRUE)
2171                         return;
2172
2173                 if (service->favorite == FALSE)
2174                         return;
2175
2176                 if (is_connected(service) == TRUE)
2177                         return;
2178
2179                 if (is_ignore(service) == FALSE &&
2180                                 service->state == CONNMAN_SERVICE_STATE_IDLE)
2181                         break;
2182
2183                 service = NULL;
2184
2185                 iter = g_sequence_iter_next(iter);
2186         }
2187
2188         if (service != NULL) {
2189                 service->userconnect = FALSE;
2190                 __connman_service_connect(service);
2191         }
2192 }
2193
2194 static void remove_timeout(struct connman_service *service)
2195 {
2196         if (service->timeout > 0) {
2197                 g_source_remove(service->timeout);
2198                 service->timeout = 0;
2199         }
2200 }
2201
2202 static void reply_pending(struct connman_service *service, int error)
2203 {
2204         remove_timeout(service);
2205
2206         if (service->pending != NULL) {
2207                 if (error > 0) {
2208                         DBusMessage *reply;
2209
2210                         reply = __connman_error_failed(service->pending,
2211                                                                 error);
2212                         if (reply != NULL)
2213                                 g_dbus_send_message(connection, reply);
2214                 } else
2215                         g_dbus_send_reply(connection, service->pending,
2216                                                         DBUS_TYPE_INVALID);
2217
2218                 dbus_message_unref(service->pending);
2219                 service->pending = NULL;
2220         }
2221 }
2222
2223 static gboolean connect_timeout(gpointer user_data)
2224 {
2225         struct connman_service *service = user_data;
2226         connman_bool_t autoconnect = FALSE;
2227
2228         DBG("service %p", service);
2229
2230         service->timeout = 0;
2231
2232         if (service->network != NULL)
2233                 __connman_network_disconnect(service->network);
2234
2235         if (service->ipconfig_ipv4)
2236                 if (!__connman_ipconfig_disable(service->ipconfig_ipv4))
2237                         service->ipconfig_ipv4 = NULL;
2238
2239         if (service->ipconfig_ipv6)
2240                 if (!__connman_ipconfig_disable(service->ipconfig_ipv6))
2241                         service->ipconfig_ipv6 = NULL;
2242
2243         __connman_stats_service_unregister(service);
2244
2245         if (service->pending != NULL) {
2246                 DBusMessage *reply;
2247
2248                 reply = __connman_error_operation_timeout(service->pending);
2249                 if (reply != NULL)
2250                         g_dbus_send_message(connection, reply);
2251
2252                 dbus_message_unref(service->pending);
2253                 service->pending = NULL;
2254         } else
2255                 autoconnect = TRUE;
2256
2257         __connman_service_indicate_state(service,
2258                                         CONNMAN_SERVICE_STATE_FAILURE);
2259
2260         if (autoconnect == TRUE && service->userconnect == FALSE)
2261                 __connman_service_auto_connect();
2262
2263         return FALSE;
2264 }
2265
2266 static void set_reconnect_state(struct connman_service *service,
2267                                                 connman_bool_t reconnect)
2268 {
2269         struct connman_device *device;
2270
2271         if (service->network == NULL)
2272                 return;
2273
2274         device = connman_network_get_device(service->network);
2275         if (device == NULL)
2276                 return;
2277
2278         __connman_device_set_reconnect(device, reconnect);
2279 }
2280
2281 static connman_bool_t get_reconnect_state(struct connman_service *service)
2282 {
2283         struct connman_device *device;
2284
2285         if (service->network == NULL)
2286                 return FALSE;
2287
2288         device = connman_network_get_device(service->network);
2289         if (device == NULL)
2290                 return FALSE;
2291
2292         return __connman_device_get_reconnect(device);
2293 }
2294
2295 static void request_input_cb (struct connman_service *service,
2296                         const char *passphrase, void *user_data)
2297 {
2298         DBG ("RequestInput return, %p", service);
2299
2300         if (passphrase == NULL)
2301                 return;
2302         __connman_service_set_passphrase(service, passphrase);
2303         __connman_service_connect(service);
2304 }
2305
2306 struct connman_service *
2307 __connman_service_connect_type(enum connman_service_type type)
2308 {
2309         struct connman_service *service;
2310         GSequenceIter *iter;
2311         int err;
2312
2313         DBG("type %d", type);
2314
2315         /*
2316          * We go through the already sorted service list.
2317          * We pick the first one matching our type, or just
2318          * the first available one if we have no type.
2319          */
2320         iter = g_sequence_get_begin_iter(service_list);
2321         if (g_sequence_iter_is_end(iter))
2322                 return NULL;
2323         service = g_sequence_get(iter);
2324
2325         /*
2326          * If the first service is connected or about to be
2327          * connected, we return it, regardless of the type.
2328          */
2329         if ((g_sequence_iter_is_end(iter) == FALSE) &&
2330                 (is_connecting(service) == TRUE ||
2331                         is_connected(service) == TRUE))
2332                 return service;
2333
2334         while (g_sequence_iter_is_end(iter) == FALSE) {
2335                 if (service->type == type ||
2336                         type == CONNMAN_SERVICE_TYPE_UNKNOWN)
2337                         break;
2338
2339                 iter = g_sequence_iter_next(iter);
2340                 service = g_sequence_get(iter);
2341         }
2342
2343         if (g_sequence_iter_is_end(iter))
2344                 return NULL;
2345
2346         service->ignore = FALSE;
2347
2348         service->userconnect = TRUE;
2349
2350         set_reconnect_state(service, FALSE);
2351
2352         err = __connman_service_connect(service);
2353         if (err < 0) {
2354                 if (err == -ENOKEY)
2355                         if ( __connman_agent_request_input(service,
2356                                                         request_input_cb,
2357                                                         NULL))
2358                                 return service;
2359
2360                 if (err != -EINPROGRESS)
2361                         return NULL;
2362         }
2363
2364         return service;
2365 }
2366
2367 static DBusMessage *connect_service(DBusConnection *conn,
2368                                         DBusMessage *msg, void *user_data)
2369 {
2370         struct connman_service *service = user_data;
2371         GSequenceIter *iter;
2372         int err;
2373
2374         DBG("service %p", service);
2375
2376         if (service->pending != NULL)
2377                 return __connman_error_in_progress(msg);
2378
2379         iter = g_sequence_get_begin_iter(service_list);
2380
2381         while (g_sequence_iter_is_end(iter) == FALSE) {
2382                 struct connman_service *temp = g_sequence_get(iter);
2383
2384                 if (service->type == temp->type &&
2385                                         is_connecting(temp) == TRUE)
2386                         return __connman_error_in_progress(msg);
2387
2388                 iter = g_sequence_iter_next(iter);
2389         }
2390
2391         service->ignore = FALSE;
2392
2393         service->userconnect = TRUE;
2394
2395         service->pending = dbus_message_ref(msg);
2396
2397         set_reconnect_state(service, FALSE);
2398
2399         err = __connman_service_connect(service);
2400         if (err < 0) {
2401                 if (err == -ENOKEY) {
2402                         if (__connman_agent_request_input(service,
2403                                                         request_input_cb,
2404                                                         NULL) == 0)
2405                                 return NULL;
2406                 }
2407
2408                 if (err != -EINPROGRESS) {
2409                         dbus_message_unref(service->pending);
2410                         service->pending = NULL;
2411
2412                         return __connman_error_failed(msg, -err);
2413                 }
2414
2415                 return NULL;
2416         }
2417
2418         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2419 }
2420
2421 static DBusMessage *disconnect_service(DBusConnection *conn,
2422                                         DBusMessage *msg, void *user_data)
2423 {
2424         struct connman_service *service = user_data;
2425         int err;
2426
2427         DBG("service %p", service);
2428
2429         reply_pending(service, ECONNABORTED);
2430
2431         service->ignore = TRUE;
2432
2433         set_reconnect_state(service, FALSE);
2434
2435         err = __connman_service_disconnect(service);
2436         if (err < 0) {
2437                 if (err != -EINPROGRESS)
2438                         return __connman_error_failed(msg, -err);
2439
2440                 return NULL;
2441         }
2442
2443         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2444 }
2445
2446 static DBusMessage *remove_service(DBusConnection *conn,
2447                                         DBusMessage *msg, void *user_data)
2448 {
2449         struct connman_service *service = user_data;
2450
2451         DBG("service %p", service);
2452
2453         if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2454                 return __connman_error_not_supported(msg);
2455
2456         if (service->immutable == TRUE)
2457                 return __connman_error_not_supported(msg);
2458
2459         if (service->favorite == FALSE &&
2460                         service->state != CONNMAN_SERVICE_STATE_FAILURE)
2461                 return __connman_error_not_supported(msg);
2462
2463         if (service->network != NULL) {
2464                 set_reconnect_state(service, FALSE);
2465
2466                 __connman_network_disconnect(service->network);
2467         }
2468
2469         g_hash_table_destroy(service->counter_table);
2470
2471         g_free(service->passphrase);
2472         service->passphrase = NULL;
2473
2474         passphrase_changed(service);
2475
2476         g_free(service->apn);
2477         service->apn = NULL;
2478
2479         g_free(service->username);
2480         service->username = NULL;
2481
2482         g_free(service->password);
2483         service->password = NULL;
2484
2485         apn_changed(service);
2486
2487         set_idle(service);
2488
2489         __connman_service_set_favorite(service, FALSE);
2490         __connman_storage_save_service(service);
2491
2492         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2493 }
2494
2495 static DBusMessage *move_service(DBusConnection *conn,
2496                                         DBusMessage *msg, void *user_data,
2497                                                                 gboolean before)
2498 {
2499         struct connman_service *service = user_data;
2500         struct connman_service *target;
2501         const char *path;
2502         GSequenceIter *src, *dst;
2503
2504         DBG("service %p", service);
2505
2506         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2507                                                         DBUS_TYPE_INVALID);
2508
2509         if (service->favorite == FALSE)
2510                 return __connman_error_not_supported(msg);
2511
2512         target = find_service(path);
2513         if (target == NULL || target->favorite == FALSE || target == service)
2514                 return __connman_error_invalid_service(msg);
2515
2516         DBG("target %s", target->identifier);
2517
2518         if (target->state != service->state)
2519                 return __connman_error_invalid_service(msg);
2520
2521         g_get_current_time(&service->modified);
2522         __connman_storage_save_service(service);
2523
2524         src = g_hash_table_lookup(service_hash, service->identifier);
2525         dst = g_hash_table_lookup(service_hash, target->identifier);
2526
2527         before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
2528
2529         __connman_profile_changed(FALSE);
2530
2531         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2532 }
2533
2534 static DBusMessage *move_before(DBusConnection *conn,
2535                                         DBusMessage *msg, void *user_data)
2536 {
2537         return move_service(conn, msg, user_data, TRUE);
2538 }
2539
2540 static DBusMessage *move_after(DBusConnection *conn,
2541                                         DBusMessage *msg, void *user_data)
2542 {
2543         return move_service(conn, msg, user_data, FALSE);
2544 }
2545
2546 static DBusMessage *reset_counters(DBusConnection *conn,
2547                                         DBusMessage *msg, void *user_data)
2548 {
2549         struct connman_service *service = user_data;
2550
2551         reset_stats(service);
2552
2553         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2554 }
2555
2556 static GDBusMethodTable service_methods[] = {
2557         { "GetProperties", "",   "a{sv}", get_properties     },
2558         { "SetProperty",   "sv", "",      set_property       },
2559         { "ClearProperty", "s",  "",      clear_property     },
2560         { "Connect",       "",   "",      connect_service,
2561                                                 G_DBUS_METHOD_FLAG_ASYNC },
2562         { "Disconnect",    "",   "",      disconnect_service },
2563         { "Remove",        "",   "",      remove_service     },
2564         { "MoveBefore",    "o",  "",      move_before        },
2565         { "MoveAfter",     "o",  "",      move_after         },
2566         { "ResetCounters", "",   "",      reset_counters     },
2567         { },
2568 };
2569
2570 static GDBusSignalTable service_signals[] = {
2571         { "PropertyChanged", "sv" },
2572         { },
2573 };
2574
2575 static void service_free(gpointer user_data)
2576 {
2577         struct connman_service *service = user_data;
2578         char *path = service->path;
2579
2580         DBG("service %p", service);
2581
2582         reply_pending(service, ENOENT);
2583
2584         g_hash_table_remove(service_hash, service->identifier);
2585
2586         stats_stop(service);
2587         __connman_storage_save_service(service);
2588
2589         service->path = NULL;
2590
2591         if (path != NULL) {
2592                 __connman_profile_changed(FALSE);
2593
2594                 g_dbus_unregister_interface(connection, path,
2595                                                 CONNMAN_SERVICE_INTERFACE);
2596                 g_free(path);
2597         }
2598
2599         if (service->network != NULL)
2600                 connman_network_unref(service->network);
2601
2602         if (service->provider != NULL)
2603                 connman_provider_unref(service->provider);
2604
2605         if (service->ipconfig_ipv4 != NULL) {
2606                 connman_ipconfig_unref(service->ipconfig_ipv4);
2607                 service->ipconfig_ipv4 = NULL;
2608         }
2609
2610         if (service->ipconfig_ipv6 != NULL) {
2611                 connman_ipconfig_unref(service->ipconfig_ipv6);
2612                 service->ipconfig_ipv6 = NULL;
2613         }
2614
2615         if (service->location != NULL)
2616                 connman_location_unref(service->location);
2617
2618         g_strfreev(service->nameservers);
2619         g_strfreev(service->domains);
2620         g_strfreev(service->proxies);
2621         g_strfreev(service->excludes);
2622
2623         g_free(service->nameserver);
2624         g_free(service->domainname);
2625         g_free(service->pac);
2626         g_free(service->mcc);
2627         g_free(service->mnc);
2628         g_free(service->apn);
2629         g_free(service->username);
2630         g_free(service->password);
2631         g_free(service->profile);
2632         g_free(service->name);
2633         g_free(service->passphrase);
2634         g_free(service->identifier);
2635         g_free(service->eap);
2636         g_free(service->identity);
2637         g_free(service->ca_cert_file);
2638         g_free(service->client_cert_file);
2639         g_free(service->private_key_file);
2640         g_free(service->private_key_passphrase);
2641         g_free(service->phase2);
2642
2643         if (service->stats.timer != NULL)
2644                 g_timer_destroy(service->stats.timer);
2645         if (service->stats_roaming.timer != NULL)
2646                 g_timer_destroy(service->stats_roaming.timer);
2647
2648         g_free(service);
2649 }
2650
2651 /**
2652  * __connman_service_put:
2653  * @service: service structure
2654  *
2655  * Release service if no longer needed
2656  */
2657 void __connman_service_put(struct connman_service *service)
2658 {
2659         DBG("service %p", service);
2660
2661         if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
2662                 GSequenceIter *iter;
2663
2664                 iter = g_hash_table_lookup(service_hash, service->identifier);
2665                 if (iter != NULL) {
2666                         reply_pending(service, ECONNABORTED);
2667
2668                         __connman_service_disconnect(service);
2669
2670                         g_sequence_remove(iter);
2671                 } else
2672                         service_free(service);
2673         }
2674 }
2675
2676 static void stats_init(struct connman_service *service)
2677 {
2678         /* home */
2679         service->stats.valid = FALSE;
2680         service->stats.enabled = FALSE;
2681         service->stats.timer = g_timer_new();
2682
2683         /* roaming */
2684         service->stats_roaming.valid = FALSE;
2685         service->stats_roaming.enabled = FALSE;
2686         service->stats_roaming.timer = g_timer_new();
2687 }
2688
2689 static void service_initialize(struct connman_service *service)
2690 {
2691         DBG("service %p", service);
2692
2693         service->refcount = 1;
2694
2695         service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
2696         service->mode     = CONNMAN_SERVICE_MODE_UNKNOWN;
2697         service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
2698         service->state    = CONNMAN_SERVICE_STATE_UNKNOWN;
2699
2700         service->favorite  = FALSE;
2701         service->immutable = FALSE;
2702         service->hidden = FALSE;
2703
2704         service->ignore = FALSE;
2705
2706         service->userconnect = FALSE;
2707
2708         service->order = 0;
2709
2710         stats_init(service);
2711
2712         service->provider = NULL;
2713 }
2714
2715 /**
2716  * connman_service_create:
2717  *
2718  * Allocate a new service.
2719  *
2720  * Returns: a newly-allocated #connman_service structure
2721  */
2722 struct connman_service *connman_service_create(void)
2723 {
2724         GSList *list;
2725         struct connman_stats_counter *counters;
2726         const char *counter;
2727
2728         struct connman_service *service;
2729
2730         service = g_try_new0(struct connman_service, 1);
2731         if (service == NULL)
2732                 return NULL;
2733
2734         DBG("service %p", service);
2735
2736         service->counter_table = g_hash_table_new_full(g_str_hash,
2737                                                 g_str_equal, NULL, g_free);
2738
2739         for (list = counter_list; list; list = list->next) {
2740                 counter = list->data;
2741
2742                 counters = g_try_new0(struct connman_stats_counter, 1);
2743                 if (counters == NULL) {
2744                         g_hash_table_destroy(service->counter_table);
2745                         g_free(service);
2746                         return NULL;
2747                 }
2748
2749                 counters->append_all = TRUE;
2750
2751                 g_hash_table_replace(service->counter_table, (gpointer)counter,
2752                                 counters);
2753         }
2754
2755         service_initialize(service);
2756
2757         service->location = __connman_location_create(service);
2758
2759         return service;
2760 }
2761
2762 struct connman_location *__connman_service_get_location(struct connman_service *service)
2763 {
2764         return service->location;
2765 }
2766
2767 /**
2768  * connman_service_ref:
2769  * @service: service structure
2770  *
2771  * Increase reference counter of service
2772  */
2773 struct connman_service *connman_service_ref(struct connman_service *service)
2774 {
2775         DBG("%p", service);
2776
2777         g_atomic_int_inc(&service->refcount);
2778
2779         return service;
2780 }
2781
2782 /**
2783  * connman_service_unref:
2784  * @service: service structure
2785  *
2786  * Decrease reference counter of service
2787  */
2788 void connman_service_unref(struct connman_service *service)
2789 {
2790         __connman_service_put(service);
2791 }
2792
2793 static gint service_compare(gconstpointer a, gconstpointer b,
2794                                                         gpointer user_data)
2795 {
2796         struct connman_service *service_a = (void *) a;
2797         struct connman_service *service_b = (void *) b;
2798
2799         if (service_a->state != service_b->state) {
2800                 if (is_connected(service_a) == TRUE)
2801                         return -1;
2802                 if (is_connected(service_b) == TRUE)
2803                         return 1;
2804
2805                 if (is_connecting(service_a) == TRUE)
2806                         return -1;
2807                 if (is_connecting(service_b) == TRUE)
2808                         return 1;
2809         }
2810
2811         if (service_a->order > service_b->order)
2812                 return -1;
2813
2814         if (service_a->order < service_b->order)
2815                 return 1;
2816
2817         if (service_a->favorite == TRUE && service_b->favorite == FALSE)
2818                 return -1;
2819
2820         if (service_a->favorite == FALSE && service_b->favorite == TRUE)
2821                 return 1;
2822
2823         if (service_a->type != service_b->type) {
2824                 switch (service_a->type) {
2825                 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2826                 case CONNMAN_SERVICE_TYPE_SYSTEM:
2827                 case CONNMAN_SERVICE_TYPE_ETHERNET:
2828                 case CONNMAN_SERVICE_TYPE_GPS:
2829                 case CONNMAN_SERVICE_TYPE_VPN:
2830                 case CONNMAN_SERVICE_TYPE_GADGET:
2831                         break;
2832                 case CONNMAN_SERVICE_TYPE_WIFI:
2833                         return 1;
2834                 case CONNMAN_SERVICE_TYPE_WIMAX:
2835                 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2836                 case CONNMAN_SERVICE_TYPE_CELLULAR:
2837                         return -1;
2838                 }
2839         }
2840
2841         return (gint) service_b->strength - (gint) service_a->strength;
2842 }
2843
2844 /**
2845  * connman_service_get_type:
2846  * @service: service structure
2847  *
2848  * Get the type of service
2849  */
2850 enum connman_service_type connman_service_get_type(struct connman_service *service)
2851 {
2852         if (service == NULL)
2853                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
2854
2855         return service->type;
2856 }
2857
2858 /**
2859  * connman_service_get_interface:
2860  * @service: service structure
2861  *
2862  * Get network interface of service
2863  */
2864 char *connman_service_get_interface(struct connman_service *service)
2865 {
2866         int index;
2867
2868         if (service == NULL)
2869                 return NULL;
2870
2871         if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
2872                 if (service->ipconfig_ipv4)
2873                         index = connman_ipconfig_get_index(
2874                                                 service->ipconfig_ipv4);
2875                 else if (service->ipconfig_ipv6)
2876                         index = connman_ipconfig_get_index(
2877                                                 service->ipconfig_ipv6);
2878                 else
2879                         return NULL;
2880
2881                 return connman_inet_ifname(index);
2882         }
2883
2884         if (service->network == NULL)
2885                 return NULL;
2886
2887         index = connman_network_get_index(service->network);
2888
2889         return connman_inet_ifname(index);
2890 }
2891
2892 /**
2893  * connman_service_get_network:
2894  * @service: service structure
2895  *
2896  * Get the service network
2897  */
2898 struct connman_network *
2899 __connman_service_get_network(struct connman_service *service)
2900 {
2901         if (service == NULL)
2902                 return NULL;
2903
2904         return service->network;
2905 }
2906
2907 struct connman_ipconfig *
2908 __connman_service_get_ip4config(struct connman_service *service)
2909 {
2910         if (service == NULL)
2911                 return NULL;
2912
2913         return service->ipconfig_ipv4;
2914 }
2915
2916 struct connman_ipconfig *
2917 __connman_service_get_ip6config(struct connman_service *service)
2918 {
2919         if (service == NULL)
2920                 return NULL;
2921
2922         return service->ipconfig_ipv6;
2923 }
2924
2925 enum connman_service_security __connman_service_get_security(struct connman_service *service)
2926 {
2927         if (service == NULL)
2928                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
2929
2930         return service->security;
2931 }
2932
2933 /**
2934  * __connman_service_set_favorite:
2935  * @service: service structure
2936  * @favorite: favorite value
2937  *
2938  * Change the favorite setting of service
2939  */
2940 int __connman_service_set_favorite(struct connman_service *service,
2941                                                 connman_bool_t favorite)
2942 {
2943         GSequenceIter *iter;
2944
2945         iter = g_hash_table_lookup(service_hash, service->identifier);
2946         if (iter == NULL)
2947                 return -ENOENT;
2948
2949         if (service->favorite == favorite)
2950                 return -EALREADY;
2951
2952         service->favorite = favorite;
2953
2954         favorite_changed(service);
2955
2956         g_sequence_sort_changed(iter, service_compare, NULL);
2957
2958         __connman_profile_changed(FALSE);
2959
2960         return 0;
2961 }
2962
2963 int __connman_service_set_immutable(struct connman_service *service,
2964                                                 connman_bool_t immutable)
2965 {
2966         service->immutable = immutable;
2967
2968         immutable_changed(service);
2969
2970         return 0;
2971 }
2972
2973 void __connman_service_set_string(struct connman_service *service,
2974                                   const char *key, const char *value)
2975 {
2976         if (g_str_equal(key, "EAP") == TRUE) {
2977                 g_free(service->eap);
2978                 service->eap = g_strdup(value);
2979         } else if (g_str_equal(key, "Identity") == TRUE) {
2980                 g_free(service->identity);
2981                 service->identity = g_strdup(value);
2982         } else if (g_str_equal(key, "CACertFile") == TRUE) {
2983                 g_free(service->ca_cert_file);
2984                 service->ca_cert_file = g_strdup(value);
2985         } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
2986                 g_free(service->client_cert_file);
2987                 service->client_cert_file = g_strdup(value);
2988         } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
2989                 g_free(service->private_key_file);
2990                 service->private_key_file = g_strdup(value);
2991         } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
2992                 g_free(service->private_key_passphrase);
2993                 service->private_key_passphrase = g_strdup(value);
2994         } else if (g_str_equal(key, "Phase2") == TRUE) {
2995                 g_free(service->phase2);
2996                 service->phase2 = g_strdup(value);
2997         } else if (g_str_equal(key, "Passphrase") == TRUE) {
2998                 g_free(service->passphrase);
2999                 service->passphrase = g_strdup(value);
3000         }
3001 }
3002
3003 static void service_complete(struct connman_service *service)
3004 {
3005         reply_pending(service, EIO);
3006
3007         if (service->userconnect == FALSE)
3008                 __connman_service_auto_connect();
3009
3010         g_get_current_time(&service->modified);
3011         __connman_storage_save_service(service);
3012 }
3013
3014 static void report_error_cb(struct connman_service *service,
3015                         gboolean retry, void *user_data)
3016 {
3017         if (retry == TRUE)
3018                 __connman_service_connect(service);
3019         else {
3020                 service_complete(service);
3021                 __connman_profile_changed(FALSE);
3022                 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3023         }
3024 }
3025
3026 int __connman_service_indicate_state(struct connman_service *service,
3027                                         enum connman_service_state state)
3028 {
3029         GSequenceIter *iter;
3030
3031         DBG("service %p state %d", service, state);
3032
3033         if (service == NULL)
3034                 return -EINVAL;
3035
3036         if (service->state == state)
3037                 return -EALREADY;
3038
3039         if (service->state == CONNMAN_SERVICE_STATE_FAILURE &&
3040                                 state == CONNMAN_SERVICE_STATE_IDLE)
3041                 return -EINVAL;
3042
3043         if (service->state == CONNMAN_SERVICE_STATE_IDLE &&
3044                                 state == CONNMAN_SERVICE_STATE_DISCONNECT)
3045                 return -EINVAL;
3046
3047         if (state == CONNMAN_SERVICE_STATE_IDLE &&
3048                         service->state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3049                 service->state = CONNMAN_SERVICE_STATE_DISCONNECT;
3050                 state_changed(service);
3051
3052                 reply_pending(service, ECONNABORTED);
3053
3054                 __connman_service_disconnect(service);
3055         }
3056
3057         if (state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3058                 if (__connman_stats_service_register(service) == 0) {
3059                         __connman_stats_get(service, FALSE,
3060                                                 &service->stats.data);
3061                         __connman_stats_get(service, TRUE,
3062                                                 &service->stats_roaming.data);
3063                 }
3064
3065                 if (service->ipconfig_ipv4)
3066                         __connman_ipconfig_enable(service->ipconfig_ipv4);
3067                 if (service->ipconfig_ipv6)
3068                         __connman_ipconfig_enable(service->ipconfig_ipv6);
3069         }
3070
3071         service->state = state;
3072         state_changed(service);
3073
3074         if (state == CONNMAN_SERVICE_STATE_ONLINE) {
3075                 if (service->login_required == TRUE) {
3076                         service->login_required = FALSE;
3077                         login_changed(service);
3078                 }
3079
3080                 connman_timeserver_sync();
3081         }
3082
3083         if (state == CONNMAN_SERVICE_STATE_IDLE) {
3084                 connman_bool_t reconnect;
3085
3086                 reconnect = get_reconnect_state(service);
3087                 if (reconnect == TRUE)
3088                         __connman_service_auto_connect();
3089         }
3090
3091         if (state == CONNMAN_SERVICE_STATE_READY) {
3092                 set_reconnect_state(service, TRUE);
3093
3094                 __connman_service_set_favorite(service, TRUE);
3095
3096                 reply_pending(service, 0);
3097
3098                 service->userconnect = FALSE;
3099
3100                 g_get_current_time(&service->modified);
3101                 __connman_storage_save_service(service);
3102
3103                 update_nameservers(service);
3104                 dns_changed(service);
3105                 domain_changed(service);
3106
3107                 __connman_wpad_start(service);
3108
3109                 __connman_notifier_connect(service->type);
3110
3111                 default_changed();
3112         } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3113                 __connman_location_finish(service);
3114
3115                 default_changed();
3116
3117                 __connman_wpad_stop(service);
3118
3119                 update_nameservers(service);
3120                 dns_changed(service);
3121                 domain_changed(service);
3122
3123                 __connman_notifier_disconnect(service->type);
3124         }
3125
3126         if (state == CONNMAN_SERVICE_STATE_FAILURE) {
3127                 if (service->userconnect == TRUE &&
3128                         __connman_agent_report_error(service,
3129                                         error2string(service->error),
3130                                         report_error_cb, NULL) == -EIO)
3131                         return 0;
3132                 service_complete(service);
3133         } else
3134                 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3135
3136         iter = g_hash_table_lookup(service_hash, service->identifier);
3137         if (iter != NULL)
3138                 g_sequence_sort_changed(iter, service_compare, NULL);
3139
3140         __connman_profile_changed(FALSE);
3141
3142         if (service->state == CONNMAN_SERVICE_STATE_ONLINE)
3143                 default_changed();
3144
3145         if (service->state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3146                 struct connman_service *def_service = get_default();
3147
3148                 if (__connman_notifier_count_connected() == 0 &&
3149                         def_service != NULL &&
3150                                 def_service->provider != NULL)
3151                         __connman_provider_disconnect(def_service->provider);
3152         }
3153
3154         if (service->state == CONNMAN_SERVICE_STATE_IDLE ||
3155                         service->state == CONNMAN_SERVICE_STATE_FAILURE)
3156                 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3157
3158         return 0;
3159 }
3160
3161 int __connman_service_indicate_error(struct connman_service *service,
3162                                         enum connman_service_error error)
3163 {
3164         DBG("service %p error %d", service, error);
3165
3166         if (service == NULL)
3167                 return -EINVAL;
3168
3169         service->error = error;
3170
3171         return __connman_service_indicate_state(service,
3172                                         CONNMAN_SERVICE_STATE_FAILURE);
3173 }
3174
3175 int __connman_service_indicate_default(struct connman_service *service)
3176 {
3177         DBG("service %p", service);
3178
3179         default_changed();
3180
3181         __connman_location_detect(service);
3182
3183         return 0;
3184 }
3185
3186 int __connman_service_request_login(struct connman_service *service)
3187 {
3188         DBG("service %p", service);
3189
3190         if (service == NULL)
3191                 return -EINVAL;
3192
3193         service->login_required = TRUE;
3194         login_changed(service);
3195
3196         return 0;
3197 }
3198
3199 static connman_bool_t prepare_network(struct connman_service *service)
3200 {
3201         enum connman_network_type type;
3202         unsigned int ssid_len;
3203
3204         type = connman_network_get_type(service->network);
3205
3206         switch (type) {
3207         case CONNMAN_NETWORK_TYPE_UNKNOWN:
3208         case CONNMAN_NETWORK_TYPE_VENDOR:
3209                 return FALSE;
3210         case CONNMAN_NETWORK_TYPE_WIFI:
3211                 if (connman_network_get_blob(service->network, "WiFi.SSID",
3212                                                         &ssid_len) == NULL)
3213                         return FALSE;
3214
3215                 connman_network_set_string(service->network,
3216                                 "WiFi.Passphrase", service->passphrase);
3217                 break;
3218         case CONNMAN_NETWORK_TYPE_ETHERNET:
3219         case CONNMAN_NETWORK_TYPE_WIMAX:
3220         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3221         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3222                 break;
3223         case CONNMAN_NETWORK_TYPE_CELLULAR:
3224                 connman_network_set_string(service->network,
3225                                                 "Cellular.APN", service->apn);
3226
3227                 connman_network_set_string(service->network,
3228                                         "Cellular.Username", service->username);
3229                 connman_network_set_string(service->network,
3230                                         "Cellular.Password", service->password);
3231                 break;
3232         }
3233
3234         return TRUE;
3235 }
3236
3237 static void prepare_8021x(struct connman_service *service)
3238 {
3239         if (service->eap != NULL)
3240                 connman_network_set_string(service->network, "WiFi.EAP",
3241                                                                 service->eap);
3242
3243         if (service->identity != NULL)
3244                 connman_network_set_string(service->network, "WiFi.Identity",
3245                                                         service->identity);
3246
3247         if (service->ca_cert_file != NULL)
3248                 connman_network_set_string(service->network, "WiFi.CACertFile",
3249                                                         service->ca_cert_file);
3250
3251         if (service->client_cert_file != NULL)
3252                 connman_network_set_string(service->network,
3253                                                 "WiFi.ClientCertFile",
3254                                                 service->client_cert_file);
3255
3256         if (service->private_key_file != NULL)
3257                 connman_network_set_string(service->network,
3258                                                 "WiFi.PrivateKeyFile",
3259                                                 service->private_key_file);
3260
3261         if (service->private_key_passphrase != NULL)
3262                 connman_network_set_string(service->network,
3263                                         "WiFi.PrivateKeyPassphrase",
3264                                         service->private_key_passphrase);
3265
3266         if (service->phase2 != NULL)
3267                 connman_network_set_string(service->network, "WiFi.Phase2",
3268                                                         service->phase2);
3269 }
3270
3271 int __connman_service_connect(struct connman_service *service)
3272 {
3273         int err;
3274
3275         DBG("service %p", service);
3276
3277         if (is_connected(service) == TRUE)
3278                 return -EISCONN;
3279
3280         if (is_connecting(service) == TRUE)
3281                 return -EALREADY;
3282
3283         switch (service->type) {
3284         case CONNMAN_SERVICE_TYPE_UNKNOWN:
3285         case CONNMAN_SERVICE_TYPE_SYSTEM:
3286         case CONNMAN_SERVICE_TYPE_GPS:
3287         case CONNMAN_SERVICE_TYPE_GADGET:
3288                 return -EINVAL;
3289         case CONNMAN_SERVICE_TYPE_ETHERNET:
3290         case CONNMAN_SERVICE_TYPE_WIMAX:
3291         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3292         case CONNMAN_SERVICE_TYPE_VPN:
3293                 break;
3294         case CONNMAN_SERVICE_TYPE_CELLULAR:
3295                 if (service->apn == NULL)
3296                         return -EINVAL;
3297                 break;
3298         case CONNMAN_SERVICE_TYPE_WIFI:
3299                 switch (service->security) {
3300                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3301                 case CONNMAN_SERVICE_SECURITY_NONE:
3302                         break;
3303                 case CONNMAN_SERVICE_SECURITY_WEP:
3304                 case CONNMAN_SERVICE_SECURITY_PSK:
3305                 case CONNMAN_SERVICE_SECURITY_WPA:
3306                 case CONNMAN_SERVICE_SECURITY_RSN:
3307                         if (service->passphrase == NULL)
3308                                 return -ENOKEY;
3309                         break;
3310                 case CONNMAN_SERVICE_SECURITY_8021X:
3311                         break;
3312                 }
3313                 break;
3314         }
3315
3316         if (service->network != NULL) {
3317                 if (prepare_network(service) == FALSE)
3318                         return -EINVAL;
3319
3320                 switch (service->security) {
3321                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3322                 case CONNMAN_SERVICE_SECURITY_NONE:
3323                 case CONNMAN_SERVICE_SECURITY_WEP:
3324                 case CONNMAN_SERVICE_SECURITY_PSK:
3325                 case CONNMAN_SERVICE_SECURITY_WPA:
3326                 case CONNMAN_SERVICE_SECURITY_RSN:
3327                         break;
3328                 case CONNMAN_SERVICE_SECURITY_8021X:
3329                         prepare_8021x(service);
3330                         break;
3331                 }
3332
3333                 if (__connman_stats_service_register(service) == 0) {
3334                         __connman_stats_get(service, FALSE,
3335                                                 &service->stats.data);
3336                         __connman_stats_get(service, TRUE,
3337                                                 &service->stats_roaming.data);
3338                 }
3339
3340                 if (service->ipconfig_ipv4)
3341                         __connman_ipconfig_enable(service->ipconfig_ipv4);
3342                 if (service->ipconfig_ipv6)
3343                         __connman_ipconfig_enable(service->ipconfig_ipv6);
3344
3345                 err = __connman_network_connect(service->network);
3346         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3347                                         service->provider != NULL)
3348                 err = __connman_provider_connect(service->provider);
3349         else
3350                 return -EOPNOTSUPP;
3351
3352         if (err < 0) {
3353                 if (err != -EINPROGRESS) {
3354                         if (service->ipconfig_ipv4)
3355                                 if (!__connman_ipconfig_disable(
3356                                                     service->ipconfig_ipv4))
3357                                         service->ipconfig_ipv4 = NULL;
3358
3359                         if (service->ipconfig_ipv6)
3360                                 if (!__connman_ipconfig_disable(
3361                                                     service->ipconfig_ipv6))
3362                                         service->ipconfig_ipv6 = NULL;
3363
3364                         __connman_stats_service_unregister(service);
3365                         if (service->userconnect == TRUE)
3366                                 return __connman_agent_report_error(service,
3367                                                 error2string(service->error),
3368                                                 report_error_cb, NULL);
3369                         else
3370                                 return err;
3371                 }
3372
3373                 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
3374                                                 connect_timeout, service);
3375
3376                 return -EINPROGRESS;
3377         }
3378
3379         return 0;
3380 }
3381
3382 int __connman_service_disconnect(struct connman_service *service)
3383 {
3384         int err;
3385
3386         DBG("service %p", service);
3387
3388         if (service->network != NULL) {
3389                 err = __connman_network_disconnect(service->network);
3390         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3391                                         service->provider != NULL)
3392                 err = __connman_provider_disconnect(service->provider);
3393         else
3394                 return -EOPNOTSUPP;
3395
3396         if (service->ipconfig_ipv4)
3397                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
3398                                                         NULL);
3399         else
3400                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
3401                                                         NULL);
3402
3403         __connman_ipconfig_clear_address(service->ipconfig_ipv4);
3404         __connman_ipconfig_clear_address(service->ipconfig_ipv6);
3405
3406         if (__connman_ipconfig_disable(service->ipconfig_ipv4) < 0)
3407                 service->ipconfig_ipv4 = NULL;
3408
3409         if (__connman_ipconfig_disable(service->ipconfig_ipv6) < 0)
3410                 service->ipconfig_ipv6 = NULL;
3411
3412         __connman_stats_service_unregister(service);
3413
3414         if (err < 0) {
3415                 if (err != -EINPROGRESS)
3416                         return err;
3417
3418                 return -EINPROGRESS;
3419         }
3420
3421         return 0;
3422 }
3423
3424 /**
3425  * __connman_service_lookup:
3426  * @pattern: search pattern
3427  * @path: return object path
3428  *
3429  * Look up a service path from a search pattern
3430  */
3431 int __connman_service_lookup(const char *pattern, const char **path)
3432 {
3433         GHashTableIter iter;
3434         gpointer key, value;
3435
3436         g_hash_table_iter_init(&iter, service_hash);
3437
3438         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
3439                 GSequenceIter *iter = value;
3440                 struct connman_service *service = g_sequence_get(iter);
3441
3442                 if (g_strcmp0(service->identifier, pattern) == 0 ||
3443                                 g_strcmp0(service->name, pattern) == 0) {
3444                         *path = (const char *) service->path;
3445                         return 0;
3446                 }
3447         }
3448
3449         return -ENXIO;
3450 }
3451
3452 /**
3453  * lookup_by_identifier:
3454  * @identifier: service identifier
3455  *
3456  * Look up a service by identifier (reference count will not be increased)
3457  */
3458 static struct connman_service *lookup_by_identifier(const char *identifier)
3459 {
3460         GSequenceIter *iter;
3461
3462         iter = g_hash_table_lookup(service_hash, identifier);
3463         if (iter != NULL)
3464                 return g_sequence_get(iter);
3465
3466         return NULL;
3467 }
3468
3469 static struct connman_network *create_hidden_wifi(struct connman_device *device,
3470                 const char *ssid, const char *mode, const char *security)
3471 {
3472         struct connman_network *network;
3473         char *name;
3474         int index;
3475         unsigned int i, ssid_len;
3476
3477         ssid_len = strlen(ssid);
3478         if (ssid_len < 1)
3479                 return NULL;
3480
3481         network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
3482         if (network == NULL)
3483                 return NULL;
3484
3485         connman_network_set_blob(network, "WiFi.SSID",
3486                                         (unsigned char *) ssid, ssid_len);
3487
3488         connman_network_set_string(network, "WiFi.Mode", mode);
3489         connman_network_set_string(network, "WiFi.Security", security);
3490
3491         name = g_try_malloc0(ssid_len + 1);
3492         if (name == NULL) {
3493                 connman_network_unref(network);
3494                 return NULL;
3495         }
3496
3497         for (i = 0; i < ssid_len; i++) {
3498                 if (g_ascii_isprint(ssid[i]))
3499                         name[i] = ssid[i];
3500                 else
3501                         name[i] = ' ';
3502         }
3503
3504         connman_network_set_name(network, name);
3505
3506         g_free(name);
3507
3508         index = connman_device_get_index(device);
3509         connman_network_set_index(network, index);
3510
3511         if (connman_device_add_network(device, network) < 0) {
3512                 connman_network_unref(network);
3513                 return NULL;
3514         }
3515
3516         connman_network_set_available(network, TRUE);
3517
3518         return network;
3519 }
3520
3521 int __connman_service_create_and_connect(DBusMessage *msg)
3522 {
3523         struct connman_service *service;
3524         struct connman_network *network;
3525         struct connman_device *device;
3526         DBusMessageIter iter, array;
3527         const char *mode = "managed", *security = "none", *group_security;
3528         const char *type = NULL, *ssid = NULL, *passphrase = NULL;
3529         unsigned int ssid_len = 0;
3530         const char *ident;
3531         char *name, *group;
3532         gboolean created = FALSE;
3533         int err;
3534
3535         dbus_message_iter_init(msg, &iter);
3536         dbus_message_iter_recurse(&iter, &array);
3537
3538         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
3539                 DBusMessageIter entry, value;
3540                 const char *key;
3541
3542                 dbus_message_iter_recurse(&array, &entry);
3543                 dbus_message_iter_get_basic(&entry, &key);
3544
3545                 dbus_message_iter_next(&entry);
3546                 dbus_message_iter_recurse(&entry, &value);
3547
3548                 switch (dbus_message_iter_get_arg_type(&value)) {
3549                 case DBUS_TYPE_STRING:
3550                         if (g_str_equal(key, "Type") == TRUE)
3551                                 dbus_message_iter_get_basic(&value, &type);
3552                         else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
3553                                         g_str_equal(key, "Mode") == TRUE)
3554                                 dbus_message_iter_get_basic(&value, &mode);
3555                         else if (g_str_equal(key, "WiFi.Security") == TRUE ||
3556                                         g_str_equal(key, "Security") == TRUE)
3557                                 dbus_message_iter_get_basic(&value, &security);
3558                         else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
3559                                         g_str_equal(key, "Passphrase") == TRUE)
3560                                 dbus_message_iter_get_basic(&value, &passphrase);
3561                         else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
3562                                         g_str_equal(key, "SSID") == TRUE)
3563                                 dbus_message_iter_get_basic(&value, &ssid);
3564                 }
3565
3566                 dbus_message_iter_next(&array);
3567         }
3568
3569         if (type == NULL)
3570                 return -EINVAL;
3571
3572         if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
3573                 return -EOPNOTSUPP;
3574
3575         if (ssid == NULL)
3576                 return -EINVAL;
3577
3578         ssid_len = strlen(ssid);
3579         if (ssid_len < 1)
3580                 return -EINVAL;
3581
3582         if (g_strcmp0(security, "none") != 0 &&
3583                                 g_strcmp0(security, "wep") != 0 &&
3584                                 g_strcmp0(security, "psk") != 0 &&
3585                                 g_strcmp0(security, "wpa") != 0 &&
3586                                 g_strcmp0(security, "rsn") != 0 &&
3587                                 g_strcmp0(security, "ieee8021x") != 0)
3588                 return -EINVAL;
3589
3590         device = __connman_element_find_device(CONNMAN_SERVICE_TYPE_WIFI);
3591         if (device == NULL)
3592                 return -EOPNOTSUPP;
3593
3594         ident = connman_device_get_ident(device);
3595         if (ident == NULL)
3596                 return -EOPNOTSUPP;
3597
3598
3599         if (!g_strcmp0(security, "wpa") ||
3600                 !g_strcmp0(security, "rsn"))
3601                 group_security = "psk";
3602         else
3603                 group_security = security;
3604
3605         group = connman_wifi_build_group_name((unsigned char *) ssid,
3606                                                 ssid_len, mode, group_security);
3607         if (group == NULL)
3608                 return -EINVAL;
3609
3610         name = g_strdup_printf("%s_%s_%s", type, ident, group);
3611
3612         service = lookup_by_identifier(name);
3613
3614         if (service != NULL)
3615                 goto done;
3616
3617         network = create_hidden_wifi(device, ssid, mode, security);
3618         if (network != NULL) {
3619                 connman_network_set_group(network, group);
3620                 created = TRUE;
3621         }
3622
3623         service = lookup_by_identifier(name);
3624
3625 done:
3626         g_free(name);
3627         g_free(group);
3628
3629         if (service == NULL) {
3630                 err = -EOPNOTSUPP;
3631                 goto failed;
3632         }
3633
3634         set_reconnect_state(service, FALSE);
3635
3636         __connman_device_disconnect(device);
3637
3638         if (passphrase != NULL) {
3639                 g_free(service->passphrase);
3640                 service->passphrase = g_strdup(passphrase);
3641         }
3642
3643         service->userconnect = TRUE;
3644
3645         err = __connman_service_connect(service);
3646         if (err < 0 && err != -EINPROGRESS)
3647                 goto failed;
3648
3649         g_dbus_send_reply(connection, msg,
3650                                 DBUS_TYPE_OBJECT_PATH, &service->path,
3651                                                         DBUS_TYPE_INVALID);
3652
3653         return 0;
3654
3655 failed:
3656         if (service != NULL && created == TRUE) {
3657                 struct connman_network *network = service->network;
3658
3659                 if (network != NULL) {
3660                         connman_network_set_available(network, FALSE);
3661                         __connman_device_cleanup_networks(device);
3662                 } else
3663                         __connman_service_put(service);
3664         }
3665
3666         return err;
3667 }
3668
3669 /**
3670  * __connman_service_get:
3671  * @identifier: service identifier
3672  *
3673  * Look up a service by identifier or create a new one if not found
3674  */
3675 static struct connman_service *service_get(const char *identifier)
3676 {
3677         struct connman_service *service;
3678         GSequenceIter *iter;
3679
3680         iter = g_hash_table_lookup(service_hash, identifier);
3681         if (iter != NULL) {
3682                 service = g_sequence_get(iter);
3683                 if (service != NULL)
3684                         connman_service_ref(service);
3685                 return service;
3686         }
3687
3688         service = connman_service_create();
3689         if (service == NULL)
3690                 return NULL;
3691
3692         DBG("service %p", service);
3693
3694         service->identifier = g_strdup(identifier);
3695
3696         service->profile = g_strdup(__connman_profile_active_ident());
3697
3698         iter = g_sequence_insert_sorted(service_list, service,
3699                                                 service_compare, NULL);
3700
3701         g_hash_table_insert(service_hash, service->identifier, iter);
3702
3703         return service;
3704 }
3705
3706 static int service_register(struct connman_service *service)
3707 {
3708         const char *path = __connman_profile_active_path();
3709         GSequenceIter *iter;
3710
3711         DBG("service %p", service);
3712
3713         if (service->path != NULL)
3714                 return -EALREADY;
3715
3716         service->path = g_strdup_printf("%s/%s", path, service->identifier);
3717
3718         DBG("path %s", service->path);
3719
3720         __connman_config_provision_service(service);
3721
3722         __connman_storage_load_service(service);
3723
3724         g_dbus_register_interface(connection, service->path,
3725                                         CONNMAN_SERVICE_INTERFACE,
3726                                         service_methods, service_signals,
3727                                                         NULL, service, NULL);
3728
3729         iter = g_hash_table_lookup(service_hash, service->identifier);
3730         if (iter != NULL)
3731                 g_sequence_sort_changed(iter, service_compare, NULL);
3732
3733         __connman_profile_changed(TRUE);
3734
3735         return 0;
3736 }
3737
3738 static void service_up(struct connman_ipconfig *ipconfig)
3739 {
3740         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3741
3742         connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
3743
3744         link_changed(service);
3745
3746         service->stats.valid = FALSE;
3747         service->stats_roaming.valid = FALSE;
3748 }
3749
3750 static void service_down(struct connman_ipconfig *ipconfig)
3751 {
3752         connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
3753 }
3754
3755 static void service_lower_up(struct connman_ipconfig *ipconfig)
3756 {
3757         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3758
3759         connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
3760
3761         stats_start(service);
3762 }
3763
3764 static void service_lower_down(struct connman_ipconfig *ipconfig)
3765 {
3766         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3767
3768         connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
3769
3770         stats_stop(service);
3771         __connman_storage_save_service(service);
3772 }
3773
3774 static void service_ip_bound(struct connman_ipconfig *ipconfig)
3775 {
3776         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3777
3778         connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
3779
3780         settings_changed(service);
3781 }
3782
3783 static void service_ip_release(struct connman_ipconfig *ipconfig)
3784 {
3785         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
3786
3787         connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
3788
3789         settings_changed(service);
3790 }
3791
3792 static const struct connman_ipconfig_ops service_ops = {
3793         .up             = service_up,
3794         .down           = service_down,
3795         .lower_up       = service_lower_up,
3796         .lower_down     = service_lower_down,
3797         .ip_bound       = service_ip_bound,
3798         .ip_release     = service_ip_release,
3799 };
3800
3801 static void setup_ip4config(struct connman_service *service, int index)
3802 {
3803         if (index < 0)
3804                 return;
3805
3806         service->ipconfig_ipv4 = connman_ipconfig_create(index,
3807                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
3808         if (service->ipconfig_ipv4 == NULL)
3809                 return;
3810
3811         connman_ipconfig_set_method(service->ipconfig_ipv4,
3812                                         CONNMAN_IPCONFIG_METHOD_DHCP);
3813
3814         connman_ipconfig_set_data(service->ipconfig_ipv4, service);
3815
3816         connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
3817 }
3818
3819 static void setup_ip6config(struct connman_service *service, int index)
3820 {
3821         if (index < 0)
3822                 return;
3823
3824         service->ipconfig_ipv6 = connman_ipconfig_create(index,
3825                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
3826         if (service->ipconfig_ipv6 == NULL)
3827                 return;
3828
3829         connman_ipconfig_set_method(service->ipconfig_ipv6,
3830                                         CONNMAN_IPCONFIG_METHOD_OFF);
3831
3832         connman_ipconfig_set_data(service->ipconfig_ipv6, service);
3833
3834         connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
3835 }
3836
3837 void __connman_service_create_ip4config(struct connman_service *service,
3838                                                                 int index)
3839 {
3840         const char *ident = service->profile;
3841         GKeyFile *keyfile;
3842
3843         DBG("ipv4 %p", service->ipconfig_ipv4);
3844
3845         if (service->ipconfig_ipv4 != NULL)
3846                 return;
3847
3848         setup_ip4config(service, index);
3849
3850         if (ident == NULL)
3851                 return;
3852
3853         keyfile = __connman_storage_open_profile(ident);
3854         if (keyfile == NULL)
3855                 return;
3856
3857         if (service->ipconfig_ipv4)
3858                 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
3859                                         service->identifier, "IPv4.");
3860         g_key_file_free(keyfile);
3861 }
3862
3863 void __connman_service_create_ip6config(struct connman_service *service,
3864                                                                 int index)
3865 {
3866         const char *ident = service->profile;
3867         GKeyFile *keyfile;
3868
3869         DBG("ipv6 %p", service->ipconfig_ipv6);
3870
3871         if (service->ipconfig_ipv6 != NULL)
3872                 return;
3873
3874         setup_ip6config(service, index);
3875
3876         if (ident == NULL)
3877                 return;
3878
3879         keyfile = __connman_storage_open_profile(ident);
3880         if (keyfile == NULL)
3881                 return;
3882
3883         if (service->ipconfig_ipv6 != NULL)
3884                 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
3885                                         service->identifier, "IPv6.");
3886
3887         g_key_file_free(keyfile);
3888 }
3889
3890 /**
3891  * __connman_service_lookup_from_network:
3892  * @network: network structure
3893  *
3894  * Look up a service by network (reference count will not be increased)
3895  */
3896 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
3897 {
3898         struct connman_service *service;
3899         const char *ident, *group;
3900         char *name;
3901
3902         DBG("network %p", network);
3903
3904         ident = __connman_network_get_ident(network);
3905         if (ident == NULL)
3906                 return NULL;
3907
3908         group = connman_network_get_group(network);
3909         if (group == NULL)
3910                 return NULL;
3911
3912         name = g_strdup_printf("%s_%s_%s",
3913                         __connman_network_get_type(network), ident, group);
3914         service = lookup_by_identifier(name);
3915         g_free(name);
3916
3917         return service;
3918 }
3919
3920 struct connman_service *__connman_service_lookup_from_index(int index)
3921 {
3922         struct connman_service *service;
3923         GSequenceIter *iter;
3924
3925         iter = g_sequence_get_begin_iter(service_list);
3926
3927         while (g_sequence_iter_is_end(iter) == FALSE) {
3928                 service = g_sequence_get(iter);
3929
3930                 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
3931                                                         == index)
3932                         return service;
3933
3934                 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
3935                                                         == index)
3936                         return service;
3937
3938                 iter = g_sequence_iter_next(iter);
3939         }
3940
3941         return NULL;
3942 }
3943
3944 const char *__connman_service_get_ident(struct connman_service *service)
3945 {
3946         return service->identifier;
3947 }
3948
3949 const char *__connman_service_get_path(struct connman_service *service)
3950 {
3951         return service->path;
3952 }
3953
3954 unsigned int __connman_service_get_order(struct connman_service *service)
3955 {
3956         GSequenceIter *iter;
3957
3958         if (service == NULL)
3959                 return 0;
3960
3961         if (service->favorite == FALSE) {
3962                 service->order = 0;
3963                 goto done;
3964         }
3965
3966         iter = g_hash_table_lookup(service_hash, service->identifier);
3967         if (iter != NULL) {
3968                 if (g_sequence_iter_get_position(iter) == 0)
3969                         service->order = 1;
3970                 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
3971                         service->order = 10;
3972                 else
3973                         service->order = 0;
3974         }
3975
3976 done:
3977         return service->order;
3978 }
3979
3980 static enum connman_service_type convert_network_type(struct connman_network *network)
3981 {
3982         enum connman_network_type type = connman_network_get_type(network);
3983
3984         switch (type) {
3985         case CONNMAN_NETWORK_TYPE_UNKNOWN:
3986         case CONNMAN_NETWORK_TYPE_VENDOR:
3987                 break;
3988         case CONNMAN_NETWORK_TYPE_ETHERNET:
3989                 return CONNMAN_SERVICE_TYPE_ETHERNET;
3990         case CONNMAN_NETWORK_TYPE_WIFI:
3991                 return CONNMAN_SERVICE_TYPE_WIFI;
3992         case CONNMAN_NETWORK_TYPE_WIMAX:
3993                 return CONNMAN_SERVICE_TYPE_WIMAX;
3994         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3995         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3996                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
3997         case CONNMAN_NETWORK_TYPE_CELLULAR:
3998                 return CONNMAN_SERVICE_TYPE_CELLULAR;
3999         }
4000
4001         return CONNMAN_SERVICE_TYPE_UNKNOWN;
4002 }
4003
4004 static enum connman_service_mode convert_wifi_mode(const char *mode)
4005 {
4006         if (mode == NULL)
4007                 return CONNMAN_SERVICE_MODE_UNKNOWN;
4008         else if (g_str_equal(mode, "managed") == TRUE)
4009                 return CONNMAN_SERVICE_MODE_MANAGED;
4010         else if (g_str_equal(mode, "adhoc") == TRUE)
4011                 return CONNMAN_SERVICE_MODE_ADHOC;
4012         else
4013                 return CONNMAN_SERVICE_MODE_UNKNOWN;
4014 }
4015
4016 static enum connman_service_security convert_wifi_security(const char *security)
4017 {
4018         if (security == NULL)
4019                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4020         else if (g_str_equal(security, "none") == TRUE)
4021                 return CONNMAN_SERVICE_SECURITY_NONE;
4022         else if (g_str_equal(security, "wep") == TRUE)
4023                 return CONNMAN_SERVICE_SECURITY_WEP;
4024         else if (g_str_equal(security, "psk") == TRUE)
4025                 return CONNMAN_SERVICE_SECURITY_PSK;
4026         else if (g_str_equal(security, "ieee8021x") == TRUE)
4027                 return CONNMAN_SERVICE_SECURITY_8021X;
4028         else if (g_str_equal(security, "wpa") == TRUE)
4029                 return CONNMAN_SERVICE_SECURITY_WPA;
4030         else if (g_str_equal(security, "rsn") == TRUE)
4031                 return CONNMAN_SERVICE_SECURITY_RSN;
4032         else
4033                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4034 }
4035
4036 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
4037 {
4038         switch (mode) {
4039         case 0:
4040         case 1:
4041                 return CONNMAN_SERVICE_MODE_GPRS;
4042         case 3:
4043                 return CONNMAN_SERVICE_MODE_EDGE;
4044         case 2:
4045         case 4:
4046         case 5:
4047         case 6:
4048                 return CONNMAN_SERVICE_MODE_UMTS;
4049         }
4050
4051         return CONNMAN_SERVICE_MODE_UNKNOWN;
4052 }
4053
4054 static void update_from_network(struct connman_service *service,
4055                                         struct connman_network *network)
4056 {
4057         connman_uint8_t strength = service->strength;
4058         GSequenceIter *iter;
4059         const char *str;
4060
4061         DBG("service %p network %p", service, network);
4062
4063         if (is_connected(service) == TRUE)
4064                 return;
4065
4066         if (is_connecting(service) == TRUE)
4067                 return;
4068
4069         str = connman_network_get_string(network, "Name");
4070         if (str != NULL) {
4071                 g_free(service->name);
4072                 service->name = g_strdup(str);
4073                 service->hidden = FALSE;
4074         } else {
4075                 g_free(service->name);
4076                 service->name = NULL;
4077                 service->hidden = TRUE;
4078         }
4079
4080         service->strength = connman_network_get_uint8(network, "Strength");
4081         service->roaming = connman_network_get_bool(network, "Roaming");
4082
4083         if (service->strength == 0) {
4084                 /*
4085                  * Filter out 0-values; it's unclear what they mean
4086                  * and they cause anomalous sorting of the priority list.
4087                  */
4088                 service->strength = strength;
4089         }
4090
4091         str = connman_network_get_string(network, "WiFi.Mode");
4092         service->mode = convert_wifi_mode(str);
4093
4094         str = connman_network_get_string(network, "WiFi.Security");
4095         service->security = convert_wifi_security(str);
4096
4097         str = connman_network_get_string(network, "Cellular.MCC");
4098         g_free(service->mcc);
4099         service->mcc = g_strdup(str);
4100
4101         str = connman_network_get_string(network, "Cellular.MNC");
4102         g_free(service->mnc);
4103         service->mnc = g_strdup(str);
4104
4105         if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
4106                 connman_uint8_t value = connman_network_get_uint8(network,
4107                                                         "Cellular.Mode");
4108
4109                 service->mode = convert_cellular_mode(value);
4110         }
4111
4112         if (service->strength > strength && service->network != NULL) {
4113                 connman_network_unref(service->network);
4114                 service->network = connman_network_ref(network);
4115
4116                 strength_changed(service);
4117         }
4118
4119         if (service->network == NULL)
4120                 service->network = connman_network_ref(network);
4121
4122         iter = g_hash_table_lookup(service_hash, service->identifier);
4123         if (iter != NULL)
4124                 g_sequence_sort_changed(iter, service_compare, NULL);
4125 }
4126
4127 /**
4128  * __connman_service_create_from_network:
4129  * @network: network structure
4130  *
4131  * Look up service by network and if not found, create one
4132  */
4133 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4134 {
4135         struct connman_service *service;
4136         struct connman_device *device;
4137         const char *ident, *group;
4138         char *name;
4139         int index;
4140
4141         DBG("network %p", network);
4142
4143         ident = __connman_network_get_ident(network);
4144         if (ident == NULL)
4145                 return NULL;
4146
4147         group = connman_network_get_group(network);
4148         if (group == NULL)
4149                 return NULL;
4150
4151         name = g_strdup_printf("%s_%s_%s",
4152                         __connman_network_get_type(network), ident, group);
4153         service = service_get(name);
4154         g_free(name);
4155
4156         if (service == NULL)
4157                 return NULL;
4158
4159         if (__connman_network_get_weakness(network) == TRUE)
4160                 return service;
4161
4162         if (service->path != NULL) {
4163                 update_from_network(service, network);
4164                 __connman_profile_changed(TRUE);
4165                 return service;
4166         }
4167
4168         service->type = convert_network_type(network);
4169
4170         switch (service->type) {
4171         case CONNMAN_SERVICE_TYPE_UNKNOWN:
4172         case CONNMAN_SERVICE_TYPE_SYSTEM:
4173         case CONNMAN_SERVICE_TYPE_ETHERNET:
4174         case CONNMAN_SERVICE_TYPE_WIMAX:
4175         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4176         case CONNMAN_SERVICE_TYPE_GPS:
4177         case CONNMAN_SERVICE_TYPE_VPN:
4178         case CONNMAN_SERVICE_TYPE_GADGET:
4179                 service->autoconnect = FALSE;
4180                 break;
4181         case CONNMAN_SERVICE_TYPE_WIFI:
4182         case CONNMAN_SERVICE_TYPE_CELLULAR:
4183                 service->autoconnect = TRUE;
4184                 break;
4185         }
4186
4187         service->state = CONNMAN_SERVICE_STATE_IDLE;
4188
4189         update_from_network(service, network);
4190
4191         index = connman_network_get_index(network);
4192         setup_ip4config(service, index);
4193         setup_ip6config(service, index);
4194
4195         service_register(service);
4196
4197         if (service->favorite == TRUE) {
4198                 device = connman_network_get_device(service->network);
4199                 if (device && __connman_device_scanning(device) == FALSE)
4200                         __connman_service_auto_connect();
4201         }
4202
4203         return service;
4204 }
4205
4206 void __connman_service_update_from_network(struct connman_network *network)
4207 {
4208         struct connman_service *service;
4209         enum connman_service_mode mode;
4210         connman_uint8_t strength, value;
4211         connman_bool_t roaming;
4212         GSequenceIter *iter;
4213         const char *name;
4214         connman_bool_t stats_enable;
4215
4216         DBG("network %p", network);
4217
4218         service = __connman_service_lookup_from_network(network);
4219         if (service == NULL)
4220                 return;
4221
4222         if (service->network == NULL)
4223                 return;
4224
4225         name = connman_network_get_string(service->network, "Name");
4226         if (g_strcmp0(service->name, name) != 0) {
4227                 g_free(service->name);
4228                 service->name = g_strdup(name);
4229                 connman_dbus_property_changed_basic(service->path,
4230                                 CONNMAN_SERVICE_INTERFACE, "Name",
4231                                 DBUS_TYPE_STRING, &service->name);
4232         }
4233
4234         strength = connman_network_get_uint8(service->network, "Strength");
4235         if (strength == service->strength)
4236                 goto roaming;
4237
4238         service->strength = strength;
4239
4240         strength_changed(service);
4241
4242 roaming:
4243         roaming = connman_network_get_bool(service->network, "Roaming");
4244         if (roaming == service->roaming)
4245                 goto done;
4246
4247         stats_enable = stats_enabled(service);
4248         if (stats_enable == TRUE)
4249                 stats_stop(service);
4250
4251         service->roaming = roaming;
4252
4253         if (stats_enable == TRUE)
4254                 stats_start(service);
4255
4256         roaming_changed(service);
4257
4258         iter = g_hash_table_lookup(service_hash, service->identifier);
4259         if (iter != NULL)
4260                 g_sequence_sort_changed(iter, service_compare, NULL);
4261
4262 done:
4263         if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
4264                 return;
4265
4266         value = connman_network_get_uint8(service->network, "Cellular.Mode");
4267         mode = convert_cellular_mode(value);
4268
4269         if (mode == service->mode)
4270                 return;
4271
4272         service->mode = mode;
4273
4274         mode_changed(service);
4275 }
4276
4277 void __connman_service_remove_from_network(struct connman_network *network)
4278 {
4279         struct connman_service *service;
4280
4281         DBG("network %p", network);
4282
4283         service = __connman_service_lookup_from_network(network);
4284         if (service == NULL)
4285                 return;
4286
4287         __connman_service_put(service);
4288 }
4289
4290 /**
4291  * __connman_service_create_from_provider:
4292  * @provider: provider structure
4293  *
4294  * Look up service by provider and if not found, create one
4295  */
4296 struct connman_service *
4297 __connman_service_create_from_provider(struct connman_provider *provider)
4298 {
4299         struct connman_service *service;
4300         const char *ident, *str;
4301         char *name;
4302         int index = connman_provider_get_index(provider);
4303
4304         DBG("provider %p", provider);
4305
4306         ident = __connman_provider_get_ident(provider);
4307         if (ident == NULL)
4308                 return NULL;
4309
4310         name = g_strdup_printf("vpn_%s", ident);
4311         service = service_get(name);
4312         g_free(name);
4313
4314         if (service == NULL)
4315                 return NULL;
4316
4317         service->type = CONNMAN_SERVICE_TYPE_VPN;
4318         service->provider = connman_provider_ref(provider);
4319         service->autoconnect = FALSE;
4320
4321         service->state = CONNMAN_SERVICE_STATE_IDLE;
4322
4323         str = connman_provider_get_string(provider, "Name");
4324         if (str != NULL) {
4325                 g_free(service->name);
4326                 service->name = g_strdup(str);
4327                 service->hidden = FALSE;
4328         } else {
4329                 g_free(service->name);
4330                 service->name = NULL;
4331                 service->hidden = TRUE;
4332         }
4333
4334         service->strength = 0;
4335
4336         service->ipconfig_ipv4 = connman_ipconfig_create(index,
4337                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
4338         if (service->ipconfig_ipv4 == NULL)
4339                 return service;
4340
4341         connman_ipconfig_set_method(service->ipconfig_ipv4,
4342                                         CONNMAN_IPCONFIG_METHOD_MANUAL);
4343         connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4344         connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4345
4346         service->ipconfig_ipv6 = connman_ipconfig_create(index,
4347                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
4348         if (service->ipconfig_ipv6 == NULL)
4349                 return service;
4350
4351         connman_ipconfig_set_method(service->ipconfig_ipv6,
4352                                         CONNMAN_IPCONFIG_METHOD_OFF);
4353         connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4354         connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4355
4356         service_register(service);
4357
4358         return service;
4359 }
4360
4361 static int service_load(struct connman_service *service)
4362 {
4363         const char *ident = service->profile;
4364         GKeyFile *keyfile;
4365         GError *error = NULL;
4366         gchar *pathname, *data = NULL;
4367         gsize length;
4368         gchar *str;
4369         connman_bool_t autoconnect;
4370         unsigned int ssid_len;
4371         int err = 0;
4372
4373         DBG("service %p", service);
4374
4375         if (ident == NULL)
4376                 return -EINVAL;
4377
4378         pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
4379         if (pathname == NULL)
4380                 return -ENOMEM;
4381
4382         keyfile = g_key_file_new();
4383
4384         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
4385                 g_free(pathname);
4386                 return -ENOENT;
4387         }
4388
4389         g_free(pathname);
4390
4391         if (g_key_file_load_from_data(keyfile, data, length,
4392                                                         0, NULL) == FALSE) {
4393                 g_free(data);
4394                 return -EILSEQ;
4395         }
4396
4397         g_free(data);
4398
4399         switch (service->type) {
4400         case CONNMAN_SERVICE_TYPE_UNKNOWN:
4401         case CONNMAN_SERVICE_TYPE_SYSTEM:
4402         case CONNMAN_SERVICE_TYPE_ETHERNET:
4403         case CONNMAN_SERVICE_TYPE_GPS:
4404         case CONNMAN_SERVICE_TYPE_VPN:
4405         case CONNMAN_SERVICE_TYPE_GADGET:
4406                 break;
4407         case CONNMAN_SERVICE_TYPE_WIFI:
4408                 if (service->name == NULL) {
4409                         gchar *name;
4410
4411                         name = g_key_file_get_string(keyfile,
4412                                         service->identifier, "Name", NULL);
4413                         if (name != NULL) {
4414                                 g_free(service->name);
4415                                 service->name = name;
4416                         }
4417
4418                         if (service->network != NULL)
4419                                 connman_network_set_name(service->network,
4420                                                                         name);
4421                 }
4422
4423                 if (service->network &&
4424                                 connman_network_get_blob(service->network,
4425                                         "WiFi.SSID", &ssid_len) == NULL) {
4426                         gchar *hex_ssid;
4427
4428                         hex_ssid = g_key_file_get_string(keyfile,
4429                                                         service->identifier,
4430                                                                 "SSID", NULL);
4431
4432                         if (hex_ssid != NULL) {
4433                                 gchar *ssid;
4434                                 unsigned int i, j = 0, hex;
4435                                 size_t hex_ssid_len = strlen(hex_ssid);
4436
4437                                 ssid = g_try_malloc0(hex_ssid_len / 2);
4438                                 if (ssid == NULL) {
4439                                         g_free(hex_ssid);
4440                                         err = -ENOMEM;
4441                                         goto done;
4442                                 }
4443
4444                                 for (i = 0; i < hex_ssid_len; i += 2) {
4445                                         sscanf(hex_ssid + i, "%02x", &hex);
4446                                         ssid[j++] = hex;
4447                                 }
4448
4449                                 connman_network_set_blob(service->network,
4450                                         "WiFi.SSID", ssid, hex_ssid_len / 2);
4451                         }
4452
4453                         g_free(hex_ssid);
4454                 }
4455                 /* fall through */
4456
4457         case CONNMAN_SERVICE_TYPE_WIMAX:
4458         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4459         case CONNMAN_SERVICE_TYPE_CELLULAR:
4460                 service->apn = g_key_file_get_string(keyfile,
4461                                         service->identifier, "APN", NULL);
4462
4463                 service->username = g_key_file_get_string(keyfile,
4464                                         service->identifier, "Username", NULL);
4465
4466                 service->password = g_key_file_get_string(keyfile,
4467                                         service->identifier, "Password", NULL);
4468
4469                 service->favorite = g_key_file_get_boolean(keyfile,
4470                                 service->identifier, "Favorite", NULL);
4471
4472                 autoconnect = g_key_file_get_boolean(keyfile,
4473                                 service->identifier, "AutoConnect", &error);
4474                 if (error == NULL)
4475                         service->autoconnect = autoconnect;
4476                 g_clear_error(&error);
4477
4478                 str = g_key_file_get_string(keyfile,
4479                                 service->identifier, "Failure", NULL);
4480                 if (str != NULL) {
4481                         if (service->favorite == FALSE)
4482                                 service->state = CONNMAN_SERVICE_STATE_FAILURE;
4483                         service->error = string2error(str);
4484                 }
4485                 break;
4486         }
4487
4488         str = g_key_file_get_string(keyfile,
4489                                 service->identifier, "Modified", NULL);
4490         if (str != NULL) {
4491                 g_time_val_from_iso8601(str, &service->modified);
4492                 g_free(str);
4493         }
4494
4495         str = g_key_file_get_string(keyfile,
4496                                 service->identifier, "Passphrase", NULL);
4497         if (str != NULL) {
4498                 g_free(service->passphrase);
4499                 service->passphrase = str;
4500         }
4501
4502         if (service->ipconfig_ipv4 != NULL)
4503                 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4504                                         service->identifier, "IPv4.");
4505
4506         if (service->ipconfig_ipv6 != NULL)
4507                 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4508                                         service->identifier, "IPv6.");
4509
4510         service->nameservers = g_key_file_get_string_list(keyfile,
4511                         service->identifier, "Nameservers", &length, NULL);
4512         if (service->nameservers != NULL && length == 0) {
4513                 g_strfreev(service->nameservers);
4514                 service->nameservers = NULL;
4515         }
4516
4517         service->domains = g_key_file_get_string_list(keyfile,
4518                         service->identifier, "Domains", &length, NULL);
4519         if (service->domains != NULL && length == 0) {
4520                 g_strfreev(service->domains);
4521                 service->domains = NULL;
4522         }
4523
4524         str = g_key_file_get_string(keyfile,
4525                                 service->identifier, "Proxy.Method", NULL);
4526         service->proxy = string2proxymethod(str);
4527
4528         service->proxies = g_key_file_get_string_list(keyfile,
4529                         service->identifier, "Proxy.Servers", &length, NULL);
4530         if (service->proxies != NULL && length == 0) {
4531                 g_strfreev(service->proxies);
4532                 service->proxies = NULL;
4533         }
4534
4535         service->excludes = g_key_file_get_string_list(keyfile,
4536                         service->identifier, "Proxy.Excludes", &length, NULL);
4537         if (service->excludes != NULL && length == 0) {
4538                 g_strfreev(service->excludes);
4539                 service->excludes = NULL;
4540         }
4541
4542         str = g_key_file_get_string(keyfile,
4543                                 service->identifier, "Proxy.URL", NULL);
4544         if (str != NULL) {
4545                 g_free(service->pac);
4546                 service->pac = str;
4547         }
4548
4549 done:
4550         g_key_file_free(keyfile);
4551
4552         return err;
4553 }
4554
4555 static int service_save(struct connman_service *service)
4556 {
4557         const char *ident = service->profile;
4558         GKeyFile *keyfile;
4559         gchar *pathname, *data = NULL;
4560         gsize length;
4561         gchar *str;
4562         const char *cst_str = NULL;
4563         int err = 0;
4564
4565         DBG("service %p", service);
4566
4567         if (ident == NULL)
4568                 return -EINVAL;
4569
4570         pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
4571         if (pathname == NULL)
4572                 return -ENOMEM;
4573
4574         keyfile = g_key_file_new();
4575
4576         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
4577                 goto update;
4578
4579         if (length > 0) {
4580                 if (g_key_file_load_from_data(keyfile, data, length,
4581                                                         0, NULL) == FALSE)
4582                         goto done;
4583         }
4584
4585         g_free(data);
4586
4587 update:
4588         if (service->name != NULL)
4589                 g_key_file_set_string(keyfile, service->identifier,
4590                                                 "Name", service->name);
4591
4592         switch (service->type) {
4593         case CONNMAN_SERVICE_TYPE_UNKNOWN:
4594         case CONNMAN_SERVICE_TYPE_SYSTEM:
4595         case CONNMAN_SERVICE_TYPE_ETHERNET:
4596         case CONNMAN_SERVICE_TYPE_GPS:
4597         case CONNMAN_SERVICE_TYPE_VPN:
4598         case CONNMAN_SERVICE_TYPE_GADGET:
4599                 break;
4600         case CONNMAN_SERVICE_TYPE_WIFI:
4601                 if (service->network) {
4602                         const unsigned char *ssid;
4603                         unsigned int ssid_len = 0;
4604
4605                         ssid = connman_network_get_blob(service->network,
4606                                                         "WiFi.SSID", &ssid_len);
4607
4608                         if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
4609                                 char *identifier = service->identifier;
4610                                 GString *str;
4611                                 unsigned int i;
4612
4613                                 str = g_string_sized_new(ssid_len * 2);
4614                                 if (str == NULL) {
4615                                         err = -ENOMEM;
4616                                         goto done;
4617                                 }
4618
4619                                 for (i = 0; i < ssid_len; i++)
4620                                         g_string_append_printf(str,
4621                                                         "%02x", ssid[i]);
4622
4623                                 g_key_file_set_string(keyfile, identifier,
4624                                                         "SSID", str->str);
4625
4626                                 g_string_free(str, TRUE);
4627                         }
4628                 }
4629                 /* fall through */
4630
4631         case CONNMAN_SERVICE_TYPE_WIMAX:
4632         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4633         case CONNMAN_SERVICE_TYPE_CELLULAR:
4634                 if (service->apn != NULL)
4635                         g_key_file_set_string(keyfile, service->identifier,
4636                                                         "APN", service->apn);
4637
4638                 if (service->username != NULL)
4639                         g_key_file_set_string(keyfile, service->identifier,
4640                                                 "Username", service->username);
4641
4642                 if (service->password != NULL)
4643                         g_key_file_set_string(keyfile, service->identifier,
4644                                                 "Password", service->password);
4645
4646                 g_key_file_set_boolean(keyfile, service->identifier,
4647                                         "Favorite", service->favorite);
4648
4649                 if (service->favorite == TRUE)
4650                         g_key_file_set_boolean(keyfile, service->identifier,
4651                                         "AutoConnect", service->autoconnect);
4652
4653                 if (service->state == CONNMAN_SERVICE_STATE_FAILURE) {
4654                         const char *failure = error2string(service->error);
4655                         if (failure != NULL)
4656                                 g_key_file_set_string(keyfile,
4657                                                         service->identifier,
4658                                                         "Failure", failure);
4659                 } else {
4660                         g_key_file_remove_key(keyfile, service->identifier,
4661                                                         "Failure", NULL);
4662                 }
4663                 break;
4664         }
4665
4666         str = g_time_val_to_iso8601(&service->modified);
4667         if (str != NULL) {
4668                 g_key_file_set_string(keyfile, service->identifier,
4669                                                         "Modified", str);
4670                 g_free(str);
4671         }
4672
4673         if (service->passphrase != NULL && strlen(service->passphrase) > 0)
4674                 g_key_file_set_string(keyfile, service->identifier,
4675                                         "Passphrase", service->passphrase);
4676         else
4677                 g_key_file_remove_key(keyfile, service->identifier,
4678                                                         "Passphrase", NULL);
4679
4680         if (service->ipconfig_ipv4 != NULL)
4681                 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
4682                                         service->identifier, "IPv4.");
4683
4684         if (service->ipconfig_ipv6 != NULL)
4685                 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
4686                                                 service->identifier, "IPv6.");
4687
4688         if (service->nameservers != NULL) {
4689                 guint len = g_strv_length(service->nameservers);
4690
4691                 g_key_file_set_string_list(keyfile, service->identifier,
4692                                                                 "Nameservers",
4693                                 (const gchar **) service->nameservers, len);
4694         } else
4695                 g_key_file_remove_key(keyfile, service->identifier,
4696                                                         "Nameservers", NULL);
4697
4698         if (service->domains != NULL) {
4699                 guint len = g_strv_length(service->domains);
4700
4701                 g_key_file_set_string_list(keyfile, service->identifier,
4702                                                                 "Domains",
4703                                 (const gchar **) service->domains, len);
4704         } else
4705                 g_key_file_remove_key(keyfile, service->identifier,
4706                                                         "Domains", NULL);
4707
4708         cst_str = proxymethod2string(service->proxy);
4709         if (cst_str != NULL)
4710                 g_key_file_set_string(keyfile, service->identifier,
4711                                 "Proxy.Method", cst_str);
4712
4713         if (service->proxies != NULL) {
4714                 guint len = g_strv_length(service->proxies);
4715
4716                 g_key_file_set_string_list(keyfile, service->identifier,
4717                                 "Proxy.Servers",
4718                                 (const gchar **) service->proxies, len);
4719         } else
4720                 g_key_file_remove_key(keyfile, service->identifier,
4721                                                 "Proxy.Servers", NULL);
4722
4723         if (service->excludes != NULL) {
4724                 guint len = g_strv_length(service->excludes);
4725
4726                 g_key_file_set_string_list(keyfile, service->identifier,
4727                                 "Proxy.Excludes",
4728                                 (const gchar **) service->excludes, len);
4729         } else
4730                 g_key_file_remove_key(keyfile, service->identifier,
4731                                                 "Proxy.Excludes", NULL);
4732
4733         if (service->pac != NULL && strlen(service->pac) > 0)
4734                 g_key_file_set_string(keyfile, service->identifier,
4735                                         "Proxy.URL", service->pac);
4736         else
4737                 g_key_file_remove_key(keyfile, service->identifier,
4738                                                         "Proxy.URL", NULL);
4739
4740         data = g_key_file_to_data(keyfile, &length, NULL);
4741
4742         if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
4743                 connman_error("Failed to store service information");
4744
4745 done:
4746         g_free(data);
4747
4748         g_key_file_free(keyfile);
4749
4750         g_free(pathname);
4751
4752         return err;
4753 }
4754
4755 static struct connman_storage service_storage = {
4756         .name           = "service",
4757         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
4758         .service_load   = service_load,
4759         .service_save   = service_save,
4760 };
4761
4762 int __connman_service_init(void)
4763 {
4764         DBG("");
4765
4766         connection = connman_dbus_get_connection();
4767
4768         if (connman_storage_register(&service_storage) < 0)
4769                 connman_error("Failed to register service storage");
4770
4771         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
4772                                                                 NULL, NULL);
4773
4774         service_list = g_sequence_new(service_free);
4775
4776         return 0;
4777 }
4778
4779 void __connman_service_cleanup(void)
4780 {
4781         DBG("");
4782
4783         g_sequence_free(service_list);
4784         service_list = NULL;
4785
4786         g_hash_table_destroy(service_hash);
4787         service_hash = NULL;
4788
4789         g_slist_free(counter_list);
4790         counter_list = NULL;
4791
4792         connman_storage_unregister(&service_storage);
4793
4794         dbus_connection_unref(connection);
4795 }