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