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