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