service: Add service setters/getters
[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 GSequence *__connman_service_get_list(struct connman_session *session,
1553                                         service_match_cb service_match)
1554 {
1555         GSequence *list;
1556         GSequenceIter *iter;
1557
1558         list = g_sequence_new(NULL);
1559         if (list == NULL)
1560                 return NULL;
1561
1562         iter = g_sequence_get_begin_iter(service_list);
1563
1564         while (g_sequence_iter_is_end(iter) == FALSE) {
1565                 service = g_sequence_get(iter);
1566
1567                 if (service_match(session, service) == TRUE)
1568                         g_sequence_append(list, service);
1569
1570                 iter = g_sequence_iter_next(iter);
1571         }
1572
1573         return list;
1574 }
1575
1576 connman_bool_t __connman_service_is_connecting(struct connman_service *service)
1577 {
1578         return is_connecting(service);
1579 }
1580
1581 connman_bool_t __connman_service_is_connected(struct connman_service *service)
1582 {
1583         return is_connected(service);
1584 }
1585
1586 const char *__connman_service_get_name(struct connman_service *service)
1587 {
1588         return service->name;
1589 }
1590
1591 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1592                                         struct connman_service *service)
1593 {
1594         dbus_bool_t required;
1595         const char *str;
1596
1597         str = __connman_service_type2string(service->type);
1598         if (str != NULL)
1599                 connman_dbus_dict_append_basic(dict, "Type",
1600                                                 DBUS_TYPE_STRING, &str);
1601
1602         str = mode2string(service->mode);
1603         if (str != NULL)
1604                 connman_dbus_dict_append_basic(dict, "Mode",
1605                                                 DBUS_TYPE_STRING, &str);
1606
1607         connman_dbus_dict_append_array(dict, "Security",
1608                                 DBUS_TYPE_STRING, append_security, service);
1609
1610         str = state2string(combine_state(service->state_ipv4,
1611                                                         service->state_ipv6));
1612         if (str != NULL)
1613                 connman_dbus_dict_append_basic(dict, "State",
1614                                                 DBUS_TYPE_STRING, &str);
1615
1616         str = error2string(service->error);
1617         if (str != NULL)
1618                 connman_dbus_dict_append_basic(dict, "Error",
1619                                                 DBUS_TYPE_STRING, &str);
1620
1621         if (service->strength > 0)
1622                 connman_dbus_dict_append_basic(dict, "Strength",
1623                                         DBUS_TYPE_BYTE, &service->strength);
1624
1625         connman_dbus_dict_append_basic(dict, "Favorite",
1626                                         DBUS_TYPE_BOOLEAN, &service->favorite);
1627
1628         connman_dbus_dict_append_basic(dict, "Immutable",
1629                                         DBUS_TYPE_BOOLEAN, &service->immutable);
1630
1631         if (service->favorite == TRUE)
1632                 connman_dbus_dict_append_basic(dict, "AutoConnect",
1633                                 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1634         else
1635                 connman_dbus_dict_append_basic(dict, "AutoConnect",
1636                                         DBUS_TYPE_BOOLEAN, &service->favorite);
1637
1638         if (service->name != NULL)
1639                 connman_dbus_dict_append_basic(dict, "Name",
1640                                         DBUS_TYPE_STRING, &service->name);
1641
1642         connman_dbus_dict_append_basic(dict, "LoginRequired",
1643                                 DBUS_TYPE_BOOLEAN, &service->login_required);
1644
1645         switch (service->type) {
1646         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1647         case CONNMAN_SERVICE_TYPE_SYSTEM:
1648         case CONNMAN_SERVICE_TYPE_GPS:
1649         case CONNMAN_SERVICE_TYPE_VPN:
1650         case CONNMAN_SERVICE_TYPE_GADGET:
1651                 break;
1652         case CONNMAN_SERVICE_TYPE_CELLULAR:
1653                 connman_dbus_dict_append_basic(dict, "Roaming",
1654                                         DBUS_TYPE_BOOLEAN, &service->roaming);
1655
1656                 required = FALSE;
1657                 connman_dbus_dict_append_basic(dict, "SetupRequired",
1658                                                 DBUS_TYPE_BOOLEAN, &required);
1659                 connman_dbus_dict_append_dict(dict, "Ethernet",
1660                                                 append_ethernet, service);
1661                 break;
1662         case CONNMAN_SERVICE_TYPE_WIFI:
1663                 if (service->passphrase != NULL && limited == FALSE)
1664                         connman_dbus_dict_append_basic(dict, "Passphrase",
1665                                 DBUS_TYPE_STRING, &service->passphrase);
1666
1667                 required = FALSE;
1668
1669                 switch (service->security) {
1670                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1671                 case CONNMAN_SERVICE_SECURITY_NONE:
1672                         break;
1673                 case CONNMAN_SERVICE_SECURITY_WEP:
1674                 case CONNMAN_SERVICE_SECURITY_PSK:
1675                 case CONNMAN_SERVICE_SECURITY_WPA:
1676                 case CONNMAN_SERVICE_SECURITY_RSN:
1677                         if (service->passphrase == NULL)
1678                                 required = TRUE;
1679                         break;
1680                 case CONNMAN_SERVICE_SECURITY_8021X:
1681                         break;
1682                 }
1683
1684                 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1685                                                 DBUS_TYPE_BOOLEAN, &required);
1686
1687                 /* fall through */
1688         case CONNMAN_SERVICE_TYPE_ETHERNET:
1689         case CONNMAN_SERVICE_TYPE_WIMAX:
1690         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1691                 connman_dbus_dict_append_dict(dict, "Ethernet",
1692                                                 append_ethernet, service);
1693                 break;
1694         }
1695
1696         connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1697
1698         connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1699                                                 append_ipv4config, service);
1700
1701         connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1702
1703         connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1704                                                 append_ipv6config, service);
1705
1706         connman_dbus_dict_append_array(dict, "Nameservers",
1707                                 DBUS_TYPE_STRING, append_dns, service);
1708
1709         connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1710                                 DBUS_TYPE_STRING, append_dnsconfig, service);
1711
1712         connman_dbus_dict_append_array(dict, "Domains",
1713                                 DBUS_TYPE_STRING, append_domain, service);
1714
1715         connman_dbus_dict_append_array(dict, "Domains.Configuration",
1716                                 DBUS_TYPE_STRING, append_domainconfig, service);
1717
1718         connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1719
1720         connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1721                                                 append_proxyconfig, service);
1722
1723         connman_dbus_dict_append_dict(dict, "Provider",
1724                                                 append_provider, service);
1725 }
1726
1727 static void append_struct(gpointer value, gpointer user_data)
1728 {
1729         struct connman_service *service = value;
1730         DBusMessageIter *iter = user_data;
1731         DBusMessageIter entry, dict;
1732
1733         if (service->path == NULL || service->hidden == TRUE)
1734                 return;
1735
1736         dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1737
1738         dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1739                                                         &service->path);
1740
1741         connman_dbus_dict_open(&entry, &dict);
1742         append_properties(&dict, TRUE, service);
1743         connman_dbus_dict_close(&entry, &dict);
1744
1745         dbus_message_iter_close_container(iter, &entry);
1746 }
1747
1748 void __connman_service_list_struct(DBusMessageIter *iter)
1749 {
1750         g_sequence_foreach(service_list, append_struct, iter);
1751 }
1752
1753 int __connman_service_get_index(struct connman_service *service)
1754 {
1755         if (service == NULL)
1756                 return -1;
1757
1758         if (service->network != NULL)
1759                 return connman_network_get_index(service->network);
1760         else if (service->provider != NULL)
1761                 return connman_provider_get_index(service->provider);
1762
1763         return -1;
1764 }
1765
1766 void __connman_service_set_domainname(struct connman_service *service,
1767                                                 const char *domainname)
1768 {
1769         if (service == NULL)
1770                 return;
1771
1772         g_free(service->domainname);
1773         service->domainname = g_strdup(domainname);
1774
1775         domain_changed(service);
1776 }
1777
1778 const char *connman_service_get_domainname(struct connman_service *service)
1779 {
1780         if (service == NULL)
1781                 return NULL;
1782
1783         if (service->domains != NULL)
1784                 return service->domains[0];
1785         else
1786                 return service->domainname;
1787 }
1788
1789 char **connman_service_get_nameservers(struct connman_service *service)
1790 {
1791         if (service == NULL)
1792                 return NULL;
1793
1794         if (service->nameservers_config != NULL)
1795                 return service->nameservers_config;
1796         else if (service->nameservers != NULL)
1797                 return service->nameservers;
1798
1799         return NULL;
1800 }
1801
1802 void connman_service_set_proxy_method(struct connman_service *service,
1803                                         enum connman_service_proxy_method method)
1804 {
1805         if (service == NULL)
1806                 return;
1807
1808         service->proxy = method;
1809
1810         proxy_changed(service);
1811
1812         if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
1813                 __connman_notifier_proxy_changed(service);
1814 }
1815
1816 enum connman_service_proxy_method connman_service_get_proxy_method(
1817                                         struct connman_service *service)
1818 {
1819         if (service == NULL)
1820                 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1821
1822         if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
1823                 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
1824                                 service->pac == NULL)
1825                         return service->proxy;
1826
1827                 return service->proxy_config;
1828         }
1829
1830         return service->proxy;
1831 }
1832
1833 char **connman_service_get_proxy_servers(struct connman_service *service)
1834 {
1835         return g_strdupv(service->proxies);
1836 }
1837
1838 char **connman_service_get_proxy_excludes(struct connman_service *service)
1839 {
1840         return g_strdupv(service->excludes);
1841 }
1842
1843 const char *connman_service_get_proxy_url(struct connman_service *service)
1844 {
1845         if (service == NULL)
1846                 return NULL;
1847
1848         return service->pac;
1849 }
1850
1851 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1852                                                         const char *url)
1853 {
1854         if (service == NULL)
1855                 return;
1856
1857         service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
1858
1859         if (service->ipconfig_ipv4) {
1860                 if (__connman_ipconfig_set_proxy_autoconfig(
1861                             service->ipconfig_ipv4, url) < 0)
1862                         return;
1863         } else if (service->ipconfig_ipv6) {
1864                 if (__connman_ipconfig_set_proxy_autoconfig(
1865                             service->ipconfig_ipv6, url) < 0)
1866                         return;
1867         } else
1868                 return;
1869
1870         proxy_changed(service);
1871
1872         __connman_notifier_proxy_changed(service);
1873 }
1874
1875 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1876 {
1877         if (service == NULL)
1878                 return NULL;
1879
1880         if (service->ipconfig_ipv4)
1881                 return __connman_ipconfig_get_proxy_autoconfig(
1882                                                 service->ipconfig_ipv4);
1883         else if (service->ipconfig_ipv6)
1884                 return __connman_ipconfig_get_proxy_autoconfig(
1885                                                 service->ipconfig_ipv6);
1886         return NULL;
1887 }
1888
1889 static void update_timeservers(struct connman_service *service)
1890 {
1891         int i;
1892
1893         if (service->timeservers == NULL)
1894                 return;
1895
1896         switch (combine_state(service->state_ipv4, service->state_ipv6)) {
1897         case CONNMAN_SERVICE_STATE_UNKNOWN:
1898         case CONNMAN_SERVICE_STATE_IDLE:
1899         case CONNMAN_SERVICE_STATE_ASSOCIATION:
1900         case CONNMAN_SERVICE_STATE_CONFIGURATION:
1901                 return;
1902         case CONNMAN_SERVICE_STATE_FAILURE:
1903         case CONNMAN_SERVICE_STATE_DISCONNECT:
1904                 for (i = 0; service->timeservers[i] != NULL; i++)
1905                         connman_timeserver_remove(service->timeservers[i]);
1906                 return;
1907         case CONNMAN_SERVICE_STATE_READY:
1908         case CONNMAN_SERVICE_STATE_ONLINE:
1909                 break;
1910         }
1911
1912         for (i = 0; service->timeservers[i] != NULL; i++)
1913                 connman_timeserver_append(service->timeservers[i]);
1914 }
1915
1916 int __connman_service_timeserver_append(struct connman_service *service,
1917                                                 const char *timeserver)
1918 {
1919         int len;
1920
1921         DBG("service %p timeserver %s", service, timeserver);
1922
1923         if (timeserver == NULL)
1924                 return -EINVAL;
1925
1926         if (service->timeservers != NULL) {
1927                 int i;
1928
1929                 for (i = 0; service->timeservers[i] != NULL; i++)
1930                         if (g_strcmp0(service->timeservers[i], timeserver) == 0)
1931                                 return -EEXIST;
1932
1933                 len = g_strv_length(service->timeservers);
1934                 service->timeservers = g_try_renew(char *, service->timeservers,
1935                                                         len + 2);
1936         } else {
1937                 len = 0;
1938                 service->timeservers = g_try_new0(char *, len + 2);
1939         }
1940
1941         if (service->timeservers == NULL)
1942                 return -ENOMEM;
1943
1944         service->timeservers[len] = g_strdup(timeserver);
1945         service->timeservers[len + 1] = NULL;
1946
1947         update_timeservers(service);
1948
1949         return 0;
1950 }
1951
1952 int __connman_service_timeserver_remove(struct connman_service *service,
1953                                                 const char *timeserver)
1954 {
1955         char **servers;
1956         int len, i, j;
1957
1958         DBG("service %p timeserver %s", service, timeserver);
1959
1960         if (timeserver == NULL)
1961                 return -EINVAL;
1962
1963         if (service->timeservers == NULL)
1964                 return 0;
1965
1966         len = g_strv_length(service->timeservers);
1967         if (len == 1) {
1968                 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
1969                         return 0;
1970
1971                 g_strfreev(service->timeservers);
1972                 service->timeservers = NULL;
1973
1974                 return 0;
1975         }
1976
1977         servers = g_try_new0(char *, len - 1);
1978         if (servers == NULL)
1979                 return -ENOMEM;
1980
1981         for (i = 0, j = 0; i < len; i++) {
1982                 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
1983                         servers[j] = g_strdup(service->timeservers[i]);
1984                         j++;
1985                 }
1986         }
1987         servers[len - 2] = NULL;
1988
1989         g_strfreev(service->timeservers);
1990         service->timeservers = servers;
1991
1992         update_timeservers(service);
1993
1994         return 0;
1995 }
1996
1997 void __connman_service_set_pac(struct connman_service *service,
1998                                         const char *pac)
1999 {
2000         if (pac == NULL)
2001                 return;
2002
2003         g_free(service->pac);
2004         service->pac = g_strdup(pac);
2005 }
2006
2007 void __connman_service_set_passphrase(struct connman_service *service,
2008                                         const char* passphrase)
2009 {
2010         if (service->immutable == TRUE)
2011                 return;
2012
2013         g_free(service->passphrase);
2014         service->passphrase = g_strdup(passphrase);
2015
2016         passphrase_changed(service);
2017
2018         if (service->network != NULL)
2019                 connman_network_set_string(service->network,
2020                                         "WiFi.Passphrase",
2021                                         service->passphrase);
2022
2023         __connman_storage_save_service(service);
2024 }
2025
2026 static DBusMessage *get_properties(DBusConnection *conn,
2027                                         DBusMessage *msg, void *user_data)
2028 {
2029         struct connman_service *service = user_data;
2030         DBusMessage *reply;
2031         DBusMessageIter array, dict;
2032
2033         DBG("service %p", service);
2034
2035         reply = dbus_message_new_method_return(msg);
2036         if (reply == NULL)
2037                 return NULL;
2038
2039         dbus_message_iter_init_append(reply, &array);
2040
2041         connman_dbus_dict_open(&array, &dict);
2042         append_properties(&dict, FALSE, service);
2043         connman_dbus_dict_close(&array, &dict);
2044
2045         return reply;
2046 }
2047
2048 static int update_proxy_configuration(struct connman_service *service,
2049                                 DBusMessageIter *array)
2050 {
2051         DBusMessageIter dict;
2052         enum connman_service_proxy_method method;
2053         GString *servers_str = NULL;
2054         GString *excludes_str = NULL;
2055         const char *url = NULL;
2056
2057         method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2058
2059         dbus_message_iter_recurse(array, &dict);
2060
2061         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2062                 DBusMessageIter entry, variant;
2063                 const char *key;
2064                 int type;
2065
2066                 dbus_message_iter_recurse(&dict, &entry);
2067
2068                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2069                         goto error;
2070
2071                 dbus_message_iter_get_basic(&entry, &key);
2072                 dbus_message_iter_next(&entry);
2073
2074                 if (dbus_message_iter_get_arg_type(&entry) !=
2075                                                         DBUS_TYPE_VARIANT)
2076                         goto error;
2077
2078                 dbus_message_iter_recurse(&entry, &variant);
2079                 type = dbus_message_iter_get_arg_type(&variant);
2080
2081                 if (g_str_equal(key, "Method") == TRUE) {
2082                         const char *val;
2083
2084                         if (type != DBUS_TYPE_STRING)
2085                                 goto error;
2086
2087                         dbus_message_iter_get_basic(&variant, &val);
2088                         method = string2proxymethod(val);
2089                 } else if (g_str_equal(key, "URL") == TRUE) {
2090                         if (type != DBUS_TYPE_STRING)
2091                                 goto error;
2092
2093                         dbus_message_iter_get_basic(&variant, &url);
2094                 } else if (g_str_equal(key, "Servers") == TRUE) {
2095                         DBusMessageIter str_array;
2096
2097                         if (type != DBUS_TYPE_ARRAY)
2098                                 goto error;
2099
2100                         servers_str = g_string_new(NULL);
2101                         if (servers_str == NULL)
2102                                 goto error;
2103
2104                         dbus_message_iter_recurse(&variant, &str_array);
2105
2106                         while (dbus_message_iter_get_arg_type(&str_array) ==
2107                                                         DBUS_TYPE_STRING) {
2108                                 char *val = NULL;
2109
2110                                 dbus_message_iter_get_basic(&str_array, &val);
2111
2112                                 if (servers_str->len > 0)
2113                                         g_string_append_printf(servers_str,
2114                                                         " %s", val);
2115                                 else
2116                                         g_string_append(servers_str, val);
2117
2118                                 dbus_message_iter_next(&str_array);
2119                         }
2120                 } else if (g_str_equal(key, "Excludes") == TRUE) {
2121                         DBusMessageIter str_array;
2122
2123                         if (type != DBUS_TYPE_ARRAY)
2124                                 goto error;
2125
2126                         excludes_str = g_string_new(NULL);
2127                         if (excludes_str == NULL)
2128                                 goto error;
2129
2130                         dbus_message_iter_recurse(&variant, &str_array);
2131
2132                         while (dbus_message_iter_get_arg_type(&str_array) ==
2133                                                         DBUS_TYPE_STRING) {
2134                                 char *val = NULL;
2135
2136                                 dbus_message_iter_get_basic(&str_array, &val);
2137
2138                                 if (excludes_str->len > 0)
2139                                         g_string_append_printf(excludes_str,
2140                                                         " %s", val);
2141                                 else
2142                                         g_string_append(excludes_str, val);
2143
2144                                 dbus_message_iter_next(&str_array);
2145                         }
2146                 }
2147
2148                 dbus_message_iter_next(&dict);
2149         }
2150
2151         switch (method) {
2152         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2153                 break;
2154         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2155                 if (servers_str == NULL && service->proxies == NULL)
2156                         goto error;
2157
2158                 if (servers_str != NULL) {
2159                         g_strfreev(service->proxies);
2160
2161                         if (servers_str->len > 0)
2162                                 service->proxies = g_strsplit_set(
2163                                         servers_str->str, " ", 0);
2164                         else
2165                                 service->proxies = NULL;
2166                 }
2167
2168                 if (excludes_str != NULL) {
2169                         g_strfreev(service->excludes);
2170
2171                         if (excludes_str->len > 0)
2172                                 service->excludes = g_strsplit_set(
2173                                         excludes_str->str, " ", 0);
2174                         else
2175                                 service->excludes = NULL;
2176                 }
2177
2178                 if (service->proxies == NULL)
2179                         method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2180
2181                 break;
2182         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2183                 g_free(service->pac);
2184
2185                 if (url != NULL && strlen(url) > 0)
2186                         service->pac = g_strdup(url);
2187                 else
2188                         service->pac = NULL;
2189
2190                 /* if we are connected:
2191                    - if service->pac == NULL
2192                    - if __connman_ipconfig_get_proxy_autoconfig(
2193                    service->ipconfig) == NULL
2194                    --> We should start WPAD */
2195
2196                 break;
2197         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2198                 goto error;
2199         }
2200
2201         if (servers_str != NULL)
2202                 g_string_free(servers_str, TRUE);
2203
2204         if (excludes_str != NULL)
2205                 g_string_free(excludes_str, TRUE);
2206
2207         service->proxy_config = method;
2208
2209         return 0;
2210
2211 error:
2212         if (servers_str != NULL)
2213                 g_string_free(servers_str, TRUE);
2214
2215         if (excludes_str != NULL)
2216                 g_string_free(excludes_str, TRUE);
2217
2218         return -EINVAL;
2219 }
2220
2221 static int set_ipconfig(struct connman_service *service,
2222                         struct connman_ipconfig *ipconfig,
2223                         DBusMessageIter *array,
2224                         enum connman_service_state state,
2225                         enum connman_service_state *new_state)
2226 {
2227         enum connman_ipconfig_method old_method;
2228         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2229         enum connman_ipconfig_type type;
2230         int err;
2231
2232         old_method = __connman_ipconfig_get_method(ipconfig);
2233
2234         if (is_connecting_state(service, state) ||
2235                                         is_connected_state(service, state))
2236                 __connman_network_clear_ipconfig(service->network, ipconfig);
2237
2238         err = __connman_ipconfig_set_config(ipconfig, array);
2239         method = __connman_ipconfig_get_method(ipconfig);
2240         type = __connman_ipconfig_get_config_type(ipconfig);
2241
2242         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2243                 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2244                                 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2245                         *new_state = service->state_ipv4 =
2246                                 CONNMAN_SERVICE_STATE_CONFIGURATION;
2247                         __connman_ipconfig_enable(ipconfig);
2248                 }
2249
2250         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2251                 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2252                                 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2253                         *new_state = service->state_ipv6;
2254                         __connman_ipconfig_enable(ipconfig);
2255                 }
2256         }
2257
2258         DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2259                 type, method, state2string(*new_state));
2260
2261         return err;
2262 }
2263
2264 static DBusMessage *set_property(DBusConnection *conn,
2265                                         DBusMessage *msg, void *user_data)
2266 {
2267         struct connman_service *service = user_data;
2268         DBusMessageIter iter, value;
2269         const char *name;
2270         int type;
2271
2272         DBG("service %p", service);
2273
2274         if (dbus_message_iter_init(msg, &iter) == FALSE)
2275                 return __connman_error_invalid_arguments(msg);
2276
2277         dbus_message_iter_get_basic(&iter, &name);
2278         dbus_message_iter_next(&iter);
2279         dbus_message_iter_recurse(&iter, &value);
2280
2281         type = dbus_message_iter_get_arg_type(&value);
2282
2283         if (g_str_equal(name, "AutoConnect") == TRUE) {
2284                 connman_bool_t autoconnect;
2285
2286                 if (type != DBUS_TYPE_BOOLEAN)
2287                         return __connman_error_invalid_arguments(msg);
2288
2289                 if (service->favorite == FALSE)
2290                         return __connman_error_invalid_service(msg);
2291
2292                 dbus_message_iter_get_basic(&value, &autoconnect);
2293
2294                 if (service->autoconnect == autoconnect)
2295                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2296
2297                 service->autoconnect = autoconnect;
2298
2299                 autoconnect_changed(service);
2300
2301                 __connman_storage_save_service(service);
2302         } else if (g_str_equal(name, "Passphrase") == TRUE) {
2303                 const char *passphrase;
2304
2305                 if (type != DBUS_TYPE_STRING)
2306                         return __connman_error_invalid_arguments(msg);
2307
2308                 if (service->immutable == TRUE)
2309                         return __connman_error_not_supported(msg);
2310
2311                 dbus_message_iter_get_basic(&value, &passphrase);
2312
2313                 __connman_service_set_passphrase(service, passphrase);
2314         } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2315                 DBusMessageIter entry;
2316                 GString *str;
2317                 int index;
2318                 const char *gw;
2319
2320                 if (type != DBUS_TYPE_ARRAY)
2321                         return __connman_error_invalid_arguments(msg);
2322
2323                 str = g_string_new(NULL);
2324                 if (str == NULL)
2325                         return __connman_error_invalid_arguments(msg);
2326
2327                 index = connman_network_get_index(service->network);
2328                 gw = __connman_ipconfig_get_gateway_from_index(index);
2329
2330                 if (gw && strlen(gw))
2331                         __connman_service_nameserver_del_routes(service);
2332
2333                 dbus_message_iter_recurse(&value, &entry);
2334
2335                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2336                         const char *val;
2337                         dbus_message_iter_get_basic(&entry, &val);
2338                         dbus_message_iter_next(&entry);
2339                         if (str->len > 0)
2340                                 g_string_append_printf(str, " %s", val);
2341                         else
2342                                 g_string_append(str, val);
2343                 }
2344
2345                 g_strfreev(service->nameservers_config);
2346
2347                 if (str->len > 0) {
2348                         service->nameservers_config =
2349                                 g_strsplit_set(str->str, " ", 0);
2350                 } else {
2351                         service->nameservers_config = NULL;
2352                 }
2353
2354                 g_string_free(str, TRUE);
2355
2356                 if (gw && strlen(gw))
2357                         __connman_service_nameserver_add_routes(service, gw);
2358
2359                 update_nameservers(service);
2360                 dns_configuration_changed(service);
2361
2362                 __connman_storage_save_service(service);
2363         } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2364                 DBusMessageIter entry;
2365                 GString *str;
2366
2367                 if (type != DBUS_TYPE_ARRAY)
2368                         return __connman_error_invalid_arguments(msg);
2369
2370                 str = g_string_new(NULL);
2371                 if (str == NULL)
2372                         return __connman_error_invalid_arguments(msg);
2373
2374                 dbus_message_iter_recurse(&value, &entry);
2375
2376                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2377                         const char *val;
2378                         dbus_message_iter_get_basic(&entry, &val);
2379                         dbus_message_iter_next(&entry);
2380                         if (str->len > 0)
2381                                 g_string_append_printf(str, " %s", val);
2382                         else
2383                                 g_string_append(str, val);
2384                 }
2385
2386                 g_strfreev(service->domains);
2387
2388                 if (str->len > 0)
2389                         service->domains = g_strsplit_set(str->str, " ", 0);
2390                 else
2391                         service->domains = NULL;
2392
2393                 g_string_free(str, TRUE);
2394
2395                 update_nameservers(service);
2396                 domain_configuration_changed(service);
2397
2398                 __connman_storage_save_service(service);
2399         } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2400                 int err;
2401
2402                 if (type != DBUS_TYPE_ARRAY)
2403                         return __connman_error_invalid_arguments(msg);
2404
2405                 err = update_proxy_configuration(service, &value);
2406
2407                 if (err < 0)
2408                         return __connman_error_failed(msg, -err);
2409
2410                 proxy_configuration_changed(service);
2411
2412                 __connman_storage_save_service(service);
2413         } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2414                         g_str_equal(name, "IPv6.Configuration")) {
2415
2416                 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2417                 enum connman_service_state state =
2418                                                 CONNMAN_SERVICE_STATE_UNKNOWN;
2419                 int err = 0;
2420
2421                 DBG("%s", name);
2422
2423                 if (service->ipconfig_ipv4 == NULL &&
2424                                         service->ipconfig_ipv6 == NULL)
2425                         return __connman_error_invalid_property(msg);
2426
2427                 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2428                         ipv4 = service->ipconfig_ipv4;
2429                         err = set_ipconfig(service, ipv4, &value,
2430                                         service->state_ipv4, &state);
2431
2432                 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2433                         ipv6 = service->ipconfig_ipv6;
2434                         err = set_ipconfig(service, ipv6, &value,
2435                                         service->state_ipv6, &state);
2436                 }
2437
2438                 if (err < 0) {
2439                         if (is_connected_state(service, state) ||
2440                                         is_connecting_state(service, state))
2441                                 __connman_network_set_ipconfig(service->network,
2442                                                                 ipv4, ipv6);
2443                         return __connman_error_failed(msg, -err);
2444                 }
2445
2446                 if (ipv4)
2447                         ipv4_configuration_changed(service);
2448                 else if (ipv6)
2449                         ipv6_configuration_changed(service);
2450
2451                 if (is_connecting(service) || is_connected(service))
2452                         __connman_network_set_ipconfig(service->network,
2453                                                         ipv4, ipv6);
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 void set_idle(struct connman_service *service)
2463 {
2464         service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
2465         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2466         state_changed(service);
2467 }
2468
2469 static DBusMessage *clear_property(DBusConnection *conn,
2470                                         DBusMessage *msg, void *user_data)
2471 {
2472         struct connman_service *service = user_data;
2473         const char *name;
2474
2475         DBG("service %p", service);
2476
2477         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2478                                                         DBUS_TYPE_INVALID);
2479
2480         if (g_str_equal(name, "Error") == TRUE) {
2481                 set_idle(service);
2482
2483                 g_get_current_time(&service->modified);
2484                 __connman_storage_save_service(service);
2485         } else if (g_str_equal(name, "Passphrase") == TRUE) {
2486                 if (service->immutable == TRUE)
2487                         return __connman_error_not_supported(msg);
2488
2489                 g_free(service->passphrase);
2490                 service->passphrase = NULL;
2491
2492                 passphrase_changed(service);
2493
2494                 __connman_storage_save_service(service);
2495         } else
2496                 return __connman_error_invalid_property(msg);
2497
2498         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2499 }
2500
2501 static connman_bool_t is_ignore(struct connman_service *service)
2502 {
2503         if (service->autoconnect == FALSE)
2504                 return TRUE;
2505
2506         if (service->roaming == TRUE)
2507                 return TRUE;
2508
2509         if (service->ignore == TRUE)
2510                 return TRUE;
2511
2512         if (combine_state(service->state_ipv4, service->state_ipv6) ==
2513                                                 CONNMAN_SERVICE_STATE_FAILURE)
2514                 return TRUE;
2515
2516         return FALSE;
2517 }
2518
2519 void __connman_service_auto_connect(void)
2520 {
2521         struct connman_service *service = NULL;
2522         GSequenceIter *iter;
2523
2524         DBG("");
2525
2526         if (__connman_session_mode() == TRUE) {
2527                 DBG("Session mode enabled: auto connect disabled");
2528                 return;
2529         }
2530
2531         iter = g_sequence_get_begin_iter(service_list);
2532
2533         while (g_sequence_iter_is_end(iter) == FALSE) {
2534                 service = g_sequence_get(iter);
2535
2536                 if (service->pending != NULL)
2537                         return;
2538
2539                 if (is_connecting(service) == TRUE)
2540                         return;
2541
2542                 if (service->favorite == FALSE)
2543                         return;
2544
2545                 if (is_connected(service) == TRUE)
2546                         return;
2547
2548                 if (is_ignore(service) == FALSE &&
2549                         combine_state(service->state_ipv4,
2550                                         service->state_ipv6) ==
2551                                                 CONNMAN_SERVICE_STATE_IDLE)
2552                         break;
2553
2554                 service = NULL;
2555
2556                 iter = g_sequence_iter_next(iter);
2557         }
2558
2559         if (service != NULL) {
2560                 service->userconnect = FALSE;
2561                 __connman_service_connect(service);
2562         }
2563 }
2564
2565 static void remove_timeout(struct connman_service *service)
2566 {
2567         if (service->timeout > 0) {
2568                 g_source_remove(service->timeout);
2569                 service->timeout = 0;
2570         }
2571 }
2572
2573 static void reply_pending(struct connman_service *service, int error)
2574 {
2575         remove_timeout(service);
2576
2577         if (service->pending != NULL) {
2578                 if (error > 0) {
2579                         DBusMessage *reply;
2580
2581                         reply = __connman_error_failed(service->pending,
2582                                                                 error);
2583                         if (reply != NULL)
2584                                 g_dbus_send_message(connection, reply);
2585                 } else {
2586                         const char *sender;
2587
2588                         sender = dbus_message_get_interface(service->pending);
2589
2590                         DBG("sender %s", sender);
2591
2592                         if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2593                                 g_dbus_send_reply(connection, service->pending,
2594                                         DBUS_TYPE_OBJECT_PATH, &service->path,
2595                                                         DBUS_TYPE_INVALID);
2596                         else
2597                                 g_dbus_send_reply(connection, service->pending,
2598                                                         DBUS_TYPE_INVALID);
2599                 }
2600
2601                 dbus_message_unref(service->pending);
2602                 service->pending = NULL;
2603         }
2604 }
2605
2606 static gboolean connect_timeout(gpointer user_data)
2607 {
2608         struct connman_service *service = user_data;
2609         connman_bool_t autoconnect = FALSE;
2610
2611         DBG("service %p", service);
2612
2613         service->timeout = 0;
2614
2615         if (service->network != NULL)
2616                 __connman_network_disconnect(service->network);
2617
2618         __connman_ipconfig_disable(service->ipconfig_ipv4);
2619         __connman_ipconfig_disable(service->ipconfig_ipv6);
2620
2621         __connman_stats_service_unregister(service);
2622
2623         if (service->pending != NULL) {
2624                 DBusMessage *reply;
2625
2626                 reply = __connman_error_operation_timeout(service->pending);
2627                 if (reply != NULL)
2628                         g_dbus_send_message(connection, reply);
2629
2630                 dbus_message_unref(service->pending);
2631                 service->pending = NULL;
2632         } else
2633                 autoconnect = TRUE;
2634
2635         __connman_service_indicate_state(service,
2636                                         CONNMAN_SERVICE_STATE_FAILURE,
2637                                         CONNMAN_IPCONFIG_TYPE_IPV4);
2638         __connman_service_indicate_state(service,
2639                                         CONNMAN_SERVICE_STATE_FAILURE,
2640                                         CONNMAN_IPCONFIG_TYPE_IPV6);
2641
2642         if (autoconnect == TRUE && service->userconnect == FALSE)
2643                 __connman_service_auto_connect();
2644
2645         return FALSE;
2646 }
2647
2648 static void set_reconnect_state(struct connman_service *service,
2649                                                 connman_bool_t reconnect)
2650 {
2651         struct connman_device *device;
2652
2653         if (service->network == NULL)
2654                 return;
2655
2656         device = connman_network_get_device(service->network);
2657         if (device == NULL)
2658                 return;
2659
2660         __connman_device_set_reconnect(device, reconnect);
2661 }
2662
2663 static connman_bool_t get_reconnect_state(struct connman_service *service)
2664 {
2665         struct connman_device *device;
2666
2667         if (service->network == NULL)
2668                 return FALSE;
2669
2670         device = connman_network_get_device(service->network);
2671         if (device == NULL)
2672                 return FALSE;
2673
2674         return __connman_device_get_reconnect(device);
2675 }
2676
2677 static void request_input_cb (struct connman_service *service,
2678                         const char *passphrase, void *user_data)
2679 {
2680         DBG ("RequestInput return, %p", service);
2681
2682         if (passphrase == NULL && service->wps == FALSE)
2683                 return;
2684
2685         if (passphrase != NULL)
2686                 __connman_service_set_passphrase(service, passphrase);
2687
2688         __connman_service_connect(service);
2689 }
2690
2691 static DBusMessage *connect_service(DBusConnection *conn,
2692                                         DBusMessage *msg, void *user_data)
2693 {
2694         struct connman_service *service = user_data;
2695         GSequenceIter *iter;
2696         int err;
2697
2698         DBG("service %p", service);
2699
2700         if (service->pending != NULL)
2701                 return __connman_error_in_progress(msg);
2702
2703         iter = g_sequence_get_begin_iter(service_list);
2704
2705         while (g_sequence_iter_is_end(iter) == FALSE) {
2706                 struct connman_service *temp = g_sequence_get(iter);
2707
2708                 if (service->type == temp->type && is_connecting(temp) == TRUE)
2709                         return __connman_error_in_progress(msg);
2710
2711                 iter = g_sequence_iter_next(iter);
2712         }
2713
2714         service->ignore = FALSE;
2715
2716         service->userconnect = TRUE;
2717
2718         service->pending = dbus_message_ref(msg);
2719
2720         set_reconnect_state(service, FALSE);
2721
2722         err = __connman_service_connect(service);
2723         if (err < 0) {
2724                 if (err == -ENOKEY) {
2725                         if (__connman_agent_request_input(service,
2726                                                         request_input_cb,
2727                                                         NULL) == 0)
2728                                 return NULL;
2729                 }
2730
2731                 if (service->pending == NULL)
2732                         return NULL;
2733
2734                 if (err != -EINPROGRESS) {
2735                         dbus_message_unref(service->pending);
2736                         service->pending = NULL;
2737
2738                         return __connman_error_failed(msg, -err);
2739                 }
2740
2741                 return NULL;
2742         }
2743
2744         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2745 }
2746
2747 static DBusMessage *disconnect_service(DBusConnection *conn,
2748                                         DBusMessage *msg, void *user_data)
2749 {
2750         struct connman_service *service = user_data;
2751         int err;
2752
2753         DBG("service %p", service);
2754
2755         reply_pending(service, ECONNABORTED);
2756
2757         service->ignore = TRUE;
2758
2759         set_reconnect_state(service, FALSE);
2760
2761         err = __connman_service_disconnect(service);
2762         if (err < 0) {
2763                 if (err != -EINPROGRESS)
2764                         return __connman_error_failed(msg, -err);
2765
2766                 return NULL;
2767         }
2768
2769         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2770 }
2771
2772 static DBusMessage *remove_service(DBusConnection *conn,
2773                                         DBusMessage *msg, void *user_data)
2774 {
2775         struct connman_service *service = user_data;
2776
2777         DBG("service %p", service);
2778
2779         if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2780                 return __connman_error_not_supported(msg);
2781
2782         if (service->immutable == TRUE)
2783                 return __connman_error_not_supported(msg);
2784
2785         if (service->favorite == FALSE &&
2786                 combine_state(service->state_ipv4, service->state_ipv6) !=
2787                                                 CONNMAN_SERVICE_STATE_FAILURE)
2788                 return __connman_error_not_supported(msg);
2789
2790         if (service->network != NULL) {
2791                 set_reconnect_state(service, FALSE);
2792
2793                 __connman_network_disconnect(service->network);
2794         }
2795
2796         g_free(service->passphrase);
2797         service->passphrase = NULL;
2798
2799         passphrase_changed(service);
2800
2801         set_idle(service);
2802
2803         __connman_service_set_favorite(service, FALSE);
2804         __connman_storage_save_service(service);
2805
2806         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2807 }
2808
2809 static DBusMessage *move_service(DBusConnection *conn,
2810                                         DBusMessage *msg, void *user_data,
2811                                                                 gboolean before)
2812 {
2813         struct connman_service *service = user_data;
2814         struct connman_service *target;
2815         const char *path;
2816         GSequenceIter *src, *dst;
2817
2818         DBG("service %p", service);
2819
2820         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2821                                                         DBUS_TYPE_INVALID);
2822
2823         if (service->favorite == FALSE)
2824                 return __connman_error_not_supported(msg);
2825
2826         target = find_service(path);
2827         if (target == NULL || target->favorite == FALSE || target == service)
2828                 return __connman_error_invalid_service(msg);
2829
2830         DBG("target %s", target->identifier);
2831
2832         if (target->state_ipv4 != service->state_ipv4 &&
2833                                 target->state_ipv6 != service->state_ipv6)
2834                 return __connman_error_invalid_service(msg);
2835
2836         g_get_current_time(&service->modified);
2837         __connman_storage_save_service(service);
2838
2839         src = g_hash_table_lookup(service_hash, service->identifier);
2840         dst = g_hash_table_lookup(service_hash, target->identifier);
2841
2842         before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
2843
2844         __connman_profile_changed(FALSE);
2845
2846         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2847 }
2848
2849 static DBusMessage *move_before(DBusConnection *conn,
2850                                         DBusMessage *msg, void *user_data)
2851 {
2852         return move_service(conn, msg, user_data, TRUE);
2853 }
2854
2855 static DBusMessage *move_after(DBusConnection *conn,
2856                                         DBusMessage *msg, void *user_data)
2857 {
2858         return move_service(conn, msg, user_data, FALSE);
2859 }
2860
2861 static DBusMessage *reset_counters(DBusConnection *conn,
2862                                         DBusMessage *msg, void *user_data)
2863 {
2864         struct connman_service *service = user_data;
2865
2866         reset_stats(service);
2867
2868         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2869 }
2870
2871 static GDBusMethodTable service_methods[] = {
2872         { "GetProperties", "",   "a{sv}", get_properties     },
2873         { "SetProperty",   "sv", "",      set_property       },
2874         { "ClearProperty", "s",  "",      clear_property     },
2875         { "Connect",       "",   "",      connect_service,
2876                                                 G_DBUS_METHOD_FLAG_ASYNC },
2877         { "Disconnect",    "",   "",      disconnect_service },
2878         { "Remove",        "",   "",      remove_service     },
2879         { "MoveBefore",    "o",  "",      move_before        },
2880         { "MoveAfter",     "o",  "",      move_after         },
2881         { "ResetCounters", "",   "",      reset_counters     },
2882         { },
2883 };
2884
2885 static GDBusSignalTable service_signals[] = {
2886         { "PropertyChanged", "sv" },
2887         { },
2888 };
2889
2890 static void service_free(gpointer user_data)
2891 {
2892         struct connman_service *service = user_data;
2893         char *path = service->path;
2894
2895         DBG("service %p", service);
2896
2897         reply_pending(service, ENOENT);
2898
2899         g_hash_table_remove(service_hash, service->identifier);
2900
2901         stats_stop(service);
2902         __connman_storage_save_service(service);
2903
2904         service->path = NULL;
2905
2906         if (path != NULL) {
2907                 __connman_profile_changed(FALSE);
2908
2909                 g_dbus_unregister_interface(connection, path,
2910                                                 CONNMAN_SERVICE_INTERFACE);
2911                 g_free(path);
2912         }
2913
2914         g_hash_table_destroy(service->counter_table);
2915
2916         if (service->network != NULL)
2917                 connman_network_unref(service->network);
2918
2919         if (service->provider != NULL)
2920                 connman_provider_unref(service->provider);
2921
2922         if (service->ipconfig_ipv4 != NULL) {
2923                 connman_ipconfig_unref(service->ipconfig_ipv4);
2924                 service->ipconfig_ipv4 = NULL;
2925         }
2926
2927         if (service->ipconfig_ipv6 != NULL) {
2928                 connman_ipconfig_unref(service->ipconfig_ipv6);
2929                 service->ipconfig_ipv6 = NULL;
2930         }
2931
2932         if (service->location != NULL)
2933                 connman_location_unref(service->location);
2934
2935         g_strfreev(service->nameservers);
2936         g_strfreev(service->nameservers_config);
2937         g_strfreev(service->domains);
2938         g_strfreev(service->proxies);
2939         g_strfreev(service->excludes);
2940
2941         g_free(service->domainname);
2942         g_free(service->pac);
2943         g_free(service->profile);
2944         g_free(service->name);
2945         g_free(service->passphrase);
2946         g_free(service->identifier);
2947         g_free(service->eap);
2948         g_free(service->identity);
2949         g_free(service->ca_cert_file);
2950         g_free(service->client_cert_file);
2951         g_free(service->private_key_file);
2952         g_free(service->private_key_passphrase);
2953         g_free(service->phase2);
2954
2955         if (service->stats.timer != NULL)
2956                 g_timer_destroy(service->stats.timer);
2957         if (service->stats_roaming.timer != NULL)
2958                 g_timer_destroy(service->stats_roaming.timer);
2959
2960         g_free(service);
2961 }
2962
2963 /**
2964  * __connman_service_put:
2965  * @service: service structure
2966  *
2967  * Release service if no longer needed
2968  */
2969 void __connman_service_put(struct connman_service *service)
2970 {
2971         DBG("service %p", service);
2972
2973         if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
2974                 GSequenceIter *iter;
2975
2976                 iter = g_hash_table_lookup(service_hash, service->identifier);
2977                 if (iter != NULL) {
2978                         reply_pending(service, ECONNABORTED);
2979
2980                         __connman_service_disconnect(service);
2981
2982                         g_sequence_remove(iter);
2983                 } else
2984                         service_free(service);
2985         }
2986 }
2987
2988 static void stats_init(struct connman_service *service)
2989 {
2990         /* home */
2991         service->stats.valid = FALSE;
2992         service->stats.enabled = FALSE;
2993         service->stats.timer = g_timer_new();
2994
2995         /* roaming */
2996         service->stats_roaming.valid = FALSE;
2997         service->stats_roaming.enabled = FALSE;
2998         service->stats_roaming.timer = g_timer_new();
2999 }
3000
3001 static void service_initialize(struct connman_service *service)
3002 {
3003         DBG("service %p", service);
3004
3005         service->refcount = 1;
3006
3007         service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
3008         service->mode     = CONNMAN_SERVICE_MODE_UNKNOWN;
3009         service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3010
3011         service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3012         service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3013
3014         service->favorite  = FALSE;
3015         service->immutable = FALSE;
3016         service->hidden = FALSE;
3017
3018         service->ignore = FALSE;
3019
3020         service->userconnect = FALSE;
3021
3022         service->order = 0;
3023
3024         stats_init(service);
3025
3026         service->provider = NULL;
3027
3028         service->wps = FALSE;
3029 }
3030
3031 /**
3032  * connman_service_create:
3033  *
3034  * Allocate a new service.
3035  *
3036  * Returns: a newly-allocated #connman_service structure
3037  */
3038 struct connman_service *connman_service_create(void)
3039 {
3040         GSList *list;
3041         struct connman_stats_counter *counters;
3042         const char *counter;
3043
3044         struct connman_service *service;
3045
3046         service = g_try_new0(struct connman_service, 1);
3047         if (service == NULL)
3048                 return NULL;
3049
3050         DBG("service %p", service);
3051
3052         service->counter_table = g_hash_table_new_full(g_str_hash,
3053                                                 g_str_equal, NULL, g_free);
3054
3055         for (list = counter_list; list; list = list->next) {
3056                 counter = list->data;
3057
3058                 counters = g_try_new0(struct connman_stats_counter, 1);
3059                 if (counters == NULL) {
3060                         g_hash_table_destroy(service->counter_table);
3061                         g_free(service);
3062                         return NULL;
3063                 }
3064
3065                 counters->append_all = TRUE;
3066
3067                 g_hash_table_replace(service->counter_table, (gpointer)counter,
3068                                 counters);
3069         }
3070
3071         service_initialize(service);
3072
3073         service->location = __connman_location_create(service);
3074
3075         return service;
3076 }
3077
3078 struct connman_location *__connman_service_get_location(struct connman_service *service)
3079 {
3080         return service->location;
3081 }
3082
3083 /**
3084  * connman_service_ref:
3085  * @service: service structure
3086  *
3087  * Increase reference counter of service
3088  */
3089 struct connman_service *connman_service_ref(struct connman_service *service)
3090 {
3091         DBG("%p", service);
3092
3093         g_atomic_int_inc(&service->refcount);
3094
3095         return service;
3096 }
3097
3098 /**
3099  * connman_service_unref:
3100  * @service: service structure
3101  *
3102  * Decrease reference counter of service
3103  */
3104 void connman_service_unref(struct connman_service *service)
3105 {
3106         __connman_service_put(service);
3107 }
3108
3109 static gint service_compare(gconstpointer a, gconstpointer b,
3110                                                         gpointer user_data)
3111 {
3112         struct connman_service *service_a = (void *) a;
3113         struct connman_service *service_b = (void *) b;
3114         enum connman_service_state state_a, state_b;
3115
3116         state_a = combine_state(service_a->state_ipv4, service_a->state_ipv6);
3117         state_b = combine_state(service_b->state_ipv4, service_b->state_ipv6);
3118
3119         if (state_a != state_b) {
3120                 if (is_connected(service_a) == TRUE)
3121                         return -1;
3122                 if (is_connected(service_b) == TRUE)
3123                         return 1;
3124
3125                 if (is_connecting(service_a) == TRUE)
3126                         return -1;
3127                 if (is_connecting(service_b) == TRUE)
3128                         return 1;
3129         }
3130
3131         if (service_a->order > service_b->order)
3132                 return -1;
3133
3134         if (service_a->order < service_b->order)
3135                 return 1;
3136
3137         if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3138                 return -1;
3139
3140         if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3141                 return 1;
3142
3143         if (service_a->type != service_b->type) {
3144                 switch (service_a->type) {
3145                 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3146                 case CONNMAN_SERVICE_TYPE_SYSTEM:
3147                 case CONNMAN_SERVICE_TYPE_ETHERNET:
3148                 case CONNMAN_SERVICE_TYPE_GPS:
3149                 case CONNMAN_SERVICE_TYPE_VPN:
3150                 case CONNMAN_SERVICE_TYPE_GADGET:
3151                         break;
3152                 case CONNMAN_SERVICE_TYPE_WIFI:
3153                         return 1;
3154                 case CONNMAN_SERVICE_TYPE_WIMAX:
3155                 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3156                 case CONNMAN_SERVICE_TYPE_CELLULAR:
3157                         return -1;
3158                 }
3159         }
3160
3161         return (gint) service_b->strength - (gint) service_a->strength;
3162 }
3163
3164 /**
3165  * connman_service_get_type:
3166  * @service: service structure
3167  *
3168  * Get the type of service
3169  */
3170 enum connman_service_type connman_service_get_type(struct connman_service *service)
3171 {
3172         if (service == NULL)
3173                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3174
3175         return service->type;
3176 }
3177
3178 /**
3179  * connman_service_get_interface:
3180  * @service: service structure
3181  *
3182  * Get network interface of service
3183  */
3184 char *connman_service_get_interface(struct connman_service *service)
3185 {
3186         int index;
3187
3188         if (service == NULL)
3189                 return NULL;
3190
3191         if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3192                 if (service->ipconfig_ipv4)
3193                         index = connman_ipconfig_get_index(
3194                                                 service->ipconfig_ipv4);
3195                 else if (service->ipconfig_ipv6)
3196                         index = connman_ipconfig_get_index(
3197                                                 service->ipconfig_ipv6);
3198                 else
3199                         return NULL;
3200
3201                 return connman_inet_ifname(index);
3202         }
3203
3204         if (service->network == NULL)
3205                 return NULL;
3206
3207         index = connman_network_get_index(service->network);
3208
3209         return connman_inet_ifname(index);
3210 }
3211
3212 /**
3213  * connman_service_get_network:
3214  * @service: service structure
3215  *
3216  * Get the service network
3217  */
3218 struct connman_network *
3219 __connman_service_get_network(struct connman_service *service)
3220 {
3221         if (service == NULL)
3222                 return NULL;
3223
3224         return service->network;
3225 }
3226
3227 struct connman_ipconfig *
3228 __connman_service_get_ip4config(struct connman_service *service)
3229 {
3230         if (service == NULL)
3231                 return NULL;
3232
3233         return service->ipconfig_ipv4;
3234 }
3235
3236 struct connman_ipconfig *
3237 __connman_service_get_ip6config(struct connman_service *service)
3238 {
3239         if (service == NULL)
3240                 return NULL;
3241
3242         return service->ipconfig_ipv6;
3243 }
3244
3245 struct connman_ipconfig *
3246 __connman_service_get_ipconfig(struct connman_service *service, int family)
3247 {
3248         if (family == AF_INET)
3249                 return __connman_service_get_ip4config(service);
3250         else if (family == AF_INET6)
3251                 return __connman_service_get_ip6config(service);
3252         else
3253                 return NULL;
3254
3255 }
3256
3257 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3258 {
3259         if (service == NULL)
3260                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3261
3262         return service->security;
3263 }
3264
3265 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3266 {
3267         if (service == NULL)
3268                 return FALSE;
3269
3270         return service->wps;
3271 }
3272
3273 /**
3274  * __connman_service_set_favorite:
3275  * @service: service structure
3276  * @favorite: favorite value
3277  *
3278  * Change the favorite setting of service
3279  */
3280 int __connman_service_set_favorite(struct connman_service *service,
3281                                                 connman_bool_t favorite)
3282 {
3283         GSequenceIter *iter;
3284
3285         iter = g_hash_table_lookup(service_hash, service->identifier);
3286         if (iter == NULL)
3287                 return -ENOENT;
3288
3289         if (service->favorite == favorite)
3290                 return -EALREADY;
3291
3292         service->favorite = favorite;
3293
3294         favorite_changed(service);
3295
3296         g_sequence_sort_changed(iter, service_compare, NULL);
3297
3298         __connman_profile_changed(FALSE);
3299
3300         return 0;
3301 }
3302
3303 int __connman_service_set_immutable(struct connman_service *service,
3304                                                 connman_bool_t immutable)
3305 {
3306         service->immutable = immutable;
3307
3308         immutable_changed(service);
3309
3310         return 0;
3311 }
3312
3313 void __connman_service_set_string(struct connman_service *service,
3314                                   const char *key, const char *value)
3315 {
3316         if (g_str_equal(key, "EAP") == TRUE) {
3317                 g_free(service->eap);
3318                 service->eap = g_strdup(value);
3319         } else if (g_str_equal(key, "Identity") == TRUE) {
3320                 g_free(service->identity);
3321                 service->identity = g_strdup(value);
3322         } else if (g_str_equal(key, "CACertFile") == TRUE) {
3323                 g_free(service->ca_cert_file);
3324                 service->ca_cert_file = g_strdup(value);
3325         } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3326                 g_free(service->client_cert_file);
3327                 service->client_cert_file = g_strdup(value);
3328         } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3329                 g_free(service->private_key_file);
3330                 service->private_key_file = g_strdup(value);
3331         } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3332                 g_free(service->private_key_passphrase);
3333                 service->private_key_passphrase = g_strdup(value);
3334         } else if (g_str_equal(key, "Phase2") == TRUE) {
3335                 g_free(service->phase2);
3336                 service->phase2 = g_strdup(value);
3337         } else if (g_str_equal(key, "Passphrase") == TRUE) {
3338                 g_free(service->passphrase);
3339                 service->passphrase = g_strdup(value);
3340         }
3341 }
3342
3343 static void service_complete(struct connman_service *service)
3344 {
3345         reply_pending(service, EIO);
3346
3347         if (service->userconnect == FALSE)
3348                 __connman_service_auto_connect();
3349
3350         g_get_current_time(&service->modified);
3351         __connman_storage_save_service(service);
3352 }
3353
3354 static void report_error_cb(struct connman_service *service,
3355                         gboolean retry, void *user_data)
3356 {
3357         if (retry == TRUE)
3358                 __connman_service_connect(service);
3359         else {
3360                 service_complete(service);
3361                 __connman_profile_changed(FALSE);
3362                 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3363         }
3364 }
3365
3366 int __connman_service_indicate_state(struct connman_service *service,
3367                                         enum connman_service_state new_state,
3368                                         enum connman_ipconfig_type type)
3369 {
3370         enum connman_service_state service_state,
3371                 state = CONNMAN_SERVICE_STATE_IDLE;
3372         GSequenceIter *iter;
3373
3374         if (service == NULL)
3375                 return -EINVAL;
3376
3377         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3378                 if (service->state_ipv4 == new_state)
3379                         return -EALREADY;
3380                 state = combine_state(new_state, service->state_ipv6);
3381         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3382                 if (service->state_ipv6 == new_state)
3383                         return -EALREADY;
3384                 state = combine_state(new_state, service->state_ipv4);
3385         } else
3386                 return -EINVAL;
3387
3388         service_state = combine_state(service->state_ipv4,
3389                                                         service->state_ipv6);
3390
3391         DBG("service %p state %s/%s => %s new %s/%d => %s",
3392                                         service,
3393                                         state2string(service->state_ipv4),
3394                                         state2string(service->state_ipv6),
3395                                         state2string(service_state),
3396                                         state2string(new_state),
3397                                         type,
3398                                         state2string(state));
3399
3400         if (service_state == CONNMAN_SERVICE_STATE_FAILURE &&
3401                                 state == CONNMAN_SERVICE_STATE_IDLE)
3402                 return -EINVAL;
3403
3404         if (service_state == CONNMAN_SERVICE_STATE_IDLE &&
3405                                 state == CONNMAN_SERVICE_STATE_DISCONNECT)
3406                 return -EINVAL;
3407
3408         if (state == CONNMAN_SERVICE_STATE_IDLE &&
3409                         service_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3410                 state_changed(service);
3411
3412                 reply_pending(service, ECONNABORTED);
3413
3414                 __connman_service_disconnect(service);
3415         }
3416
3417         if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3418                 if (__connman_stats_service_register(service) == 0) {
3419                         __connman_stats_get(service, FALSE,
3420                                                 &service->stats.data);
3421                         __connman_stats_get(service, TRUE,
3422                                                 &service->stats_roaming.data);
3423                 }
3424
3425                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
3426                         new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3427                         __connman_ipconfig_enable(service->ipconfig_ipv4);
3428                 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
3429                         new_state == CONNMAN_SERVICE_STATE_CONFIGURATION)
3430                         __connman_ipconfig_enable(service->ipconfig_ipv6);
3431         }
3432
3433         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3434                 service->state_ipv4 = new_state;
3435         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3436                 service->state_ipv6 = new_state;
3437
3438         if (state == service_state)
3439                 return -EALREADY;
3440
3441         state_changed(service);
3442
3443         if (state == CONNMAN_SERVICE_STATE_ONLINE) {
3444                 if (service->login_required == TRUE) {
3445                         service->login_required = FALSE;
3446                         login_changed(service);
3447                 }
3448
3449                 connman_timeserver_sync();
3450         }
3451
3452         if (state == CONNMAN_SERVICE_STATE_IDLE) {
3453                 connman_bool_t reconnect;
3454
3455                 reconnect = get_reconnect_state(service);
3456                 if (reconnect == TRUE)
3457                         __connman_service_auto_connect();
3458         }
3459
3460         if (state == CONNMAN_SERVICE_STATE_READY) {
3461                 enum connman_service_proxy_method proxy_config;
3462                 enum connman_ipconfig_method method;
3463
3464                 set_reconnect_state(service, TRUE);
3465
3466                 __connman_service_set_favorite(service, TRUE);
3467
3468                 reply_pending(service, 0);
3469
3470                 service->userconnect = FALSE;
3471
3472                 g_get_current_time(&service->modified);
3473                 __connman_storage_save_service(service);
3474
3475                 update_nameservers(service);
3476                 dns_changed(service);
3477                 domain_changed(service);
3478
3479                 proxy_config = service->proxy_config;
3480
3481                 /*
3482                  * We start WPAD if we haven't got a PAC URL from DHCP and
3483                  * if our proxy manual configuration is either empty or set
3484                  * to AUTO with an empty URL.
3485                  */
3486                 if (service->proxy == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3487                         (proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN ||
3488                                 (proxy_config ==
3489                                         CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
3490                                         service->pac == NULL)))
3491                         if (__connman_wpad_start(service) < 0) {
3492                                 service->proxy =
3493                                         CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3494
3495                                 __connman_notifier_proxy_changed(service);
3496                         }
3497
3498                 __connman_notifier_connect(service->type);
3499
3500                 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3501                         connman_network_get_bool(service->network,
3502                                                 "WiFi.UseWPS") == TRUE) {
3503                         const char *pass;
3504
3505                         pass = connman_network_get_string(service->network,
3506                                                         "WiFi.Passphrase");
3507
3508                         __connman_service_set_passphrase(service, pass);
3509
3510                         connman_network_set_bool(service->network,
3511                                                         "WiFi.UseWPS", FALSE);
3512                 }
3513
3514                 default_changed();
3515
3516                 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3517                 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
3518                         __connman_ipconfig_disable_ipv6(
3519                                                 service->ipconfig_ipv6);
3520
3521         } else if (state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3522                 __connman_location_finish(service);
3523
3524                 default_changed();
3525
3526                 __connman_wpad_stop(service);
3527
3528                 update_nameservers(service);
3529                 dns_changed(service);
3530                 domain_changed(service);
3531
3532                 __connman_notifier_disconnect(service->type);
3533         }
3534
3535         if (state == CONNMAN_SERVICE_STATE_FAILURE) {
3536                 if (service->userconnect == TRUE &&
3537                         __connman_agent_report_error(service,
3538                                         error2string(service->error),
3539                                         report_error_cb, NULL) == -EIO)
3540                         return 0;
3541                 service_complete(service);
3542         } else
3543                 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3544
3545         iter = g_hash_table_lookup(service_hash, service->identifier);
3546         if (iter != NULL)
3547                 g_sequence_sort_changed(iter, service_compare, NULL);
3548
3549         __connman_profile_changed(FALSE);
3550
3551         service_state = combine_state(service->state_ipv4,
3552                                                         service->state_ipv6);
3553         if (service_state == CONNMAN_SERVICE_STATE_ONLINE)
3554                 default_changed();
3555
3556         if (service_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3557                 struct connman_service *def_service = get_default();
3558
3559                 if (__connman_notifier_count_connected() == 0 &&
3560                         def_service != NULL &&
3561                                 def_service->provider != NULL)
3562                         __connman_provider_disconnect(def_service->provider);
3563         }
3564
3565         if (service_state == CONNMAN_SERVICE_STATE_IDLE ||
3566                         service_state == CONNMAN_SERVICE_STATE_FAILURE)
3567                 __connman_element_request_scan(CONNMAN_ELEMENT_TYPE_UNKNOWN);
3568
3569         return 0;
3570 }
3571
3572 int __connman_service_indicate_error(struct connman_service *service,
3573                                         enum connman_service_error error)
3574 {
3575         DBG("service %p error %d", service, error);
3576
3577         if (service == NULL)
3578                 return -EINVAL;
3579
3580         service->error = error;
3581
3582         if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
3583                 __connman_service_set_passphrase(service, NULL);
3584
3585         return __connman_service_indicate_state(service,
3586                                                 CONNMAN_SERVICE_STATE_FAILURE,
3587                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
3588 }
3589
3590 int __connman_service_clear_error(struct connman_service *service)
3591 {
3592         enum connman_service_state state;
3593
3594         DBG("service %p", service);
3595
3596         if (service == NULL)
3597                 return -EINVAL;
3598
3599         state = combine_state(service->state_ipv4, service->state_ipv6);
3600
3601         if (state != CONNMAN_SERVICE_STATE_FAILURE)
3602                 return -EINVAL;
3603
3604         service->state_ipv4 = service->state_ipv6 =
3605                                                 CONNMAN_SERVICE_STATE_UNKNOWN;
3606         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3607
3608         if (service->favorite == TRUE)
3609                 set_reconnect_state(service, TRUE);
3610
3611         __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_IDLE,
3612                                         CONNMAN_IPCONFIG_TYPE_IPV6);
3613
3614         /*
3615          * Toggling the IPv6 state to IDLE could trigger the auto connect
3616          * machinery and consequently the IPv4 state.
3617          */
3618         if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
3619                         service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
3620                 return 0;
3621
3622         return __connman_service_indicate_state(service,
3623                                                 CONNMAN_SERVICE_STATE_IDLE,
3624                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
3625 }
3626
3627 int __connman_service_indicate_default(struct connman_service *service)
3628 {
3629         DBG("service %p", service);
3630
3631         default_changed();
3632
3633         __connman_location_detect(service);
3634
3635         return 0;
3636 }
3637
3638 int __connman_service_request_login(struct connman_service *service)
3639 {
3640         DBG("service %p", service);
3641
3642         if (service == NULL)
3643                 return -EINVAL;
3644
3645         service->login_required = TRUE;
3646         login_changed(service);
3647
3648         return 0;
3649 }
3650
3651 static connman_bool_t prepare_network(struct connman_service *service)
3652 {
3653         enum connman_network_type type;
3654         unsigned int ssid_len;
3655
3656         type = connman_network_get_type(service->network);
3657
3658         switch (type) {
3659         case CONNMAN_NETWORK_TYPE_UNKNOWN:
3660         case CONNMAN_NETWORK_TYPE_VENDOR:
3661                 return FALSE;
3662         case CONNMAN_NETWORK_TYPE_WIFI:
3663                 if (connman_network_get_blob(service->network, "WiFi.SSID",
3664                                                         &ssid_len) == NULL)
3665                         return FALSE;
3666
3667                 if (service->passphrase != NULL)
3668                         connman_network_set_string(service->network,
3669                                 "WiFi.Passphrase", service->passphrase);
3670                 break;
3671         case CONNMAN_NETWORK_TYPE_ETHERNET:
3672         case CONNMAN_NETWORK_TYPE_WIMAX:
3673         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3674         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3675         case CONNMAN_NETWORK_TYPE_CELLULAR:
3676                 break;
3677         }
3678
3679         return TRUE;
3680 }
3681
3682 static void prepare_8021x(struct connman_service *service)
3683 {
3684         if (service->eap != NULL)
3685                 connman_network_set_string(service->network, "WiFi.EAP",
3686                                                                 service->eap);
3687
3688         if (service->identity != NULL)
3689                 connman_network_set_string(service->network, "WiFi.Identity",
3690                                                         service->identity);
3691
3692         if (service->ca_cert_file != NULL)
3693                 connman_network_set_string(service->network, "WiFi.CACertFile",
3694                                                         service->ca_cert_file);
3695
3696         if (service->client_cert_file != NULL)
3697                 connman_network_set_string(service->network,
3698                                                 "WiFi.ClientCertFile",
3699                                                 service->client_cert_file);
3700
3701         if (service->private_key_file != NULL)
3702                 connman_network_set_string(service->network,
3703                                                 "WiFi.PrivateKeyFile",
3704                                                 service->private_key_file);
3705
3706         if (service->private_key_passphrase != NULL)
3707                 connman_network_set_string(service->network,
3708                                         "WiFi.PrivateKeyPassphrase",
3709                                         service->private_key_passphrase);
3710
3711         if (service->phase2 != NULL)
3712                 connman_network_set_string(service->network, "WiFi.Phase2",
3713                                                         service->phase2);
3714 }
3715
3716 static int service_connect(struct connman_service *service)
3717 {
3718         int err;
3719
3720         switch (service->type) {
3721         case CONNMAN_SERVICE_TYPE_UNKNOWN:
3722         case CONNMAN_SERVICE_TYPE_SYSTEM:
3723         case CONNMAN_SERVICE_TYPE_GPS:
3724         case CONNMAN_SERVICE_TYPE_GADGET:
3725                 return -EINVAL;
3726         case CONNMAN_SERVICE_TYPE_ETHERNET:
3727         case CONNMAN_SERVICE_TYPE_WIMAX:
3728         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3729         case CONNMAN_SERVICE_TYPE_CELLULAR:
3730         case CONNMAN_SERVICE_TYPE_VPN:
3731                 break;
3732         case CONNMAN_SERVICE_TYPE_WIFI:
3733                 switch (service->security) {
3734                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3735                 case CONNMAN_SERVICE_SECURITY_NONE:
3736                         break;
3737                 case CONNMAN_SERVICE_SECURITY_WEP:
3738                 case CONNMAN_SERVICE_SECURITY_PSK:
3739                 case CONNMAN_SERVICE_SECURITY_WPA:
3740                 case CONNMAN_SERVICE_SECURITY_RSN:
3741                         if (service->passphrase == NULL) {
3742                                 if (service->network == NULL)
3743                                         return -EOPNOTSUPP;
3744
3745                                 if (service->wps == FALSE ||
3746                                         connman_network_get_bool(
3747                                                         service->network,
3748                                                         "WiFi.UseWPS") == FALSE)
3749                                         return -ENOKEY;
3750                         }
3751                         break;
3752                 case CONNMAN_SERVICE_SECURITY_8021X:
3753                         break;
3754                 }
3755                 break;
3756         }
3757
3758         if (service->network != NULL) {
3759                 if (prepare_network(service) == FALSE)
3760                         return -EINVAL;
3761
3762                 switch (service->security) {
3763                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3764                 case CONNMAN_SERVICE_SECURITY_NONE:
3765                 case CONNMAN_SERVICE_SECURITY_WEP:
3766                 case CONNMAN_SERVICE_SECURITY_PSK:
3767                 case CONNMAN_SERVICE_SECURITY_WPA:
3768                 case CONNMAN_SERVICE_SECURITY_RSN:
3769                         break;
3770                 case CONNMAN_SERVICE_SECURITY_8021X:
3771                         prepare_8021x(service);
3772                         break;
3773                 }
3774
3775                 if (__connman_stats_service_register(service) == 0) {
3776                         __connman_stats_get(service, FALSE,
3777                                                 &service->stats.data);
3778                         __connman_stats_get(service, TRUE,
3779                                                 &service->stats_roaming.data);
3780                 }
3781
3782                 if (service->ipconfig_ipv4)
3783                         __connman_ipconfig_enable(service->ipconfig_ipv4);
3784                 if (service->ipconfig_ipv6)
3785                         __connman_ipconfig_enable(service->ipconfig_ipv6);
3786
3787                 err = __connman_network_connect(service->network);
3788         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3789                                         service->provider != NULL)
3790                 err = __connman_provider_connect(service->provider);
3791         else
3792                 return -EOPNOTSUPP;
3793
3794         if (err < 0) {
3795                 if (err != -EINPROGRESS) {
3796                         __connman_ipconfig_disable(service->ipconfig_ipv4);
3797                         __connman_ipconfig_disable(service->ipconfig_ipv6);
3798                         __connman_stats_service_unregister(service);
3799                 }
3800         }
3801
3802         return err;
3803 }
3804
3805
3806 int __connman_service_connect(struct connman_service *service)
3807 {
3808         enum connman_service_state state;
3809         int err;
3810
3811         state = combine_state(service->state_ipv4, service->state_ipv6);
3812
3813         DBG("service %p state %s", service, state2string(state));
3814
3815         if (is_connected(service) == TRUE)
3816                 return -EISCONN;
3817
3818         if (is_connecting(service) == TRUE)
3819                 return -EALREADY;
3820
3821         switch (service->type) {
3822         case CONNMAN_SERVICE_TYPE_UNKNOWN:
3823         case CONNMAN_SERVICE_TYPE_SYSTEM:
3824         case CONNMAN_SERVICE_TYPE_GPS:
3825         case CONNMAN_SERVICE_TYPE_GADGET:
3826                 return -EINVAL;
3827         default:
3828                 err = service_connect(service);
3829         }
3830
3831         if (err >= 0)
3832                 return 0;
3833
3834         if (err == -EINPROGRESS) {
3835                 service->timeout = g_timeout_add_seconds(CONNECT_TIMEOUT,
3836                                                 connect_timeout, service);
3837
3838                 return -EINPROGRESS;
3839         }
3840
3841         if (err == -ENOKEY)
3842                 return -ENOKEY;
3843
3844         if (service->userconnect == TRUE)
3845                 reply_pending(service, err);
3846
3847         __connman_service_indicate_state(service,
3848                                         CONNMAN_SERVICE_STATE_FAILURE,
3849                                         CONNMAN_IPCONFIG_TYPE_IPV4);
3850         __connman_service_indicate_state(service,
3851                                         CONNMAN_SERVICE_STATE_FAILURE,
3852                                         CONNMAN_IPCONFIG_TYPE_IPV6);
3853
3854         return err;
3855 }
3856
3857 int __connman_service_disconnect(struct connman_service *service)
3858 {
3859         int err;
3860
3861         DBG("service %p", service);
3862
3863         if (service->network != NULL) {
3864                 err = __connman_network_disconnect(service->network);
3865         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3866                                         service->provider != NULL)
3867                 err = __connman_provider_disconnect(service->provider);
3868         else
3869                 return -EOPNOTSUPP;
3870
3871         if (err < 0 && err != -EINPROGRESS)
3872                 return err;
3873
3874         __connman_6to4_remove(service->ipconfig_ipv4);
3875
3876         if (service->ipconfig_ipv4)
3877                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
3878                                                         NULL);
3879         else
3880                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
3881                                                         NULL);
3882
3883         __connman_ipconfig_address_remove(service->ipconfig_ipv4);
3884         __connman_ipconfig_address_remove(service->ipconfig_ipv6);
3885
3886         __connman_ipconfig_disable(service->ipconfig_ipv4);
3887         __connman_ipconfig_disable(service->ipconfig_ipv6);
3888
3889         __connman_stats_service_unregister(service);
3890
3891         return err;
3892 }
3893
3894 int __connman_service_disconnect_all(void)
3895 {
3896         GSequenceIter *iter;
3897
3898         DBG("");
3899
3900         iter = g_sequence_get_begin_iter(service_list);
3901
3902         while (g_sequence_iter_is_end(iter) == FALSE) {
3903                 struct connman_service *service = g_sequence_get(iter);
3904
3905                 service->ignore = TRUE;
3906
3907                 set_reconnect_state(service, FALSE);
3908
3909                 __connman_service_disconnect(service);
3910
3911                 iter = g_sequence_iter_next(iter);
3912         }
3913
3914         return 0;
3915
3916 }
3917
3918 /**
3919  * __connman_service_lookup:
3920  * @pattern: search pattern
3921  * @path: return object path
3922  *
3923  * Look up a service path from a search pattern
3924  */
3925 int __connman_service_lookup(const char *pattern, const char **path)
3926 {
3927         GHashTableIter iter;
3928         gpointer key, value;
3929
3930         g_hash_table_iter_init(&iter, service_hash);
3931
3932         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
3933                 GSequenceIter *iter = value;
3934                 struct connman_service *service = g_sequence_get(iter);
3935
3936                 if (g_strcmp0(service->identifier, pattern) == 0 ||
3937                                 g_strcmp0(service->name, pattern) == 0) {
3938                         *path = (const char *) service->path;
3939                         return 0;
3940                 }
3941         }
3942
3943         return -ENXIO;
3944 }
3945
3946 /**
3947  * lookup_by_identifier:
3948  * @identifier: service identifier
3949  *
3950  * Look up a service by identifier (reference count will not be increased)
3951  */
3952 static struct connman_service *lookup_by_identifier(const char *identifier)
3953 {
3954         GSequenceIter *iter;
3955
3956         iter = g_hash_table_lookup(service_hash, identifier);
3957         if (iter != NULL)
3958                 return g_sequence_get(iter);
3959
3960         return NULL;
3961 }
3962
3963 static struct connman_network *create_hidden_wifi(struct connman_device *device,
3964                 const char *ssid, const char *mode, const char *security)
3965 {
3966         struct connman_network *network;
3967         char *name;
3968         int index;
3969         unsigned int i, ssid_len;
3970
3971         ssid_len = strlen(ssid);
3972         if (ssid_len < 1)
3973                 return NULL;
3974
3975         network = connman_network_create(NULL, CONNMAN_NETWORK_TYPE_WIFI);
3976         if (network == NULL)
3977                 return NULL;
3978
3979         connman_network_set_blob(network, "WiFi.SSID",
3980                                         (unsigned char *) ssid, ssid_len);
3981
3982         connman_network_set_string(network, "WiFi.Mode", mode);
3983         connman_network_set_string(network, "WiFi.Security", security);
3984
3985         name = g_try_malloc0(ssid_len + 1);
3986         if (name == NULL) {
3987                 connman_network_unref(network);
3988                 return NULL;
3989         }
3990
3991         for (i = 0; i < ssid_len; i++) {
3992                 if (g_ascii_isprint(ssid[i]))
3993                         name[i] = ssid[i];
3994                 else
3995                         name[i] = ' ';
3996         }
3997
3998         connman_network_set_name(network, name);
3999
4000         g_free(name);
4001
4002         index = connman_device_get_index(device);
4003         connman_network_set_index(network, index);
4004
4005         if (connman_device_add_network(device, network) < 0) {
4006                 connman_network_unref(network);
4007                 return NULL;
4008         }
4009
4010         connman_network_set_available(network, TRUE);
4011
4012         return network;
4013 }
4014
4015 int __connman_service_create_and_connect(DBusMessage *msg)
4016 {
4017         struct connman_service *service;
4018         struct connman_network *network;
4019         struct connman_device *device;
4020         DBusMessageIter iter, array;
4021         const char *mode = "managed", *security = "none", *group_security;
4022         const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4023         unsigned int ssid_len = 0;
4024         const char *ident;
4025         char *name, *group;
4026         gboolean created = FALSE;
4027         int err;
4028
4029         dbus_message_iter_init(msg, &iter);
4030         dbus_message_iter_recurse(&iter, &array);
4031
4032         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4033                 DBusMessageIter entry, value;
4034                 const char *key;
4035
4036                 dbus_message_iter_recurse(&array, &entry);
4037                 dbus_message_iter_get_basic(&entry, &key);
4038
4039                 dbus_message_iter_next(&entry);
4040                 dbus_message_iter_recurse(&entry, &value);
4041
4042                 switch (dbus_message_iter_get_arg_type(&value)) {
4043                 case DBUS_TYPE_STRING:
4044                         if (g_str_equal(key, "Type") == TRUE)
4045                                 dbus_message_iter_get_basic(&value, &type);
4046                         else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4047                                         g_str_equal(key, "Mode") == TRUE)
4048                                 dbus_message_iter_get_basic(&value, &mode);
4049                         else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4050                                         g_str_equal(key, "Security") == TRUE)
4051                                 dbus_message_iter_get_basic(&value, &security);
4052                         else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4053                                         g_str_equal(key, "Passphrase") == TRUE)
4054                                 dbus_message_iter_get_basic(&value, &passphrase);
4055                         else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4056                                         g_str_equal(key, "SSID") == TRUE)
4057                                 dbus_message_iter_get_basic(&value, &ssid);
4058                 }
4059
4060                 dbus_message_iter_next(&array);
4061         }
4062
4063         if (type == NULL)
4064                 return -EINVAL;
4065
4066         if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4067                 return -EOPNOTSUPP;
4068
4069         if (ssid == NULL)
4070                 return -EINVAL;
4071
4072         ssid_len = strlen(ssid);
4073         if (ssid_len < 1)
4074                 return -EINVAL;
4075
4076         if (g_strcmp0(security, "none") != 0 &&
4077                                 g_strcmp0(security, "wep") != 0 &&
4078                                 g_strcmp0(security, "psk") != 0 &&
4079                                 g_strcmp0(security, "wpa") != 0 &&
4080                                 g_strcmp0(security, "rsn") != 0 &&
4081                                 g_strcmp0(security, "ieee8021x") != 0)
4082                 return -EINVAL;
4083
4084         device = __connman_element_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4085         if (device == NULL)
4086                 return -EOPNOTSUPP;
4087
4088         ident = connman_device_get_ident(device);
4089         if (ident == NULL)
4090                 return -EOPNOTSUPP;
4091
4092
4093         if (!g_strcmp0(security, "wpa") ||
4094                 !g_strcmp0(security, "rsn"))
4095                 group_security = "psk";
4096         else
4097                 group_security = security;
4098
4099         group = connman_wifi_build_group_name((unsigned char *) ssid,
4100                                                 ssid_len, mode, group_security);
4101         if (group == NULL)
4102                 return -EINVAL;
4103
4104         name = g_strdup_printf("%s_%s_%s", type, ident, group);
4105
4106         service = lookup_by_identifier(name);
4107
4108         if (service != NULL)
4109                 goto done;
4110
4111         network = create_hidden_wifi(device, ssid, mode, security);
4112         if (network != NULL) {
4113                 connman_network_set_group(network, group);
4114                 created = TRUE;
4115         }
4116
4117         service = lookup_by_identifier(name);
4118
4119 done:
4120         g_free(name);
4121         g_free(group);
4122
4123         if (service == NULL) {
4124                 err = -EOPNOTSUPP;
4125                 goto failed;
4126         }
4127
4128         if (is_connected(service) == TRUE) {
4129                 err = -EISCONN;
4130                 goto failed;
4131         }
4132
4133         if (is_connecting(service) == TRUE) {
4134                 err = -EALREADY;
4135                 goto failed;
4136         }
4137
4138         set_reconnect_state(service, FALSE);
4139
4140         __connman_device_disconnect(device);
4141
4142         if (passphrase != NULL) {
4143                 g_free(service->passphrase);
4144                 service->passphrase = g_strdup(passphrase);
4145         }
4146
4147         service->userconnect = TRUE;
4148
4149         err = __connman_service_connect(service);
4150         if (err < 0 && err != -EINPROGRESS)
4151                 goto failed;
4152
4153         service->pending = dbus_message_ref(msg);
4154
4155         return 0;
4156
4157 failed:
4158         if (service != NULL && created == TRUE) {
4159                 struct connman_network *network = service->network;
4160
4161                 if (network != NULL) {
4162                         connman_network_set_available(network, FALSE);
4163                         __connman_device_cleanup_networks(device);
4164                 } else
4165                         __connman_service_put(service);
4166         }
4167
4168         return err;
4169 }
4170
4171 /**
4172  * __connman_service_get:
4173  * @identifier: service identifier
4174  *
4175  * Look up a service by identifier or create a new one if not found
4176  */
4177 static struct connman_service *service_get(const char *identifier)
4178 {
4179         struct connman_service *service;
4180         GSequenceIter *iter;
4181
4182         iter = g_hash_table_lookup(service_hash, identifier);
4183         if (iter != NULL) {
4184                 service = g_sequence_get(iter);
4185                 if (service != NULL)
4186                         connman_service_ref(service);
4187                 return service;
4188         }
4189
4190         service = connman_service_create();
4191         if (service == NULL)
4192                 return NULL;
4193
4194         DBG("service %p", service);
4195
4196         service->identifier = g_strdup(identifier);
4197
4198         service->profile = g_strdup(__connman_profile_active_ident());
4199
4200         iter = g_sequence_insert_sorted(service_list, service,
4201                                                 service_compare, NULL);
4202
4203         g_hash_table_insert(service_hash, service->identifier, iter);
4204
4205         return service;
4206 }
4207
4208 static int service_register(struct connman_service *service)
4209 {
4210         const char *path = __connman_profile_active_path();
4211         GSequenceIter *iter;
4212
4213         DBG("service %p", service);
4214
4215         if (service->path != NULL)
4216                 return -EALREADY;
4217
4218         service->path = g_strdup_printf("%s/%s", path, service->identifier);
4219
4220         DBG("path %s", service->path);
4221
4222         __connman_config_provision_service(service);
4223
4224         __connman_storage_load_service(service);
4225
4226         g_dbus_register_interface(connection, service->path,
4227                                         CONNMAN_SERVICE_INTERFACE,
4228                                         service_methods, service_signals,
4229                                                         NULL, service, NULL);
4230
4231         iter = g_hash_table_lookup(service_hash, service->identifier);
4232         if (iter != NULL)
4233                 g_sequence_sort_changed(iter, service_compare, NULL);
4234
4235         __connman_profile_changed(TRUE);
4236
4237         return 0;
4238 }
4239
4240 static void service_up(struct connman_ipconfig *ipconfig)
4241 {
4242         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4243
4244         connman_info("%s up", connman_ipconfig_get_ifname(ipconfig));
4245
4246         link_changed(service);
4247
4248         service->stats.valid = FALSE;
4249         service->stats_roaming.valid = FALSE;
4250 }
4251
4252 static void service_down(struct connman_ipconfig *ipconfig)
4253 {
4254         connman_info("%s down", connman_ipconfig_get_ifname(ipconfig));
4255 }
4256
4257 static void service_lower_up(struct connman_ipconfig *ipconfig)
4258 {
4259         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4260
4261         connman_info("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4262
4263         stats_start(service);
4264 }
4265
4266 static void service_lower_down(struct connman_ipconfig *ipconfig)
4267 {
4268         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4269
4270         connman_info("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4271
4272         stats_stop(service);
4273         __connman_storage_save_service(service);
4274 }
4275
4276 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4277 {
4278         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4279         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4280         enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4281
4282         connman_info("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4283
4284         type = __connman_ipconfig_get_config_type(ipconfig);
4285         method = __connman_ipconfig_get_method(ipconfig);
4286
4287         DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4288                                                         type, method);
4289
4290         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4291                         method == CONNMAN_IPCONFIG_METHOD_AUTO)
4292                 __connman_service_indicate_state(service,
4293                                                 CONNMAN_SERVICE_STATE_READY,
4294                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
4295
4296         settings_changed(service);
4297 }
4298
4299 static void service_ip_release(struct connman_ipconfig *ipconfig)
4300 {
4301         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4302         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4303         enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4304
4305         connman_info("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4306
4307         type = __connman_ipconfig_get_config_type(ipconfig);
4308         method = __connman_ipconfig_get_method(ipconfig);
4309
4310         DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4311                                                         type, method);
4312
4313         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4314                         method == CONNMAN_IPCONFIG_METHOD_OFF)
4315                 __connman_service_indicate_state(service,
4316                                         CONNMAN_SERVICE_STATE_DISCONNECT,
4317                                         CONNMAN_IPCONFIG_TYPE_IPV6);
4318
4319         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4320                         method == CONNMAN_IPCONFIG_METHOD_OFF)
4321                 __connman_service_indicate_state(service,
4322                                         CONNMAN_SERVICE_STATE_DISCONNECT,
4323                                         CONNMAN_IPCONFIG_TYPE_IPV4);
4324
4325         settings_changed(service);
4326 }
4327
4328 static const struct connman_ipconfig_ops service_ops = {
4329         .up             = service_up,
4330         .down           = service_down,
4331         .lower_up       = service_lower_up,
4332         .lower_down     = service_lower_down,
4333         .ip_bound       = service_ip_bound,
4334         .ip_release     = service_ip_release,
4335 };
4336
4337 static void setup_ip4config(struct connman_service *service, int index)
4338 {
4339         if (index < 0)
4340                 return;
4341
4342         service->ipconfig_ipv4 = connman_ipconfig_create(index,
4343                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
4344         if (service->ipconfig_ipv4 == NULL)
4345                 return;
4346
4347         connman_ipconfig_set_method(service->ipconfig_ipv4,
4348                                         CONNMAN_IPCONFIG_METHOD_DHCP);
4349
4350         connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4351
4352         connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4353 }
4354
4355 static void setup_ip6config(struct connman_service *service, int index)
4356 {
4357         if (index < 0)
4358                 return;
4359
4360         service->ipconfig_ipv6 = connman_ipconfig_create(index,
4361                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
4362         if (service->ipconfig_ipv6 == NULL)
4363                 return;
4364
4365         connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4366
4367         connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4368 }
4369
4370 void __connman_service_create_ip4config(struct connman_service *service,
4371                                                                 int index)
4372 {
4373         const char *ident = service->profile;
4374         GKeyFile *keyfile;
4375
4376         DBG("ipv4 %p", service->ipconfig_ipv4);
4377
4378         if (service->ipconfig_ipv4 != NULL)
4379                 return;
4380
4381         setup_ip4config(service, index);
4382
4383         if (ident == NULL)
4384                 return;
4385
4386         keyfile = __connman_storage_open_profile(ident);
4387         if (keyfile == NULL)
4388                 return;
4389
4390         if (service->ipconfig_ipv4)
4391                 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4392                                         service->identifier, "IPv4.");
4393         g_key_file_free(keyfile);
4394 }
4395
4396 void __connman_service_create_ip6config(struct connman_service *service,
4397                                                                 int index)
4398 {
4399         const char *ident = service->profile;
4400         GKeyFile *keyfile;
4401
4402         DBG("ipv6 %p", service->ipconfig_ipv6);
4403
4404         if (service->ipconfig_ipv6 != NULL)
4405                 return;
4406
4407         setup_ip6config(service, index);
4408
4409         if (ident == NULL)
4410                 return;
4411
4412         keyfile = __connman_storage_open_profile(ident);
4413         if (keyfile == NULL)
4414                 return;
4415
4416         if (service->ipconfig_ipv6 != NULL)
4417                 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4418                                         service->identifier, "IPv6.");
4419
4420         g_key_file_free(keyfile);
4421 }
4422
4423 /**
4424  * __connman_service_lookup_from_network:
4425  * @network: network structure
4426  *
4427  * Look up a service by network (reference count will not be increased)
4428  */
4429 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4430 {
4431         struct connman_service *service;
4432         const char *ident, *group;
4433         char *name;
4434
4435         DBG("network %p", network);
4436
4437         ident = __connman_network_get_ident(network);
4438         if (ident == NULL)
4439                 return NULL;
4440
4441         group = connman_network_get_group(network);
4442         if (group == NULL)
4443                 return NULL;
4444
4445         name = g_strdup_printf("%s_%s_%s",
4446                         __connman_network_get_type(network), ident, group);
4447         service = lookup_by_identifier(name);
4448         g_free(name);
4449
4450         return service;
4451 }
4452
4453 struct connman_service *__connman_service_lookup_from_index(int index)
4454 {
4455         struct connman_service *service;
4456         GSequenceIter *iter;
4457
4458         iter = g_sequence_get_begin_iter(service_list);
4459
4460         while (g_sequence_iter_is_end(iter) == FALSE) {
4461                 service = g_sequence_get(iter);
4462
4463                 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4464                                                         == index)
4465                         return service;
4466
4467                 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4468                                                         == index)
4469                         return service;
4470
4471                 iter = g_sequence_iter_next(iter);
4472         }
4473
4474         return NULL;
4475 }
4476
4477 const char *__connman_service_get_ident(struct connman_service *service)
4478 {
4479         return service->identifier;
4480 }
4481
4482 const char *__connman_service_get_path(struct connman_service *service)
4483 {
4484         return service->path;
4485 }
4486
4487 unsigned int __connman_service_get_order(struct connman_service *service)
4488 {
4489         GSequenceIter *iter;
4490
4491         if (service == NULL)
4492                 return 0;
4493
4494         if (service->favorite == FALSE) {
4495                 service->order = 0;
4496                 goto done;
4497         }
4498
4499         iter = g_hash_table_lookup(service_hash, service->identifier);
4500         if (iter != NULL) {
4501                 if (g_sequence_iter_get_position(iter) == 0)
4502                         service->order = 1;
4503                 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4504                         service->order = 10;
4505                 else
4506                         service->order = 0;
4507         }
4508
4509 done:
4510         return service->order;
4511 }
4512
4513 static enum connman_service_type convert_network_type(struct connman_network *network)
4514 {
4515         enum connman_network_type type = connman_network_get_type(network);
4516
4517         switch (type) {
4518         case CONNMAN_NETWORK_TYPE_UNKNOWN:
4519         case CONNMAN_NETWORK_TYPE_VENDOR:
4520                 break;
4521         case CONNMAN_NETWORK_TYPE_ETHERNET:
4522                 return CONNMAN_SERVICE_TYPE_ETHERNET;
4523         case CONNMAN_NETWORK_TYPE_WIFI:
4524                 return CONNMAN_SERVICE_TYPE_WIFI;
4525         case CONNMAN_NETWORK_TYPE_WIMAX:
4526                 return CONNMAN_SERVICE_TYPE_WIMAX;
4527         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4528         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4529                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4530         case CONNMAN_NETWORK_TYPE_CELLULAR:
4531                 return CONNMAN_SERVICE_TYPE_CELLULAR;
4532         }
4533
4534         return CONNMAN_SERVICE_TYPE_UNKNOWN;
4535 }
4536
4537 static enum connman_service_mode convert_wifi_mode(const char *mode)
4538 {
4539         if (mode == NULL)
4540                 return CONNMAN_SERVICE_MODE_UNKNOWN;
4541         else if (g_str_equal(mode, "managed") == TRUE)
4542                 return CONNMAN_SERVICE_MODE_MANAGED;
4543         else if (g_str_equal(mode, "adhoc") == TRUE)
4544                 return CONNMAN_SERVICE_MODE_ADHOC;
4545         else
4546                 return CONNMAN_SERVICE_MODE_UNKNOWN;
4547 }
4548
4549 static enum connman_service_security convert_wifi_security(const char *security)
4550 {
4551         if (security == NULL)
4552                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4553         else if (g_str_equal(security, "none") == TRUE)
4554                 return CONNMAN_SERVICE_SECURITY_NONE;
4555         else if (g_str_equal(security, "wep") == TRUE)
4556                 return CONNMAN_SERVICE_SECURITY_WEP;
4557         else if (g_str_equal(security, "psk") == TRUE)
4558                 return CONNMAN_SERVICE_SECURITY_PSK;
4559         else if (g_str_equal(security, "ieee8021x") == TRUE)
4560                 return CONNMAN_SERVICE_SECURITY_8021X;
4561         else if (g_str_equal(security, "wpa") == TRUE)
4562                 return CONNMAN_SERVICE_SECURITY_WPA;
4563         else if (g_str_equal(security, "rsn") == TRUE)
4564                 return CONNMAN_SERVICE_SECURITY_RSN;
4565         else
4566                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4567 }
4568
4569 static enum connman_service_mode convert_cellular_mode(connman_uint8_t mode)
4570 {
4571         switch (mode) {
4572         case 0:
4573         case 1:
4574                 return CONNMAN_SERVICE_MODE_GPRS;
4575         case 3:
4576                 return CONNMAN_SERVICE_MODE_EDGE;
4577         case 2:
4578         case 4:
4579         case 5:
4580         case 6:
4581                 return CONNMAN_SERVICE_MODE_UMTS;
4582         }
4583
4584         return CONNMAN_SERVICE_MODE_UNKNOWN;
4585 }
4586
4587 static void update_from_network(struct connman_service *service,
4588                                         struct connman_network *network)
4589 {
4590         connman_uint8_t strength = service->strength;
4591         GSequenceIter *iter;
4592         const char *str;
4593
4594         DBG("service %p network %p", service, network);
4595
4596         if (is_connected(service) == TRUE)
4597                 return;
4598
4599         if (is_connecting(service) == TRUE)
4600                 return;
4601
4602         str = connman_network_get_string(network, "Name");
4603         if (str != NULL) {
4604                 g_free(service->name);
4605                 service->name = g_strdup(str);
4606                 service->hidden = FALSE;
4607         } else {
4608                 g_free(service->name);
4609                 service->name = NULL;
4610                 service->hidden = TRUE;
4611         }
4612
4613         service->strength = connman_network_get_uint8(network, "Strength");
4614         service->roaming = connman_network_get_bool(network, "Roaming");
4615
4616         if (service->strength == 0) {
4617                 /*
4618                  * Filter out 0-values; it's unclear what they mean
4619                  * and they cause anomalous sorting of the priority list.
4620                  */
4621                 service->strength = strength;
4622         }
4623
4624         str = connman_network_get_string(network, "WiFi.Mode");
4625         service->mode = convert_wifi_mode(str);
4626
4627         str = connman_network_get_string(network, "WiFi.Security");
4628         service->security = convert_wifi_security(str);
4629
4630         if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
4631                 connman_uint8_t value = connman_network_get_uint8(network,
4632                                                         "Cellular.Mode");
4633
4634                 service->mode = convert_cellular_mode(value);
4635         } else if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4636                 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4637
4638         if (service->strength > strength && service->network != NULL) {
4639                 connman_network_unref(service->network);
4640                 service->network = connman_network_ref(network);
4641
4642                 strength_changed(service);
4643         }
4644
4645         if (service->network == NULL)
4646                 service->network = connman_network_ref(network);
4647
4648         iter = g_hash_table_lookup(service_hash, service->identifier);
4649         if (iter != NULL)
4650                 g_sequence_sort_changed(iter, service_compare, NULL);
4651 }
4652
4653 /**
4654  * __connman_service_create_from_network:
4655  * @network: network structure
4656  *
4657  * Look up service by network and if not found, create one
4658  */
4659 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4660 {
4661         struct connman_service *service;
4662         struct connman_device *device;
4663         const char *ident, *group;
4664         char *name;
4665         int index;
4666
4667         DBG("network %p", network);
4668
4669         ident = __connman_network_get_ident(network);
4670         if (ident == NULL)
4671                 return NULL;
4672
4673         group = connman_network_get_group(network);
4674         if (group == NULL)
4675                 return NULL;
4676
4677         name = g_strdup_printf("%s_%s_%s",
4678                         __connman_network_get_type(network), ident, group);
4679         service = service_get(name);
4680         g_free(name);
4681
4682         if (service == NULL)
4683                 return NULL;
4684
4685         if (__connman_network_get_weakness(network) == TRUE)
4686                 return service;
4687
4688         if (service->path != NULL) {
4689                 update_from_network(service, network);
4690                 __connman_profile_changed(TRUE);
4691                 return service;
4692         }
4693
4694         service->type = convert_network_type(network);
4695
4696         switch (service->type) {
4697         case CONNMAN_SERVICE_TYPE_UNKNOWN:
4698         case CONNMAN_SERVICE_TYPE_SYSTEM:
4699         case CONNMAN_SERVICE_TYPE_ETHERNET:
4700         case CONNMAN_SERVICE_TYPE_WIMAX:
4701         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4702         case CONNMAN_SERVICE_TYPE_GPS:
4703         case CONNMAN_SERVICE_TYPE_VPN:
4704         case CONNMAN_SERVICE_TYPE_GADGET:
4705                 service->autoconnect = FALSE;
4706                 break;
4707         case CONNMAN_SERVICE_TYPE_WIFI:
4708         case CONNMAN_SERVICE_TYPE_CELLULAR:
4709                 service->autoconnect = TRUE;
4710                 break;
4711         }
4712
4713         service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4714
4715         update_from_network(service, network);
4716
4717         index = connman_network_get_index(network);
4718
4719         if (service->ipconfig_ipv4 == NULL)
4720                 setup_ip4config(service, index);
4721
4722         if (service->ipconfig_ipv6 == NULL)
4723                 setup_ip6config(service, index);
4724
4725         service_register(service);
4726
4727         if (service->favorite == TRUE) {
4728                 device = connman_network_get_device(service->network);
4729                 if (device && __connman_device_scanning(device) == FALSE)
4730                         __connman_service_auto_connect();
4731         }
4732
4733         return service;
4734 }
4735
4736 void __connman_service_update_from_network(struct connman_network *network)
4737 {
4738         struct connman_service *service;
4739         enum connman_service_mode mode;
4740         connman_uint8_t strength, value;
4741         connman_bool_t roaming;
4742         GSequenceIter *iter;
4743         const char *name;
4744         connman_bool_t stats_enable;
4745
4746         DBG("network %p", network);
4747
4748         service = __connman_service_lookup_from_network(network);
4749         if (service == NULL)
4750                 return;
4751
4752         if (service->network == NULL)
4753                 return;
4754
4755         name = connman_network_get_string(service->network, "Name");
4756         if (g_strcmp0(service->name, name) != 0) {
4757                 g_free(service->name);
4758                 service->name = g_strdup(name);
4759                 connman_dbus_property_changed_basic(service->path,
4760                                 CONNMAN_SERVICE_INTERFACE, "Name",
4761                                 DBUS_TYPE_STRING, &service->name);
4762         }
4763
4764         if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4765                 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4766
4767         strength = connman_network_get_uint8(service->network, "Strength");
4768         if (strength == service->strength)
4769                 goto roaming;
4770
4771         service->strength = strength;
4772
4773         strength_changed(service);
4774
4775 roaming:
4776         roaming = connman_network_get_bool(service->network, "Roaming");
4777         if (roaming == service->roaming)
4778                 goto done;
4779
4780         stats_enable = stats_enabled(service);
4781         if (stats_enable == TRUE)
4782                 stats_stop(service);
4783
4784         service->roaming = roaming;
4785
4786         if (stats_enable == TRUE)
4787                 stats_start(service);
4788
4789         roaming_changed(service);
4790
4791         iter = g_hash_table_lookup(service_hash, service->identifier);
4792         if (iter != NULL)
4793                 g_sequence_sort_changed(iter, service_compare, NULL);
4794
4795 done:
4796         if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
4797                 return;
4798
4799         value = connman_network_get_uint8(service->network, "Cellular.Mode");
4800         mode = convert_cellular_mode(value);
4801
4802         if (mode == service->mode)
4803                 return;
4804
4805         service->mode = mode;
4806
4807         mode_changed(service);
4808 }
4809
4810 void __connman_service_remove_from_network(struct connman_network *network)
4811 {
4812         struct connman_service *service;
4813
4814         DBG("network %p", network);
4815
4816         service = __connman_service_lookup_from_network(network);
4817         if (service == NULL)
4818                 return;
4819
4820         __connman_service_put(service);
4821 }
4822
4823 /**
4824  * __connman_service_create_from_provider:
4825  * @provider: provider structure
4826  *
4827  * Look up service by provider and if not found, create one
4828  */
4829 struct connman_service *
4830 __connman_service_create_from_provider(struct connman_provider *provider)
4831 {
4832         struct connman_service *service;
4833         const char *ident, *str;
4834         char *name;
4835         int index = connman_provider_get_index(provider);
4836
4837         DBG("provider %p", provider);
4838
4839         ident = __connman_provider_get_ident(provider);
4840         if (ident == NULL)
4841                 return NULL;
4842
4843         name = g_strdup_printf("vpn_%s", ident);
4844         service = service_get(name);
4845         g_free(name);
4846
4847         if (service == NULL)
4848                 return NULL;
4849
4850         service->type = CONNMAN_SERVICE_TYPE_VPN;
4851         service->provider = connman_provider_ref(provider);
4852         service->autoconnect = FALSE;
4853
4854         service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4855
4856         str = connman_provider_get_string(provider, "Name");
4857         if (str != NULL) {
4858                 g_free(service->name);
4859                 service->name = g_strdup(str);
4860                 service->hidden = FALSE;
4861         } else {
4862                 g_free(service->name);
4863                 service->name = NULL;
4864                 service->hidden = TRUE;
4865         }
4866
4867         service->strength = 0;
4868
4869         if (service->ipconfig_ipv4 == NULL)
4870                 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4871                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
4872         if (service->ipconfig_ipv4 == NULL)
4873                 return service;
4874
4875         connman_ipconfig_set_method(service->ipconfig_ipv4,
4876                                         CONNMAN_IPCONFIG_METHOD_MANUAL);
4877         connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4878         connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4879
4880         if (service->ipconfig_ipv6 == NULL)
4881                 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4882                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
4883         if (service->ipconfig_ipv6 == NULL)
4884                 return service;
4885
4886         connman_ipconfig_set_method(service->ipconfig_ipv6,
4887                                         CONNMAN_IPCONFIG_METHOD_OFF);
4888         connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4889         connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4890
4891         service_register(service);
4892
4893         return service;
4894 }
4895
4896 static int service_load(struct connman_service *service)
4897 {
4898         const char *ident = service->profile;
4899         GKeyFile *keyfile;
4900         GError *error = NULL;
4901         gchar *pathname, *data = NULL;
4902         gsize length;
4903         gchar *str;
4904         connman_bool_t autoconnect;
4905         unsigned int ssid_len;
4906         int err = 0;
4907
4908         DBG("service %p", service);
4909
4910         if (ident == NULL)
4911                 return -EINVAL;
4912
4913         pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
4914         if (pathname == NULL)
4915                 return -ENOMEM;
4916
4917         keyfile = g_key_file_new();
4918
4919         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
4920                 g_free(pathname);
4921                 return -ENOENT;
4922         }
4923
4924         g_free(pathname);
4925
4926         if (g_key_file_load_from_data(keyfile, data, length,
4927                                                         0, NULL) == FALSE) {
4928                 g_free(data);
4929                 return -EILSEQ;
4930         }
4931
4932         g_free(data);
4933
4934         switch (service->type) {
4935         case CONNMAN_SERVICE_TYPE_UNKNOWN:
4936         case CONNMAN_SERVICE_TYPE_SYSTEM:
4937         case CONNMAN_SERVICE_TYPE_ETHERNET:
4938         case CONNMAN_SERVICE_TYPE_GPS:
4939         case CONNMAN_SERVICE_TYPE_VPN:
4940         case CONNMAN_SERVICE_TYPE_GADGET:
4941                 break;
4942         case CONNMAN_SERVICE_TYPE_WIFI:
4943                 if (service->name == NULL) {
4944                         gchar *name;
4945
4946                         name = g_key_file_get_string(keyfile,
4947                                         service->identifier, "Name", NULL);
4948                         if (name != NULL) {
4949                                 g_free(service->name);
4950                                 service->name = name;
4951                         }
4952
4953                         if (service->network != NULL)
4954                                 connman_network_set_name(service->network,
4955                                                                         name);
4956                 }
4957
4958                 if (service->network &&
4959                                 connman_network_get_blob(service->network,
4960                                         "WiFi.SSID", &ssid_len) == NULL) {
4961                         gchar *hex_ssid;
4962
4963                         hex_ssid = g_key_file_get_string(keyfile,
4964                                                         service->identifier,
4965                                                                 "SSID", NULL);
4966
4967                         if (hex_ssid != NULL) {
4968                                 gchar *ssid;
4969                                 unsigned int i, j = 0, hex;
4970                                 size_t hex_ssid_len = strlen(hex_ssid);
4971
4972                                 ssid = g_try_malloc0(hex_ssid_len / 2);
4973                                 if (ssid == NULL) {
4974                                         g_free(hex_ssid);
4975                                         err = -ENOMEM;
4976                                         goto done;
4977                                 }
4978
4979                                 for (i = 0; i < hex_ssid_len; i += 2) {
4980                                         sscanf(hex_ssid + i, "%02x", &hex);
4981                                         ssid[j++] = hex;
4982                                 }
4983
4984                                 connman_network_set_blob(service->network,
4985                                         "WiFi.SSID", ssid, hex_ssid_len / 2);
4986                         }
4987
4988                         g_free(hex_ssid);
4989                 }
4990                 /* fall through */
4991
4992         case CONNMAN_SERVICE_TYPE_WIMAX:
4993         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4994         case CONNMAN_SERVICE_TYPE_CELLULAR:
4995                 service->favorite = g_key_file_get_boolean(keyfile,
4996                                 service->identifier, "Favorite", NULL);
4997
4998                 autoconnect = g_key_file_get_boolean(keyfile,
4999                                 service->identifier, "AutoConnect", &error);
5000                 if (error == NULL)
5001                         service->autoconnect = autoconnect;
5002                 g_clear_error(&error);
5003
5004                 str = g_key_file_get_string(keyfile,
5005                                 service->identifier, "Failure", NULL);
5006                 if (str != NULL) {
5007                         if (service->favorite == FALSE)
5008                                 service->state_ipv4 = service->state_ipv6 =
5009                                         CONNMAN_SERVICE_STATE_FAILURE;
5010                         service->error = string2error(str);
5011                 }
5012                 break;
5013         }
5014
5015         str = g_key_file_get_string(keyfile,
5016                                 service->identifier, "Modified", NULL);
5017         if (str != NULL) {
5018                 g_time_val_from_iso8601(str, &service->modified);
5019                 g_free(str);
5020         }
5021
5022         str = g_key_file_get_string(keyfile,
5023                                 service->identifier, "Passphrase", NULL);
5024         if (str != NULL) {
5025                 g_free(service->passphrase);
5026                 service->passphrase = str;
5027         }
5028
5029         if (service->ipconfig_ipv4 != NULL)
5030                 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5031                                         service->identifier, "IPv4.");
5032
5033         if (service->ipconfig_ipv6 != NULL)
5034                 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5035                                         service->identifier, "IPv6.");
5036
5037         service->nameservers_config = g_key_file_get_string_list(keyfile,
5038                         service->identifier, "Nameservers", &length, NULL);
5039         if (service->nameservers_config != NULL && length == 0) {
5040                 g_strfreev(service->nameservers_config);
5041                 service->nameservers_config = NULL;
5042         }
5043
5044         service->domains = g_key_file_get_string_list(keyfile,
5045                         service->identifier, "Domains", &length, NULL);
5046         if (service->domains != NULL && length == 0) {
5047                 g_strfreev(service->domains);
5048                 service->domains = NULL;
5049         }
5050
5051         str = g_key_file_get_string(keyfile,
5052                                 service->identifier, "Proxy.Method", NULL);
5053         if (str != NULL)
5054                 service->proxy_config = string2proxymethod(str);
5055
5056         g_free(str);
5057
5058         service->proxies = g_key_file_get_string_list(keyfile,
5059                         service->identifier, "Proxy.Servers", &length, NULL);
5060         if (service->proxies != NULL && length == 0) {
5061                 g_strfreev(service->proxies);
5062                 service->proxies = NULL;
5063         }
5064
5065         service->excludes = g_key_file_get_string_list(keyfile,
5066                         service->identifier, "Proxy.Excludes", &length, NULL);
5067         if (service->excludes != NULL && length == 0) {
5068                 g_strfreev(service->excludes);
5069                 service->excludes = NULL;
5070         }
5071
5072         str = g_key_file_get_string(keyfile,
5073                                 service->identifier, "Proxy.URL", NULL);
5074         if (str != NULL) {
5075                 g_free(service->pac);
5076                 service->pac = str;
5077         }
5078
5079 done:
5080         g_key_file_free(keyfile);
5081
5082         return err;
5083 }
5084
5085 static int service_save(struct connman_service *service)
5086 {
5087         const char *ident = service->profile;
5088         GKeyFile *keyfile;
5089         gchar *pathname, *data = NULL;
5090         gsize length;
5091         gchar *str;
5092         const char *cst_str = NULL;
5093         int err = 0;
5094
5095         DBG("service %p", service);
5096
5097         if (ident == NULL)
5098                 return -EINVAL;
5099
5100         pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5101         if (pathname == NULL)
5102                 return -ENOMEM;
5103
5104         keyfile = g_key_file_new();
5105
5106         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
5107                 goto update;
5108
5109         if (length > 0) {
5110                 if (g_key_file_load_from_data(keyfile, data, length,
5111                                                         0, NULL) == FALSE)
5112                         goto done;
5113         }
5114
5115         g_free(data);
5116
5117 update:
5118         if (service->name != NULL)
5119                 g_key_file_set_string(keyfile, service->identifier,
5120                                                 "Name", service->name);
5121
5122         switch (service->type) {
5123         case CONNMAN_SERVICE_TYPE_UNKNOWN:
5124         case CONNMAN_SERVICE_TYPE_SYSTEM:
5125         case CONNMAN_SERVICE_TYPE_ETHERNET:
5126         case CONNMAN_SERVICE_TYPE_GPS:
5127         case CONNMAN_SERVICE_TYPE_VPN:
5128         case CONNMAN_SERVICE_TYPE_GADGET:
5129                 break;
5130         case CONNMAN_SERVICE_TYPE_WIFI:
5131                 if (service->network) {
5132                         const unsigned char *ssid;
5133                         unsigned int ssid_len = 0;
5134
5135                         ssid = connman_network_get_blob(service->network,
5136                                                         "WiFi.SSID", &ssid_len);
5137
5138                         if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
5139                                 char *identifier = service->identifier;
5140                                 GString *str;
5141                                 unsigned int i;
5142
5143                                 str = g_string_sized_new(ssid_len * 2);
5144                                 if (str == NULL) {
5145                                         err = -ENOMEM;
5146                                         goto done;
5147                                 }
5148
5149                                 for (i = 0; i < ssid_len; i++)
5150                                         g_string_append_printf(str,
5151                                                         "%02x", ssid[i]);
5152
5153                                 g_key_file_set_string(keyfile, identifier,
5154                                                         "SSID", str->str);
5155
5156                                 g_string_free(str, TRUE);
5157                         }
5158                 }
5159                 /* fall through */
5160
5161         case CONNMAN_SERVICE_TYPE_WIMAX:
5162         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5163         case CONNMAN_SERVICE_TYPE_CELLULAR:
5164                 g_key_file_set_boolean(keyfile, service->identifier,
5165                                         "Favorite", service->favorite);
5166
5167                 if (service->favorite == TRUE)
5168                         g_key_file_set_boolean(keyfile, service->identifier,
5169                                         "AutoConnect", service->autoconnect);
5170
5171                 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
5172                         service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
5173                         const char *failure = error2string(service->error);
5174                         if (failure != NULL)
5175                                 g_key_file_set_string(keyfile,
5176                                                         service->identifier,
5177                                                         "Failure", failure);
5178                 } else {
5179                         g_key_file_remove_key(keyfile, service->identifier,
5180                                                         "Failure", NULL);
5181                 }
5182                 break;
5183         }
5184
5185         str = g_time_val_to_iso8601(&service->modified);
5186         if (str != NULL) {
5187                 g_key_file_set_string(keyfile, service->identifier,
5188                                                         "Modified", str);
5189                 g_free(str);
5190         }
5191
5192         if (service->passphrase != NULL && strlen(service->passphrase) > 0)
5193                 g_key_file_set_string(keyfile, service->identifier,
5194                                         "Passphrase", service->passphrase);
5195         else
5196                 g_key_file_remove_key(keyfile, service->identifier,
5197                                                         "Passphrase", NULL);
5198
5199         if (service->ipconfig_ipv4 != NULL)
5200                 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
5201                                         service->identifier, "IPv4.");
5202
5203         if (service->ipconfig_ipv6 != NULL)
5204                 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
5205                                                 service->identifier, "IPv6.");
5206
5207         if (service->nameservers_config != NULL) {
5208                 guint len = g_strv_length(service->nameservers_config);
5209
5210                 g_key_file_set_string_list(keyfile, service->identifier,
5211                                                                 "Nameservers",
5212                                 (const gchar **) service->nameservers_config, len);
5213         } else
5214                 g_key_file_remove_key(keyfile, service->identifier,
5215                                                         "Nameservers", NULL);
5216
5217         if (service->domains != NULL) {
5218                 guint len = g_strv_length(service->domains);
5219
5220                 g_key_file_set_string_list(keyfile, service->identifier,
5221                                                                 "Domains",
5222                                 (const gchar **) service->domains, len);
5223         } else
5224                 g_key_file_remove_key(keyfile, service->identifier,
5225                                                         "Domains", NULL);
5226
5227         cst_str = proxymethod2string(service->proxy_config);
5228         if (cst_str != NULL)
5229                 g_key_file_set_string(keyfile, service->identifier,
5230                                 "Proxy.Method", cst_str);
5231
5232         if (service->proxies != NULL) {
5233                 guint len = g_strv_length(service->proxies);
5234
5235                 g_key_file_set_string_list(keyfile, service->identifier,
5236                                 "Proxy.Servers",
5237                                 (const gchar **) service->proxies, len);
5238         } else
5239                 g_key_file_remove_key(keyfile, service->identifier,
5240                                                 "Proxy.Servers", NULL);
5241
5242         if (service->excludes != NULL) {
5243                 guint len = g_strv_length(service->excludes);
5244
5245                 g_key_file_set_string_list(keyfile, service->identifier,
5246                                 "Proxy.Excludes",
5247                                 (const gchar **) service->excludes, len);
5248         } else
5249                 g_key_file_remove_key(keyfile, service->identifier,
5250                                                 "Proxy.Excludes", NULL);
5251
5252         if (service->pac != NULL && strlen(service->pac) > 0)
5253                 g_key_file_set_string(keyfile, service->identifier,
5254                                         "Proxy.URL", service->pac);
5255         else
5256                 g_key_file_remove_key(keyfile, service->identifier,
5257                                                         "Proxy.URL", NULL);
5258
5259         data = g_key_file_to_data(keyfile, &length, NULL);
5260
5261         if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
5262                 connman_error("Failed to store service information");
5263
5264 done:
5265         g_free(data);
5266
5267         g_key_file_free(keyfile);
5268
5269         g_free(pathname);
5270
5271         return err;
5272 }
5273
5274 static struct connman_storage service_storage = {
5275         .name           = "service",
5276         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
5277         .service_load   = service_load,
5278         .service_save   = service_save,
5279 };
5280
5281 int __connman_service_init(void)
5282 {
5283         DBG("");
5284
5285         connection = connman_dbus_get_connection();
5286
5287         if (connman_storage_register(&service_storage) < 0)
5288                 connman_error("Failed to register service storage");
5289
5290         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5291                                                                 NULL, NULL);
5292
5293         service_list = g_sequence_new(service_free);
5294
5295         return 0;
5296 }
5297
5298 void __connman_service_cleanup(void)
5299 {
5300         DBG("");
5301
5302         g_sequence_free(service_list);
5303         service_list = NULL;
5304
5305         g_hash_table_destroy(service_hash);
5306         service_hash = NULL;
5307
5308         g_slist_free(counter_list);
5309         counter_list = NULL;
5310
5311         connman_storage_unregister(&service_storage);
5312
5313         dbus_connection_unref(connection);
5314 }