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