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