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