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