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