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