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