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