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