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