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