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