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