service: Extend debug message for session refcounting
[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 ref count %d", service,
1632                 g_atomic_int_get(&service->session_usage_count) + 1);
1633
1634         g_atomic_int_inc(&service->session_usage_count);
1635 }
1636
1637 connman_bool_t __connman_service_session_dec(struct connman_service *service)
1638 {
1639         DBG("service %p ref count %d", service,
1640                 g_atomic_int_get(&service->session_usage_count) - 1);
1641
1642         if (g_atomic_int_dec_and_test(&service->session_usage_count) == FALSE)
1643                 return FALSE;
1644
1645         return TRUE;
1646 }
1647
1648 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1649                                         struct connman_service *service)
1650 {
1651         dbus_bool_t required;
1652         const char *str;
1653
1654         str = __connman_service_type2string(service->type);
1655         if (str != NULL)
1656                 connman_dbus_dict_append_basic(dict, "Type",
1657                                                 DBUS_TYPE_STRING, &str);
1658
1659         connman_dbus_dict_append_array(dict, "Security",
1660                                 DBUS_TYPE_STRING, append_security, service);
1661
1662         str = state2string(service->state);
1663         if (str != NULL)
1664                 connman_dbus_dict_append_basic(dict, "State",
1665                                                 DBUS_TYPE_STRING, &str);
1666
1667         str = error2string(service->error);
1668         if (str != NULL)
1669                 connman_dbus_dict_append_basic(dict, "Error",
1670                                                 DBUS_TYPE_STRING, &str);
1671
1672         if (service->strength > 0)
1673                 connman_dbus_dict_append_basic(dict, "Strength",
1674                                         DBUS_TYPE_BYTE, &service->strength);
1675
1676         connman_dbus_dict_append_basic(dict, "Favorite",
1677                                         DBUS_TYPE_BOOLEAN, &service->favorite);
1678
1679         connman_dbus_dict_append_basic(dict, "Immutable",
1680                                         DBUS_TYPE_BOOLEAN, &service->immutable);
1681
1682         if (service->favorite == TRUE)
1683                 connman_dbus_dict_append_basic(dict, "AutoConnect",
1684                                 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1685         else
1686                 connman_dbus_dict_append_basic(dict, "AutoConnect",
1687                                         DBUS_TYPE_BOOLEAN, &service->favorite);
1688
1689         if (service->name != NULL)
1690                 connman_dbus_dict_append_basic(dict, "Name",
1691                                         DBUS_TYPE_STRING, &service->name);
1692
1693         connman_dbus_dict_append_basic(dict, "LoginRequired",
1694                                 DBUS_TYPE_BOOLEAN, &service->login_required);
1695
1696         switch (service->type) {
1697         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1698         case CONNMAN_SERVICE_TYPE_SYSTEM:
1699         case CONNMAN_SERVICE_TYPE_GPS:
1700         case CONNMAN_SERVICE_TYPE_VPN:
1701         case CONNMAN_SERVICE_TYPE_GADGET:
1702                 break;
1703         case CONNMAN_SERVICE_TYPE_CELLULAR:
1704                 connman_dbus_dict_append_basic(dict, "Roaming",
1705                                         DBUS_TYPE_BOOLEAN, &service->roaming);
1706
1707                 required = FALSE;
1708                 connman_dbus_dict_append_basic(dict, "SetupRequired",
1709                                                 DBUS_TYPE_BOOLEAN, &required);
1710                 connman_dbus_dict_append_dict(dict, "Ethernet",
1711                                                 append_ethernet, service);
1712                 break;
1713         case CONNMAN_SERVICE_TYPE_WIFI:
1714                 if (service->passphrase != NULL && limited == FALSE)
1715                         connman_dbus_dict_append_basic(dict, "Passphrase",
1716                                 DBUS_TYPE_STRING, &service->passphrase);
1717
1718                 required = FALSE;
1719
1720                 switch (service->security) {
1721                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1722                 case CONNMAN_SERVICE_SECURITY_NONE:
1723                         break;
1724                 case CONNMAN_SERVICE_SECURITY_WEP:
1725                 case CONNMAN_SERVICE_SECURITY_PSK:
1726                 case CONNMAN_SERVICE_SECURITY_WPA:
1727                 case CONNMAN_SERVICE_SECURITY_RSN:
1728                         if (service->passphrase == NULL)
1729                                 required = TRUE;
1730                         break;
1731                 case CONNMAN_SERVICE_SECURITY_8021X:
1732                         break;
1733                 }
1734
1735                 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
1736                                                 DBUS_TYPE_BOOLEAN, &required);
1737
1738                 /* fall through */
1739         case CONNMAN_SERVICE_TYPE_ETHERNET:
1740         case CONNMAN_SERVICE_TYPE_WIMAX:
1741         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1742                 connman_dbus_dict_append_dict(dict, "Ethernet",
1743                                                 append_ethernet, service);
1744                 break;
1745         }
1746
1747         connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1748
1749         connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1750                                                 append_ipv4config, service);
1751
1752         connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1753
1754         connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1755                                                 append_ipv6config, service);
1756
1757         connman_dbus_dict_append_array(dict, "Nameservers",
1758                                 DBUS_TYPE_STRING, append_dns, service);
1759
1760         connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1761                                 DBUS_TYPE_STRING, append_dnsconfig, service);
1762
1763         connman_dbus_dict_append_array(dict, "Domains",
1764                                 DBUS_TYPE_STRING, append_domain, service);
1765
1766         connman_dbus_dict_append_array(dict, "Domains.Configuration",
1767                                 DBUS_TYPE_STRING, append_domainconfig, service);
1768
1769         connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1770
1771         connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1772                                                 append_proxyconfig, service);
1773
1774         connman_dbus_dict_append_dict(dict, "Provider",
1775                                                 append_provider, service);
1776 }
1777
1778 static void append_struct(gpointer value, gpointer user_data)
1779 {
1780         struct connman_service *service = value;
1781         DBusMessageIter *iter = user_data;
1782         DBusMessageIter entry, dict;
1783
1784         if (service->path == NULL || service->hidden == TRUE)
1785                 return;
1786
1787         dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
1788
1789         dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
1790                                                         &service->path);
1791
1792         connman_dbus_dict_open(&entry, &dict);
1793         append_properties(&dict, TRUE, service);
1794         connman_dbus_dict_close(&entry, &dict);
1795
1796         dbus_message_iter_close_container(iter, &entry);
1797 }
1798
1799 void __connman_service_list_struct(DBusMessageIter *iter)
1800 {
1801         g_sequence_foreach(service_list, append_struct, iter);
1802 }
1803
1804 int __connman_service_get_index(struct connman_service *service)
1805 {
1806         if (service == NULL)
1807                 return -1;
1808
1809         if (service->network != NULL)
1810                 return connman_network_get_index(service->network);
1811         else if (service->provider != NULL)
1812                 return connman_provider_get_index(service->provider);
1813
1814         return -1;
1815 }
1816
1817 void __connman_service_set_domainname(struct connman_service *service,
1818                                                 const char *domainname)
1819 {
1820         if (service == NULL)
1821                 return;
1822
1823         g_free(service->domainname);
1824         service->domainname = g_strdup(domainname);
1825
1826         domain_changed(service);
1827 }
1828
1829 const char *connman_service_get_domainname(struct connman_service *service)
1830 {
1831         if (service == NULL)
1832                 return NULL;
1833
1834         if (service->domains != NULL)
1835                 return service->domains[0];
1836         else
1837                 return service->domainname;
1838 }
1839
1840 char **connman_service_get_nameservers(struct connman_service *service)
1841 {
1842         if (service == NULL)
1843                 return NULL;
1844
1845         if (service->nameservers_config != NULL)
1846                 return service->nameservers_config;
1847         else if (service->nameservers != NULL)
1848                 return service->nameservers;
1849
1850         return NULL;
1851 }
1852
1853 void connman_service_set_proxy_method(struct connman_service *service,
1854                                         enum connman_service_proxy_method method)
1855 {
1856         if (service == NULL)
1857                 return;
1858
1859         service->proxy = method;
1860
1861         proxy_changed(service);
1862
1863         if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
1864                 __connman_notifier_proxy_changed(service);
1865 }
1866
1867 enum connman_service_proxy_method connman_service_get_proxy_method(
1868                                         struct connman_service *service)
1869 {
1870         if (service == NULL)
1871                 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
1872
1873         if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
1874                 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
1875                                 service->pac == NULL)
1876                         return service->proxy;
1877
1878                 return service->proxy_config;
1879         }
1880
1881         return service->proxy;
1882 }
1883
1884 char **connman_service_get_proxy_servers(struct connman_service *service)
1885 {
1886         return g_strdupv(service->proxies);
1887 }
1888
1889 char **connman_service_get_proxy_excludes(struct connman_service *service)
1890 {
1891         return g_strdupv(service->excludes);
1892 }
1893
1894 const char *connman_service_get_proxy_url(struct connman_service *service)
1895 {
1896         if (service == NULL)
1897                 return NULL;
1898
1899         return service->pac;
1900 }
1901
1902 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
1903                                                         const char *url)
1904 {
1905         if (service == NULL)
1906                 return;
1907
1908         service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
1909
1910         if (service->ipconfig_ipv4) {
1911                 if (__connman_ipconfig_set_proxy_autoconfig(
1912                             service->ipconfig_ipv4, url) < 0)
1913                         return;
1914         } else if (service->ipconfig_ipv6) {
1915                 if (__connman_ipconfig_set_proxy_autoconfig(
1916                             service->ipconfig_ipv6, url) < 0)
1917                         return;
1918         } else
1919                 return;
1920
1921         proxy_changed(service);
1922
1923         __connman_notifier_proxy_changed(service);
1924 }
1925
1926 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
1927 {
1928         if (service == NULL)
1929                 return NULL;
1930
1931         if (service->ipconfig_ipv4)
1932                 return __connman_ipconfig_get_proxy_autoconfig(
1933                                                 service->ipconfig_ipv4);
1934         else if (service->ipconfig_ipv6)
1935                 return __connman_ipconfig_get_proxy_autoconfig(
1936                                                 service->ipconfig_ipv6);
1937         return NULL;
1938 }
1939
1940 static void update_timeservers(struct connman_service *service)
1941 {
1942         int i;
1943
1944         if (service->timeservers == NULL)
1945                 return;
1946
1947         switch (service->state) {
1948         case CONNMAN_SERVICE_STATE_UNKNOWN:
1949         case CONNMAN_SERVICE_STATE_IDLE:
1950         case CONNMAN_SERVICE_STATE_ASSOCIATION:
1951         case CONNMAN_SERVICE_STATE_CONFIGURATION:
1952                 return;
1953         case CONNMAN_SERVICE_STATE_FAILURE:
1954         case CONNMAN_SERVICE_STATE_DISCONNECT:
1955                 for (i = 0; service->timeservers[i] != NULL; i++)
1956                         connman_timeserver_remove(service->timeservers[i]);
1957                 return;
1958         case CONNMAN_SERVICE_STATE_READY:
1959         case CONNMAN_SERVICE_STATE_ONLINE:
1960                 break;
1961         }
1962
1963         for (i = 0; service->timeservers[i] != NULL; i++)
1964                 connman_timeserver_append(service->timeservers[i]);
1965 }
1966
1967 int __connman_service_timeserver_append(struct connman_service *service,
1968                                                 const char *timeserver)
1969 {
1970         int len;
1971
1972         DBG("service %p timeserver %s", service, timeserver);
1973
1974         if (timeserver == NULL)
1975                 return -EINVAL;
1976
1977         if (service->timeservers != NULL) {
1978                 int i;
1979
1980                 for (i = 0; service->timeservers[i] != NULL; i++)
1981                         if (g_strcmp0(service->timeservers[i], timeserver) == 0)
1982                                 return -EEXIST;
1983
1984                 len = g_strv_length(service->timeservers);
1985                 service->timeservers = g_try_renew(char *, service->timeservers,
1986                                                         len + 2);
1987         } else {
1988                 len = 0;
1989                 service->timeservers = g_try_new0(char *, len + 2);
1990         }
1991
1992         if (service->timeservers == NULL)
1993                 return -ENOMEM;
1994
1995         service->timeservers[len] = g_strdup(timeserver);
1996         service->timeservers[len + 1] = NULL;
1997
1998         update_timeservers(service);
1999
2000         return 0;
2001 }
2002
2003 int __connman_service_timeserver_remove(struct connman_service *service,
2004                                                 const char *timeserver)
2005 {
2006         char **servers;
2007         int len, i, j;
2008
2009         DBG("service %p timeserver %s", service, timeserver);
2010
2011         if (timeserver == NULL)
2012                 return -EINVAL;
2013
2014         if (service->timeservers == NULL)
2015                 return 0;
2016
2017         len = g_strv_length(service->timeservers);
2018         if (len == 1) {
2019                 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
2020                         return 0;
2021
2022                 g_strfreev(service->timeservers);
2023                 service->timeservers = NULL;
2024
2025                 return 0;
2026         }
2027
2028         servers = g_try_new0(char *, len - 1);
2029         if (servers == NULL)
2030                 return -ENOMEM;
2031
2032         for (i = 0, j = 0; i < len; i++) {
2033                 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2034                         servers[j] = g_strdup(service->timeservers[i]);
2035                         j++;
2036                 }
2037         }
2038         servers[len - 2] = NULL;
2039
2040         g_strfreev(service->timeservers);
2041         service->timeservers = servers;
2042
2043         update_timeservers(service);
2044
2045         return 0;
2046 }
2047
2048 void __connman_service_set_pac(struct connman_service *service,
2049                                         const char *pac)
2050 {
2051         g_free(service->pac);
2052         service->pac = g_strdup(pac);
2053
2054         proxy_changed(service);
2055 }
2056
2057 void __connman_service_set_identity(struct connman_service *service,
2058                                         const char *identity)
2059 {
2060         if (service->immutable)
2061                 return;
2062
2063         g_free(service->identity);
2064         service->identity = g_strdup(identity);
2065
2066         if (service->network != NULL)
2067                 connman_network_set_string(service->network,
2068                                         "WiFi.Identity",
2069                                         service->identity);
2070 }
2071
2072 void __connman_service_set_agent_identity(struct connman_service *service,
2073                                                 const char *agent_identity)
2074 {
2075         g_free(service->agent_identity);
2076         service->agent_identity = g_strdup(agent_identity);
2077
2078         if (service->network != NULL)
2079                 connman_network_set_string(service->network,
2080                                         "WiFi.AgentIdentity",
2081                                         service->agent_identity);
2082 }
2083
2084 void __connman_service_set_passphrase(struct connman_service *service,
2085                                         const char* passphrase)
2086 {
2087         if (service->immutable == TRUE)
2088                 return;
2089
2090         g_free(service->passphrase);
2091         service->passphrase = g_strdup(passphrase);
2092
2093         passphrase_changed(service);
2094
2095         if (service->network != NULL)
2096                 connman_network_set_string(service->network,
2097                                         "WiFi.Passphrase",
2098                                         service->passphrase);
2099
2100         __connman_storage_save_service(service);
2101 }
2102
2103 void __connman_service_set_agent_passphrase(struct connman_service *service,
2104                                                 const char *agent_passphrase)
2105 {
2106         g_free(service->agent_passphrase);
2107         service->agent_passphrase = g_strdup(agent_passphrase);
2108
2109         if (service->network != NULL)
2110                 connman_network_set_string(service->network,
2111                                         "WiFi.AgentPassphrase",
2112                                         service->agent_passphrase);
2113 }
2114
2115 static DBusMessage *get_properties(DBusConnection *conn,
2116                                         DBusMessage *msg, void *user_data)
2117 {
2118         struct connman_service *service = user_data;
2119         DBusMessage *reply;
2120         DBusMessageIter array, dict;
2121
2122         DBG("service %p", service);
2123
2124         reply = dbus_message_new_method_return(msg);
2125         if (reply == NULL)
2126                 return NULL;
2127
2128         dbus_message_iter_init_append(reply, &array);
2129
2130         connman_dbus_dict_open(&array, &dict);
2131         append_properties(&dict, FALSE, service);
2132         connman_dbus_dict_close(&array, &dict);
2133
2134         return reply;
2135 }
2136
2137 static int update_proxy_configuration(struct connman_service *service,
2138                                 DBusMessageIter *array)
2139 {
2140         DBusMessageIter dict;
2141         enum connman_service_proxy_method method;
2142         GString *servers_str = NULL;
2143         GString *excludes_str = NULL;
2144         const char *url = NULL;
2145
2146         method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2147
2148         dbus_message_iter_recurse(array, &dict);
2149
2150         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2151                 DBusMessageIter entry, variant;
2152                 const char *key;
2153                 int type;
2154
2155                 dbus_message_iter_recurse(&dict, &entry);
2156
2157                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2158                         goto error;
2159
2160                 dbus_message_iter_get_basic(&entry, &key);
2161                 dbus_message_iter_next(&entry);
2162
2163                 if (dbus_message_iter_get_arg_type(&entry) !=
2164                                                         DBUS_TYPE_VARIANT)
2165                         goto error;
2166
2167                 dbus_message_iter_recurse(&entry, &variant);
2168                 type = dbus_message_iter_get_arg_type(&variant);
2169
2170                 if (g_str_equal(key, "Method") == TRUE) {
2171                         const char *val;
2172
2173                         if (type != DBUS_TYPE_STRING)
2174                                 goto error;
2175
2176                         dbus_message_iter_get_basic(&variant, &val);
2177                         method = string2proxymethod(val);
2178                 } else if (g_str_equal(key, "URL") == TRUE) {
2179                         if (type != DBUS_TYPE_STRING)
2180                                 goto error;
2181
2182                         dbus_message_iter_get_basic(&variant, &url);
2183                 } else if (g_str_equal(key, "Servers") == TRUE) {
2184                         DBusMessageIter str_array;
2185
2186                         if (type != DBUS_TYPE_ARRAY)
2187                                 goto error;
2188
2189                         servers_str = g_string_new(NULL);
2190                         if (servers_str == NULL)
2191                                 goto error;
2192
2193                         dbus_message_iter_recurse(&variant, &str_array);
2194
2195                         while (dbus_message_iter_get_arg_type(&str_array) ==
2196                                                         DBUS_TYPE_STRING) {
2197                                 char *val = NULL;
2198
2199                                 dbus_message_iter_get_basic(&str_array, &val);
2200
2201                                 if (servers_str->len > 0)
2202                                         g_string_append_printf(servers_str,
2203                                                         " %s", val);
2204                                 else
2205                                         g_string_append(servers_str, val);
2206
2207                                 dbus_message_iter_next(&str_array);
2208                         }
2209                 } else if (g_str_equal(key, "Excludes") == TRUE) {
2210                         DBusMessageIter str_array;
2211
2212                         if (type != DBUS_TYPE_ARRAY)
2213                                 goto error;
2214
2215                         excludes_str = g_string_new(NULL);
2216                         if (excludes_str == NULL)
2217                                 goto error;
2218
2219                         dbus_message_iter_recurse(&variant, &str_array);
2220
2221                         while (dbus_message_iter_get_arg_type(&str_array) ==
2222                                                         DBUS_TYPE_STRING) {
2223                                 char *val = NULL;
2224
2225                                 dbus_message_iter_get_basic(&str_array, &val);
2226
2227                                 if (excludes_str->len > 0)
2228                                         g_string_append_printf(excludes_str,
2229                                                         " %s", val);
2230                                 else
2231                                         g_string_append(excludes_str, val);
2232
2233                                 dbus_message_iter_next(&str_array);
2234                         }
2235                 }
2236
2237                 dbus_message_iter_next(&dict);
2238         }
2239
2240         switch (method) {
2241         case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2242                 break;
2243         case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2244                 if (servers_str == NULL && service->proxies == NULL)
2245                         goto error;
2246
2247                 if (servers_str != NULL) {
2248                         g_strfreev(service->proxies);
2249
2250                         if (servers_str->len > 0)
2251                                 service->proxies = g_strsplit_set(
2252                                         servers_str->str, " ", 0);
2253                         else
2254                                 service->proxies = NULL;
2255                 }
2256
2257                 if (excludes_str != NULL) {
2258                         g_strfreev(service->excludes);
2259
2260                         if (excludes_str->len > 0)
2261                                 service->excludes = g_strsplit_set(
2262                                         excludes_str->str, " ", 0);
2263                         else
2264                                 service->excludes = NULL;
2265                 }
2266
2267                 if (service->proxies == NULL)
2268                         method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2269
2270                 break;
2271         case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2272                 g_free(service->pac);
2273
2274                 if (url != NULL && strlen(url) > 0)
2275                         service->pac = g_strdup(url);
2276                 else
2277                         service->pac = NULL;
2278
2279                 /* if we are connected:
2280                    - if service->pac == NULL
2281                    - if __connman_ipconfig_get_proxy_autoconfig(
2282                    service->ipconfig) == NULL
2283                    --> We should start WPAD */
2284
2285                 break;
2286         case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2287                 goto error;
2288         }
2289
2290         if (servers_str != NULL)
2291                 g_string_free(servers_str, TRUE);
2292
2293         if (excludes_str != NULL)
2294                 g_string_free(excludes_str, TRUE);
2295
2296         service->proxy_config = method;
2297
2298         return 0;
2299
2300 error:
2301         if (servers_str != NULL)
2302                 g_string_free(servers_str, TRUE);
2303
2304         if (excludes_str != NULL)
2305                 g_string_free(excludes_str, TRUE);
2306
2307         return -EINVAL;
2308 }
2309
2310 static int set_ipconfig(struct connman_service *service,
2311                         struct connman_ipconfig *ipconfig,
2312                         DBusMessageIter *array,
2313                         enum connman_service_state state,
2314                         enum connman_service_state *new_state)
2315 {
2316         enum connman_ipconfig_method old_method;
2317         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2318         enum connman_ipconfig_type type;
2319         int err;
2320
2321         old_method = __connman_ipconfig_get_method(ipconfig);
2322
2323         if (is_connecting_state(service, state) ||
2324                                         is_connected_state(service, state))
2325                 __connman_network_clear_ipconfig(service->network, ipconfig);
2326
2327         err = __connman_ipconfig_set_config(ipconfig, array);
2328         method = __connman_ipconfig_get_method(ipconfig);
2329         type = __connman_ipconfig_get_config_type(ipconfig);
2330
2331         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2332                 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2333                                 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2334                         *new_state = service->state_ipv4 =
2335                                 CONNMAN_SERVICE_STATE_CONFIGURATION;
2336                         __connman_ipconfig_enable(ipconfig);
2337                 }
2338
2339         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2340                 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2341                                 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2342                         *new_state = service->state_ipv6;
2343                         __connman_ipconfig_enable(ipconfig);
2344                 }
2345         }
2346
2347         DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2348                 type, method, state2string(*new_state));
2349
2350         return err;
2351 }
2352
2353 static DBusMessage *set_property(DBusConnection *conn,
2354                                         DBusMessage *msg, void *user_data)
2355 {
2356         struct connman_service *service = user_data;
2357         DBusMessageIter iter, value;
2358         const char *name;
2359         int type;
2360
2361         DBG("service %p", service);
2362
2363         if (dbus_message_iter_init(msg, &iter) == FALSE)
2364                 return __connman_error_invalid_arguments(msg);
2365
2366         dbus_message_iter_get_basic(&iter, &name);
2367         dbus_message_iter_next(&iter);
2368         dbus_message_iter_recurse(&iter, &value);
2369
2370         type = dbus_message_iter_get_arg_type(&value);
2371
2372         if (g_str_equal(name, "AutoConnect") == TRUE) {
2373                 connman_bool_t autoconnect;
2374
2375                 if (type != DBUS_TYPE_BOOLEAN)
2376                         return __connman_error_invalid_arguments(msg);
2377
2378                 if (service->favorite == FALSE)
2379                         return __connman_error_invalid_service(msg);
2380
2381                 dbus_message_iter_get_basic(&value, &autoconnect);
2382
2383                 if (service->autoconnect == autoconnect)
2384                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2385
2386                 service->autoconnect = autoconnect;
2387
2388                 autoconnect_changed(service);
2389
2390                 __connman_storage_save_service(service);
2391         } else if (g_str_equal(name, "Passphrase") == TRUE) {
2392                 const char *passphrase;
2393
2394                 if (type != DBUS_TYPE_STRING)
2395                         return __connman_error_invalid_arguments(msg);
2396
2397                 if (service->immutable == TRUE)
2398                         return __connman_error_not_supported(msg);
2399
2400                 dbus_message_iter_get_basic(&value, &passphrase);
2401
2402                 __connman_service_set_passphrase(service, passphrase);
2403         } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2404                 DBusMessageIter entry;
2405                 GString *str;
2406                 int index;
2407                 const char *gw;
2408
2409                 if (type != DBUS_TYPE_ARRAY)
2410                         return __connman_error_invalid_arguments(msg);
2411
2412                 str = g_string_new(NULL);
2413                 if (str == NULL)
2414                         return __connman_error_invalid_arguments(msg);
2415
2416                 index = connman_network_get_index(service->network);
2417                 gw = __connman_ipconfig_get_gateway_from_index(index);
2418
2419                 if (gw && strlen(gw))
2420                         __connman_service_nameserver_del_routes(service);
2421
2422                 dbus_message_iter_recurse(&value, &entry);
2423
2424                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2425                         const char *val;
2426                         dbus_message_iter_get_basic(&entry, &val);
2427                         dbus_message_iter_next(&entry);
2428                         if (str->len > 0)
2429                                 g_string_append_printf(str, " %s", val);
2430                         else
2431                                 g_string_append(str, val);
2432                 }
2433
2434                 g_strfreev(service->nameservers_config);
2435
2436                 if (str->len > 0) {
2437                         service->nameservers_config =
2438                                 g_strsplit_set(str->str, " ", 0);
2439                 } else {
2440                         service->nameservers_config = NULL;
2441                 }
2442
2443                 g_string_free(str, TRUE);
2444
2445                 if (gw && strlen(gw))
2446                         __connman_service_nameserver_add_routes(service, gw);
2447
2448                 update_nameservers(service);
2449                 dns_configuration_changed(service);
2450
2451                 __connman_storage_save_service(service);
2452         } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2453                 DBusMessageIter entry;
2454                 GString *str;
2455
2456                 if (type != DBUS_TYPE_ARRAY)
2457                         return __connman_error_invalid_arguments(msg);
2458
2459                 str = g_string_new(NULL);
2460                 if (str == NULL)
2461                         return __connman_error_invalid_arguments(msg);
2462
2463                 dbus_message_iter_recurse(&value, &entry);
2464
2465                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2466                         const char *val;
2467                         dbus_message_iter_get_basic(&entry, &val);
2468                         dbus_message_iter_next(&entry);
2469                         if (str->len > 0)
2470                                 g_string_append_printf(str, " %s", val);
2471                         else
2472                                 g_string_append(str, val);
2473                 }
2474
2475                 g_strfreev(service->domains);
2476
2477                 if (str->len > 0)
2478                         service->domains = g_strsplit_set(str->str, " ", 0);
2479                 else
2480                         service->domains = NULL;
2481
2482                 g_string_free(str, TRUE);
2483
2484                 update_nameservers(service);
2485                 domain_configuration_changed(service);
2486
2487                 __connman_storage_save_service(service);
2488         } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2489                 int err;
2490
2491                 if (type != DBUS_TYPE_ARRAY)
2492                         return __connman_error_invalid_arguments(msg);
2493
2494                 err = update_proxy_configuration(service, &value);
2495
2496                 if (err < 0)
2497                         return __connman_error_failed(msg, -err);
2498
2499                 proxy_configuration_changed(service);
2500
2501                 __connman_storage_save_service(service);
2502         } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2503                         g_str_equal(name, "IPv6.Configuration")) {
2504
2505                 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2506                 enum connman_service_state state =
2507                                                 CONNMAN_SERVICE_STATE_UNKNOWN;
2508                 int err = 0;
2509
2510                 DBG("%s", name);
2511
2512                 if (service->ipconfig_ipv4 == NULL &&
2513                                         service->ipconfig_ipv6 == NULL)
2514                         return __connman_error_invalid_property(msg);
2515
2516                 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2517                         ipv4 = service->ipconfig_ipv4;
2518                         err = set_ipconfig(service, ipv4, &value,
2519                                         service->state_ipv4, &state);
2520
2521                 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2522                         ipv6 = service->ipconfig_ipv6;
2523                         err = set_ipconfig(service, ipv6, &value,
2524                                         service->state_ipv6, &state);
2525                 }
2526
2527                 if (err < 0) {
2528                         if (is_connected_state(service, state) ||
2529                                         is_connecting_state(service, state))
2530                                 __connman_network_set_ipconfig(service->network,
2531                                                                 ipv4, ipv6);
2532                         return __connman_error_failed(msg, -err);
2533                 }
2534
2535                 if (ipv4)
2536                         ipv4_configuration_changed(service);
2537                 else if (ipv6)
2538                         ipv6_configuration_changed(service);
2539
2540                 if (is_connecting(service) || is_connected(service))
2541                         __connman_network_set_ipconfig(service->network,
2542                                                         ipv4, ipv6);
2543
2544                 __connman_storage_save_service(service);
2545         } else
2546                 return __connman_error_invalid_property(msg);
2547
2548         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2549 }
2550
2551 static void set_idle(struct connman_service *service)
2552 {
2553         service->state = service->state_ipv4 = service->state_ipv6 =
2554                                                 CONNMAN_SERVICE_STATE_IDLE;
2555         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2556         state_changed(service);
2557 }
2558
2559 static DBusMessage *clear_property(DBusConnection *conn,
2560                                         DBusMessage *msg, void *user_data)
2561 {
2562         struct connman_service *service = user_data;
2563         const char *name;
2564
2565         DBG("service %p", service);
2566
2567         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2568                                                         DBUS_TYPE_INVALID);
2569
2570         if (g_str_equal(name, "Error") == TRUE) {
2571                 set_idle(service);
2572
2573                 g_get_current_time(&service->modified);
2574                 __connman_storage_save_service(service);
2575         } else if (g_str_equal(name, "Passphrase") == TRUE) {
2576                 if (service->immutable == TRUE)
2577                         return __connman_error_not_supported(msg);
2578
2579                 g_free(service->passphrase);
2580                 service->passphrase = NULL;
2581
2582                 passphrase_changed(service);
2583
2584                 __connman_storage_save_service(service);
2585         } else
2586                 return __connman_error_invalid_property(msg);
2587
2588         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2589 }
2590
2591 static connman_bool_t is_ignore(struct connman_service *service)
2592 {
2593         if (service->autoconnect == FALSE)
2594                 return TRUE;
2595
2596         if (service->roaming == TRUE)
2597                 return TRUE;
2598
2599         if (service->ignore == TRUE)
2600                 return TRUE;
2601
2602         if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
2603                 return TRUE;
2604
2605         return FALSE;
2606 }
2607
2608 void __connman_service_auto_connect(void)
2609 {
2610         struct connman_service *service = NULL;
2611         GSequenceIter *iter;
2612
2613         DBG("");
2614
2615         if (__connman_session_mode() == TRUE) {
2616                 DBG("Session mode enabled: auto connect disabled");
2617                 return;
2618         }
2619
2620         iter = g_sequence_get_begin_iter(service_list);
2621
2622         while (g_sequence_iter_is_end(iter) == FALSE) {
2623                 service = g_sequence_get(iter);
2624
2625                 if (service->pending != NULL)
2626                         return;
2627
2628                 if (is_connecting(service) == TRUE)
2629                         return;
2630
2631                 if (service->favorite == FALSE)
2632                         return;
2633
2634                 if (is_connected(service) == TRUE)
2635                         return;
2636
2637                 if (is_ignore(service) == FALSE && service->state ==
2638                                                 CONNMAN_SERVICE_STATE_IDLE)
2639                         break;
2640
2641                 service = NULL;
2642
2643                 iter = g_sequence_iter_next(iter);
2644         }
2645
2646         if (service != NULL) {
2647                 service->userconnect = FALSE;
2648                 __connman_service_connect(service);
2649         }
2650 }
2651
2652 static void remove_timeout(struct connman_service *service)
2653 {
2654         if (service->timeout > 0) {
2655                 g_source_remove(service->timeout);
2656                 service->timeout = 0;
2657         }
2658 }
2659
2660 static void reply_pending(struct connman_service *service, int error)
2661 {
2662         remove_timeout(service);
2663
2664         if (service->pending != NULL) {
2665                 if (error > 0) {
2666                         DBusMessage *reply;
2667
2668                         reply = __connman_error_failed(service->pending,
2669                                                                 error);
2670                         if (reply != NULL)
2671                                 g_dbus_send_message(connection, reply);
2672                 } else {
2673                         const char *sender;
2674
2675                         sender = dbus_message_get_interface(service->pending);
2676
2677                         DBG("sender %s", sender);
2678
2679                         if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2680                                 g_dbus_send_reply(connection, service->pending,
2681                                         DBUS_TYPE_OBJECT_PATH, &service->path,
2682                                                         DBUS_TYPE_INVALID);
2683                         else
2684                                 g_dbus_send_reply(connection, service->pending,
2685                                                         DBUS_TYPE_INVALID);
2686                 }
2687
2688                 dbus_message_unref(service->pending);
2689                 service->pending = NULL;
2690         }
2691 }
2692
2693 static gboolean connect_timeout(gpointer user_data)
2694 {
2695         struct connman_service *service = user_data;
2696         connman_bool_t autoconnect = FALSE;
2697
2698         DBG("service %p", service);
2699
2700         service->timeout = 0;
2701
2702         if (service->network != NULL)
2703                 __connman_network_disconnect(service->network);
2704
2705         __connman_ipconfig_disable(service->ipconfig_ipv4);
2706         __connman_ipconfig_disable(service->ipconfig_ipv6);
2707
2708         __connman_stats_service_unregister(service);
2709
2710         if (service->pending != NULL) {
2711                 DBusMessage *reply;
2712
2713                 reply = __connman_error_operation_timeout(service->pending);
2714                 if (reply != NULL)
2715                         g_dbus_send_message(connection, reply);
2716
2717                 dbus_message_unref(service->pending);
2718                 service->pending = NULL;
2719         } else
2720                 autoconnect = TRUE;
2721
2722         __connman_service_ipconfig_indicate_state(service,
2723                                         CONNMAN_SERVICE_STATE_FAILURE,
2724                                         CONNMAN_IPCONFIG_TYPE_IPV4);
2725         __connman_service_ipconfig_indicate_state(service,
2726                                         CONNMAN_SERVICE_STATE_FAILURE,
2727                                         CONNMAN_IPCONFIG_TYPE_IPV6);
2728
2729         if (autoconnect == TRUE && service->userconnect == FALSE)
2730                 __connman_service_auto_connect();
2731
2732         return FALSE;
2733 }
2734
2735 static void set_reconnect_state(struct connman_service *service,
2736                                                 connman_bool_t reconnect)
2737 {
2738         struct connman_device *device;
2739
2740         if (service->network == NULL)
2741                 return;
2742
2743         device = connman_network_get_device(service->network);
2744         if (device == NULL)
2745                 return;
2746
2747         __connman_device_set_reconnect(device, reconnect);
2748 }
2749
2750 static connman_bool_t get_reconnect_state(struct connman_service *service)
2751 {
2752         struct connman_device *device;
2753
2754         if (service->network == NULL)
2755                 return FALSE;
2756
2757         device = connman_network_get_device(service->network);
2758         if (device == NULL)
2759                 return FALSE;
2760
2761         return __connman_device_get_reconnect(device);
2762 }
2763
2764 static void request_input_cb (struct connman_service *service,
2765                         const char *identity, const char *passphrase,
2766                         void *user_data)
2767 {
2768         DBG ("RequestInput return, %p", service);
2769
2770         if (identity == NULL && passphrase == NULL && service->wps == FALSE)
2771                 return;
2772
2773         if (identity != NULL)
2774                 __connman_service_set_agent_identity(service, identity);
2775
2776         if (passphrase != NULL) {
2777                 switch (service->security) {
2778                 case CONNMAN_SERVICE_SECURITY_WEP:
2779                 case CONNMAN_SERVICE_SECURITY_PSK:
2780                         __connman_service_set_passphrase(service, passphrase);
2781                         break;
2782                 case CONNMAN_SERVICE_SECURITY_8021X:
2783                         __connman_service_set_agent_passphrase(service,
2784                                                         passphrase);
2785                         break;
2786                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2787                 case CONNMAN_SERVICE_SECURITY_NONE:
2788                 case CONNMAN_SERVICE_SECURITY_WPA:
2789                 case CONNMAN_SERVICE_SECURITY_RSN:
2790                         DBG("service security '%s' not handled",
2791                                 security2string(service->security));
2792                         break;
2793                 }
2794         }
2795
2796         __connman_service_connect(service);
2797
2798         /* Never cache agent provided credentials */
2799         __connman_service_set_agent_identity(service, NULL);
2800         __connman_service_set_agent_passphrase(service, NULL);
2801 }
2802
2803 static DBusMessage *connect_service(DBusConnection *conn,
2804                                         DBusMessage *msg, void *user_data)
2805 {
2806         struct connman_service *service = user_data;
2807         GSequenceIter *iter;
2808         int err;
2809
2810         DBG("service %p", service);
2811
2812         if (service->pending != NULL)
2813                 return __connman_error_in_progress(msg);
2814
2815         iter = g_sequence_get_begin_iter(service_list);
2816
2817         while (g_sequence_iter_is_end(iter) == FALSE) {
2818                 struct connman_service *temp = g_sequence_get(iter);
2819
2820                 if (service->type == temp->type && is_connecting(temp) == TRUE)
2821                         return __connman_error_in_progress(msg);
2822
2823                 iter = g_sequence_iter_next(iter);
2824         }
2825
2826         service->ignore = FALSE;
2827
2828         service->userconnect = TRUE;
2829
2830         service->pending = dbus_message_ref(msg);
2831
2832         set_reconnect_state(service, FALSE);
2833
2834         err = __connman_service_connect(service);
2835         if (err < 0) {
2836                 if (service->pending == NULL)
2837                         return NULL;
2838
2839                 if (err != -EINPROGRESS) {
2840                         dbus_message_unref(service->pending);
2841                         service->pending = NULL;
2842
2843                         return __connman_error_failed(msg, -err);
2844                 }
2845
2846                 return NULL;
2847         }
2848
2849         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2850 }
2851
2852 static DBusMessage *disconnect_service(DBusConnection *conn,
2853                                         DBusMessage *msg, void *user_data)
2854 {
2855         struct connman_service *service = user_data;
2856         int err;
2857
2858         DBG("service %p", service);
2859
2860         reply_pending(service, ECONNABORTED);
2861
2862         service->ignore = TRUE;
2863
2864         set_reconnect_state(service, FALSE);
2865
2866         err = __connman_service_disconnect(service);
2867         if (err < 0) {
2868                 if (err != -EINPROGRESS)
2869                         return __connman_error_failed(msg, -err);
2870
2871                 return NULL;
2872         }
2873
2874         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2875 }
2876
2877 static DBusMessage *remove_service(DBusConnection *conn,
2878                                         DBusMessage *msg, void *user_data)
2879 {
2880         struct connman_service *service = user_data;
2881
2882         DBG("service %p", service);
2883
2884         if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
2885                 return __connman_error_not_supported(msg);
2886
2887         if (service->immutable == TRUE)
2888                 return __connman_error_not_supported(msg);
2889
2890         if (service->favorite == FALSE && service->state !=
2891                                                 CONNMAN_SERVICE_STATE_FAILURE)
2892                 return __connman_error_not_supported(msg);
2893
2894         if (service->network != NULL) {
2895                 set_reconnect_state(service, FALSE);
2896
2897                 __connman_network_disconnect(service->network);
2898         }
2899
2900         g_free(service->passphrase);
2901         service->passphrase = NULL;
2902
2903         passphrase_changed(service);
2904
2905         set_idle(service);
2906
2907         __connman_service_set_favorite(service, FALSE);
2908         __connman_storage_save_service(service);
2909
2910         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2911 }
2912
2913 static DBusMessage *move_service(DBusConnection *conn,
2914                                         DBusMessage *msg, void *user_data,
2915                                                                 gboolean before)
2916 {
2917         struct connman_service *service = user_data;
2918         struct connman_service *target;
2919         const char *path;
2920         GSequenceIter *src, *dst;
2921
2922         DBG("service %p", service);
2923
2924         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2925                                                         DBUS_TYPE_INVALID);
2926
2927         if (service->favorite == FALSE)
2928                 return __connman_error_not_supported(msg);
2929
2930         target = find_service(path);
2931         if (target == NULL || target->favorite == FALSE || target == service)
2932                 return __connman_error_invalid_service(msg);
2933
2934         DBG("target %s", target->identifier);
2935
2936         if (target->state_ipv4 != service->state_ipv4 &&
2937                                 target->state_ipv6 != service->state_ipv6)
2938                 return __connman_error_invalid_service(msg);
2939
2940         g_get_current_time(&service->modified);
2941         __connman_storage_save_service(service);
2942
2943         src = g_hash_table_lookup(service_hash, service->identifier);
2944         dst = g_hash_table_lookup(service_hash, target->identifier);
2945
2946         before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
2947
2948         __connman_profile_changed(FALSE);
2949
2950         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2951 }
2952
2953 static DBusMessage *move_before(DBusConnection *conn,
2954                                         DBusMessage *msg, void *user_data)
2955 {
2956         return move_service(conn, msg, user_data, TRUE);
2957 }
2958
2959 static DBusMessage *move_after(DBusConnection *conn,
2960                                         DBusMessage *msg, void *user_data)
2961 {
2962         return move_service(conn, msg, user_data, FALSE);
2963 }
2964
2965 static DBusMessage *reset_counters(DBusConnection *conn,
2966                                         DBusMessage *msg, void *user_data)
2967 {
2968         struct connman_service *service = user_data;
2969
2970         reset_stats(service);
2971
2972         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2973 }
2974
2975 static GDBusMethodTable service_methods[] = {
2976         { "GetProperties", "",   "a{sv}", get_properties     },
2977         { "SetProperty",   "sv", "",      set_property       },
2978         { "ClearProperty", "s",  "",      clear_property     },
2979         { "Connect",       "",   "",      connect_service,
2980                                                 G_DBUS_METHOD_FLAG_ASYNC },
2981         { "Disconnect",    "",   "",      disconnect_service },
2982         { "Remove",        "",   "",      remove_service     },
2983         { "MoveBefore",    "o",  "",      move_before        },
2984         { "MoveAfter",     "o",  "",      move_after         },
2985         { "ResetCounters", "",   "",      reset_counters     },
2986         { },
2987 };
2988
2989 static GDBusSignalTable service_signals[] = {
2990         { "PropertyChanged", "sv" },
2991         { },
2992 };
2993
2994 static void service_free(gpointer user_data)
2995 {
2996         struct connman_service *service = user_data;
2997         char *path = service->path;
2998
2999         DBG("service %p", service);
3000
3001         reply_pending(service, ENOENT);
3002
3003         g_hash_table_remove(service_hash, service->identifier);
3004
3005         __connman_notifier_service_remove(service);
3006
3007         stats_stop(service);
3008         __connman_storage_save_service(service);
3009
3010         service->path = NULL;
3011
3012         if (path != NULL) {
3013                 __connman_profile_changed(FALSE);
3014
3015                 g_dbus_unregister_interface(connection, path,
3016                                                 CONNMAN_SERVICE_INTERFACE);
3017                 g_free(path);
3018         }
3019
3020         g_hash_table_destroy(service->counter_table);
3021
3022         if (service->network != NULL) {
3023                 if (service->network_created == TRUE)
3024                         connman_network_unref(service->network);
3025         }
3026
3027         if (service->provider != NULL)
3028                 connman_provider_unref(service->provider);
3029
3030         if (service->ipconfig_ipv4 != NULL) {
3031                 connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3032                 connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3033                 connman_ipconfig_unref(service->ipconfig_ipv4);
3034                 service->ipconfig_ipv4 = NULL;
3035         }
3036
3037         if (service->ipconfig_ipv6 != NULL) {
3038                 connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3039                 connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3040                 connman_ipconfig_unref(service->ipconfig_ipv6);
3041                 service->ipconfig_ipv6 = NULL;
3042         }
3043
3044         if (service->location != NULL)
3045                 connman_location_unref(service->location);
3046
3047         g_strfreev(service->nameservers);
3048         g_strfreev(service->nameservers_config);
3049         g_strfreev(service->domains);
3050         g_strfreev(service->proxies);
3051         g_strfreev(service->excludes);
3052
3053         g_free(service->domainname);
3054         g_free(service->pac);
3055         g_free(service->profile);
3056         g_free(service->name);
3057         g_free(service->passphrase);
3058         g_free(service->agent_passphrase);
3059         g_free(service->identifier);
3060         g_free(service->eap);
3061         g_free(service->identity);
3062         g_free(service->agent_identity);
3063         g_free(service->ca_cert_file);
3064         g_free(service->client_cert_file);
3065         g_free(service->private_key_file);
3066         g_free(service->private_key_passphrase);
3067         g_free(service->phase2);
3068
3069         if (service->stats.timer != NULL)
3070                 g_timer_destroy(service->stats.timer);
3071         if (service->stats_roaming.timer != NULL)
3072                 g_timer_destroy(service->stats_roaming.timer);
3073
3074         g_free(service);
3075 }
3076
3077 /**
3078  * __connman_service_put:
3079  * @service: service structure
3080  *
3081  * Release service if no longer needed
3082  */
3083 void __connman_service_put(struct connman_service *service)
3084 {
3085         DBG("service %p", service);
3086
3087         if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
3088                 GSequenceIter *iter;
3089
3090                 iter = g_hash_table_lookup(service_hash, service->identifier);
3091                 if (iter != NULL) {
3092                         reply_pending(service, ECONNABORTED);
3093
3094                         __connman_service_disconnect(service);
3095
3096                         g_sequence_remove(iter);
3097                 } else
3098                         service_free(service);
3099         }
3100 }
3101
3102 static void stats_init(struct connman_service *service)
3103 {
3104         /* home */
3105         service->stats.valid = FALSE;
3106         service->stats.enabled = FALSE;
3107         service->stats.timer = g_timer_new();
3108
3109         /* roaming */
3110         service->stats_roaming.valid = FALSE;
3111         service->stats_roaming.enabled = FALSE;
3112         service->stats_roaming.timer = g_timer_new();
3113 }
3114
3115 static void service_initialize(struct connman_service *service)
3116 {
3117         DBG("service %p", service);
3118
3119         service->refcount = 1;
3120         service->session_usage_count = 0;
3121
3122         service->network_created = FALSE;
3123
3124         service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
3125         service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3126
3127         service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3128         service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3129         service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3130
3131         service->favorite  = FALSE;
3132         service->immutable = FALSE;
3133         service->hidden = FALSE;
3134
3135         service->ignore = FALSE;
3136
3137         service->userconnect = FALSE;
3138
3139         service->order = 0;
3140
3141         stats_init(service);
3142
3143         service->provider = NULL;
3144
3145         service->wps = FALSE;
3146 }
3147
3148 /**
3149  * connman_service_create:
3150  *
3151  * Allocate a new service.
3152  *
3153  * Returns: a newly-allocated #connman_service structure
3154  */
3155 struct connman_service *connman_service_create(void)
3156 {
3157         GSList *list;
3158         struct connman_stats_counter *counters;
3159         const char *counter;
3160
3161         struct connman_service *service;
3162
3163         service = g_try_new0(struct connman_service, 1);
3164         if (service == NULL)
3165                 return NULL;
3166
3167         DBG("service %p", service);
3168
3169         service->counter_table = g_hash_table_new_full(g_str_hash,
3170                                                 g_str_equal, NULL, g_free);
3171
3172         for (list = counter_list; list; list = list->next) {
3173                 counter = list->data;
3174
3175                 counters = g_try_new0(struct connman_stats_counter, 1);
3176                 if (counters == NULL) {
3177                         g_hash_table_destroy(service->counter_table);
3178                         g_free(service);
3179                         return NULL;
3180                 }
3181
3182                 counters->append_all = TRUE;
3183
3184                 g_hash_table_replace(service->counter_table, (gpointer)counter,
3185                                 counters);
3186         }
3187
3188         service_initialize(service);
3189
3190         service->location = __connman_location_create(service);
3191
3192         return service;
3193 }
3194
3195 struct connman_location *__connman_service_get_location(struct connman_service *service)
3196 {
3197         return service->location;
3198 }
3199
3200 /**
3201  * connman_service_ref:
3202  * @service: service structure
3203  *
3204  * Increase reference counter of service
3205  */
3206 struct connman_service *connman_service_ref(struct connman_service *service)
3207 {
3208         DBG("%p", service);
3209
3210         g_atomic_int_inc(&service->refcount);
3211
3212         return service;
3213 }
3214
3215 /**
3216  * connman_service_unref:
3217  * @service: service structure
3218  *
3219  * Decrease reference counter of service
3220  */
3221 void connman_service_unref(struct connman_service *service)
3222 {
3223         __connman_service_put(service);
3224 }
3225
3226 static gint service_compare(gconstpointer a, gconstpointer b,
3227                                                         gpointer user_data)
3228 {
3229         struct connman_service *service_a = (void *) a;
3230         struct connman_service *service_b = (void *) b;
3231         enum connman_service_state state_a, state_b;
3232
3233         state_a = service_a->state;
3234         state_b = service_b->state;
3235
3236         if (state_a != state_b) {
3237                 if (is_connected(service_a) == TRUE)
3238                         return -1;
3239                 if (is_connected(service_b) == TRUE)
3240                         return 1;
3241
3242                 if (is_connecting(service_a) == TRUE)
3243                         return -1;
3244                 if (is_connecting(service_b) == TRUE)
3245                         return 1;
3246         }
3247
3248         if (service_a->order > service_b->order)
3249                 return -1;
3250
3251         if (service_a->order < service_b->order)
3252                 return 1;
3253
3254         if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3255                 return -1;
3256
3257         if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3258                 return 1;
3259
3260         if (service_a->type != service_b->type) {
3261                 switch (service_a->type) {
3262                 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3263                 case CONNMAN_SERVICE_TYPE_SYSTEM:
3264                 case CONNMAN_SERVICE_TYPE_ETHERNET:
3265                 case CONNMAN_SERVICE_TYPE_GPS:
3266                 case CONNMAN_SERVICE_TYPE_VPN:
3267                 case CONNMAN_SERVICE_TYPE_GADGET:
3268                         break;
3269                 case CONNMAN_SERVICE_TYPE_WIFI:
3270                         return 1;
3271                 case CONNMAN_SERVICE_TYPE_WIMAX:
3272                 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3273                 case CONNMAN_SERVICE_TYPE_CELLULAR:
3274                         return -1;
3275                 }
3276         }
3277
3278         return (gint) service_b->strength - (gint) service_a->strength;
3279 }
3280
3281 /**
3282  * connman_service_get_type:
3283  * @service: service structure
3284  *
3285  * Get the type of service
3286  */
3287 enum connman_service_type connman_service_get_type(struct connman_service *service)
3288 {
3289         if (service == NULL)
3290                 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3291
3292         return service->type;
3293 }
3294
3295 /**
3296  * connman_service_get_interface:
3297  * @service: service structure
3298  *
3299  * Get network interface of service
3300  */
3301 char *connman_service_get_interface(struct connman_service *service)
3302 {
3303         int index;
3304
3305         if (service == NULL)
3306                 return NULL;
3307
3308         if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3309                 if (service->ipconfig_ipv4)
3310                         index = connman_ipconfig_get_index(
3311                                                 service->ipconfig_ipv4);
3312                 else if (service->ipconfig_ipv6)
3313                         index = connman_ipconfig_get_index(
3314                                                 service->ipconfig_ipv6);
3315                 else
3316                         return NULL;
3317
3318                 return connman_inet_ifname(index);
3319         }
3320
3321         if (service->network == NULL)
3322                 return NULL;
3323
3324         index = connman_network_get_index(service->network);
3325
3326         return connman_inet_ifname(index);
3327 }
3328
3329 /**
3330  * connman_service_get_network:
3331  * @service: service structure
3332  *
3333  * Get the service network
3334  */
3335 struct connman_network *
3336 __connman_service_get_network(struct connman_service *service)
3337 {
3338         if (service == NULL)
3339                 return NULL;
3340
3341         return service->network;
3342 }
3343
3344 struct connman_ipconfig *
3345 __connman_service_get_ip4config(struct connman_service *service)
3346 {
3347         if (service == NULL)
3348                 return NULL;
3349
3350         return service->ipconfig_ipv4;
3351 }
3352
3353 struct connman_ipconfig *
3354 __connman_service_get_ip6config(struct connman_service *service)
3355 {
3356         if (service == NULL)
3357                 return NULL;
3358
3359         return service->ipconfig_ipv6;
3360 }
3361
3362 struct connman_ipconfig *
3363 __connman_service_get_ipconfig(struct connman_service *service, int family)
3364 {
3365         if (family == AF_INET)
3366                 return __connman_service_get_ip4config(service);
3367         else if (family == AF_INET6)
3368                 return __connman_service_get_ip6config(service);
3369         else
3370                 return NULL;
3371
3372 }
3373
3374 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3375 {
3376         if (service == NULL)
3377                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3378
3379         return service->security;
3380 }
3381
3382 const char *__connman_service_get_phase2(struct connman_service *service)
3383 {
3384         if (service == NULL)
3385                 return NULL;
3386
3387         return service->phase2;
3388 }
3389
3390 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3391 {
3392         if (service == NULL)
3393                 return FALSE;
3394
3395         return service->wps;
3396 }
3397
3398 /**
3399  * __connman_service_set_favorite:
3400  * @service: service structure
3401  * @favorite: favorite value
3402  *
3403  * Change the favorite setting of service
3404  */
3405 int __connman_service_set_favorite(struct connman_service *service,
3406                                                 connman_bool_t favorite)
3407 {
3408         GSequenceIter *iter;
3409
3410         iter = g_hash_table_lookup(service_hash, service->identifier);
3411         if (iter == NULL)
3412                 return -ENOENT;
3413
3414         if (service->favorite == favorite)
3415                 return -EALREADY;
3416
3417         service->favorite = favorite;
3418
3419         favorite_changed(service);
3420
3421         g_sequence_sort_changed(iter, service_compare, NULL);
3422
3423         __connman_profile_changed(FALSE);
3424
3425         return 0;
3426 }
3427
3428 int __connman_service_set_immutable(struct connman_service *service,
3429                                                 connman_bool_t immutable)
3430 {
3431         service->immutable = immutable;
3432
3433         immutable_changed(service);
3434
3435         return 0;
3436 }
3437
3438 void __connman_service_set_string(struct connman_service *service,
3439                                   const char *key, const char *value)
3440 {
3441         if (g_str_equal(key, "EAP") == TRUE) {
3442                 g_free(service->eap);
3443                 service->eap = g_strdup(value);
3444         } else if (g_str_equal(key, "Identity") == TRUE) {
3445                 g_free(service->identity);
3446                 service->identity = g_strdup(value);
3447         } else if (g_str_equal(key, "CACertFile") == TRUE) {
3448                 g_free(service->ca_cert_file);
3449                 service->ca_cert_file = g_strdup(value);
3450         } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3451                 g_free(service->client_cert_file);
3452                 service->client_cert_file = g_strdup(value);
3453         } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3454                 g_free(service->private_key_file);
3455                 service->private_key_file = g_strdup(value);
3456         } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3457                 g_free(service->private_key_passphrase);
3458                 service->private_key_passphrase = g_strdup(value);
3459         } else if (g_str_equal(key, "Phase2") == TRUE) {
3460                 g_free(service->phase2);
3461                 service->phase2 = g_strdup(value);
3462         } else if (g_str_equal(key, "Passphrase") == TRUE) {
3463                 g_free(service->passphrase);
3464                 service->passphrase = g_strdup(value);
3465         }
3466 }
3467
3468 static void service_complete(struct connman_service *service)
3469 {
3470         reply_pending(service, EIO);
3471
3472         if (service->userconnect == FALSE)
3473                 __connman_service_auto_connect();
3474
3475         g_get_current_time(&service->modified);
3476         __connman_storage_save_service(service);
3477 }
3478
3479 static void report_error_cb(struct connman_service *service,
3480                         gboolean retry, void *user_data)
3481 {
3482         if (retry == TRUE)
3483                 __connman_service_connect(service);
3484         else {
3485                 service_complete(service);
3486                 __connman_profile_changed(FALSE);
3487                 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3488         }
3489 }
3490
3491 static int __connman_service_indicate_state(struct connman_service *service)
3492 {
3493         enum connman_service_state old_state, new_state;
3494         GSequenceIter *iter;
3495
3496         if (service == NULL)
3497                 return -EINVAL;
3498
3499         old_state = service->state;
3500         new_state = combine_state(service->state_ipv4, service->state_ipv6);
3501
3502         if (old_state == new_state)
3503                 return -EALREADY;
3504
3505         DBG("service %p old %s - new %s/%s => %s",
3506                                         service,
3507                                         state2string(old_state),
3508                                         state2string(service->state_ipv4),
3509                                         state2string(service->state_ipv6),
3510                                         state2string(new_state));
3511
3512         service->state = new_state;
3513         state_changed(service);
3514
3515         if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
3516                         old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3517                 reply_pending(service, ECONNABORTED);
3518
3519                 __connman_service_disconnect(service);
3520         }
3521
3522         if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3523                 if (__connman_stats_service_register(service) == 0) {
3524                         __connman_stats_get(service, FALSE,
3525                                                 &service->stats.data);
3526                         __connman_stats_get(service, TRUE,
3527                                                 &service->stats_roaming.data);
3528                 }
3529         }
3530
3531         if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
3532                 if (service->login_required == TRUE) {
3533                         service->login_required = FALSE;
3534                         login_changed(service);
3535                 }
3536
3537                 connman_timeserver_sync();
3538
3539                 default_changed();
3540         }
3541
3542         if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
3543                 connman_bool_t reconnect;
3544
3545                 reconnect = get_reconnect_state(service);
3546                 if (reconnect == TRUE)
3547                         __connman_service_auto_connect();
3548
3549                 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3550         }
3551
3552         if (new_state == CONNMAN_SERVICE_STATE_READY) {
3553                 enum connman_ipconfig_method method;
3554
3555                 set_reconnect_state(service, TRUE);
3556
3557                 __connman_service_set_favorite(service, TRUE);
3558
3559                 reply_pending(service, 0);
3560
3561                 service->userconnect = FALSE;
3562
3563                 g_get_current_time(&service->modified);
3564                 __connman_storage_save_service(service);
3565
3566                 update_nameservers(service);
3567                 dns_changed(service);
3568                 domain_changed(service);
3569
3570                 __connman_notifier_connect(service->type);
3571
3572                 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3573                         connman_network_get_bool(service->network,
3574                                                 "WiFi.UseWPS") == TRUE) {
3575                         const char *pass;
3576
3577                         pass = connman_network_get_string(service->network,
3578                                                         "WiFi.Passphrase");
3579
3580                         __connman_service_set_passphrase(service, pass);
3581
3582                         connman_network_set_bool(service->network,
3583                                                         "WiFi.UseWPS", FALSE);
3584                 }
3585
3586                 default_changed();
3587
3588                 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3589                 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
3590                         __connman_ipconfig_disable_ipv6(
3591                                                 service->ipconfig_ipv6);
3592
3593         } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
3594                 struct connman_service *def_service = get_default();
3595
3596                 if (__connman_notifier_count_connected() == 0 &&
3597                         def_service != NULL &&
3598                                 def_service->provider != NULL)
3599                         __connman_provider_disconnect(def_service->provider);
3600
3601                 __connman_location_finish(service);
3602
3603                 default_changed();
3604
3605                 __connman_wpad_stop(service);
3606
3607                 update_nameservers(service);
3608                 dns_changed(service);
3609                 domain_changed(service);
3610
3611                 __connman_notifier_disconnect(service->type);
3612         }
3613
3614         if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
3615                 if (service->userconnect == TRUE &&
3616                         __connman_agent_report_error(service,
3617                                         error2string(service->error),
3618                                         report_error_cb, NULL) == -EIO)
3619                         return 0;
3620                 service_complete(service);
3621
3622                 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3623         } else
3624                 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3625
3626         iter = g_hash_table_lookup(service_hash, service->identifier);
3627         if (iter != NULL)
3628                 g_sequence_sort_changed(iter, service_compare, NULL);
3629
3630         __connman_profile_changed(FALSE);
3631
3632         return 0;
3633 }
3634
3635 int __connman_service_indicate_error(struct connman_service *service,
3636                                         enum connman_service_error error)
3637 {
3638         DBG("service %p error %d", service, error);
3639
3640         if (service == NULL)
3641                 return -EINVAL;
3642
3643         service->error = error;
3644
3645         if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
3646                 __connman_service_set_passphrase(service, NULL);
3647
3648         __connman_service_ipconfig_indicate_state(service,
3649                                                 CONNMAN_SERVICE_STATE_FAILURE,
3650                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
3651         __connman_service_ipconfig_indicate_state(service,
3652                                                 CONNMAN_SERVICE_STATE_FAILURE,
3653                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
3654         return 0;
3655 }
3656
3657 int __connman_service_clear_error(struct connman_service *service)
3658 {
3659         DBG("service %p", service);
3660
3661         if (service == NULL)
3662                 return -EINVAL;
3663
3664         if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
3665                 return -EINVAL;
3666
3667         service->state_ipv4 = service->state_ipv6 =
3668                                                 CONNMAN_SERVICE_STATE_UNKNOWN;
3669         service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
3670
3671         if (service->favorite == TRUE)
3672                 set_reconnect_state(service, TRUE);
3673
3674         __connman_service_ipconfig_indicate_state(service,
3675                                         CONNMAN_SERVICE_STATE_IDLE,
3676                                         CONNMAN_IPCONFIG_TYPE_IPV6);
3677
3678         /*
3679          * Toggling the IPv6 state to IDLE could trigger the auto connect
3680          * machinery and consequently the IPv4 state.
3681          */
3682         if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
3683                         service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
3684                 return 0;
3685
3686         return __connman_service_ipconfig_indicate_state(service,
3687                                                 CONNMAN_SERVICE_STATE_IDLE,
3688                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
3689 }
3690
3691 int __connman_service_indicate_default(struct connman_service *service)
3692 {
3693         DBG("service %p", service);
3694
3695         default_changed();
3696
3697         __connman_location_detect(service);
3698
3699         return 0;
3700 }
3701
3702 static void check_proxy_setup(struct connman_service *service)
3703 {
3704         /*
3705          * We start WPAD if we haven't got a PAC URL from DHCP and
3706          * if our proxy manual configuration is either empty or set
3707          * to AUTO with an empty URL.
3708          */
3709
3710         if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
3711                 return;
3712
3713         if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
3714                 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
3715                         service->pac != NULL))
3716                 return;
3717
3718         if (__connman_wpad_start(service) < 0) {
3719                 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
3720                 __connman_notifier_proxy_changed(service);
3721         }
3722 }
3723
3724 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
3725                                         enum connman_service_state new_state,
3726                                         enum connman_ipconfig_type type)
3727 {
3728         struct connman_ipconfig *ipconfig = NULL;
3729         enum connman_service_state old_state;
3730
3731         if (service == NULL)
3732                 return -EINVAL;
3733
3734         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
3735                 old_state = service->state_ipv4;
3736                 ipconfig = service->ipconfig_ipv4;
3737         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
3738                 old_state = service->state_ipv6;
3739                 ipconfig = service->ipconfig_ipv6;
3740         }
3741
3742         if (ipconfig == NULL)
3743                 return -EINVAL;
3744
3745         /* Any change? */
3746         if (old_state == new_state)
3747                 return -EALREADY;
3748
3749         DBG("service %p (%s) state %d (%s) type %d (%s)",
3750                 service, service ? service->identifier : NULL,
3751                 new_state, state2string(new_state),
3752                 type, __connman_ipconfig_type2string(type));
3753
3754         switch (new_state) {
3755         case CONNMAN_SERVICE_STATE_UNKNOWN:
3756         case CONNMAN_SERVICE_STATE_IDLE:
3757                 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
3758                         return -EINVAL;
3759                 break;
3760         case CONNMAN_SERVICE_STATE_ASSOCIATION:
3761                 break;
3762         case CONNMAN_SERVICE_STATE_CONFIGURATION:
3763                 __connman_ipconfig_enable(ipconfig);
3764                 break;
3765         case CONNMAN_SERVICE_STATE_READY:
3766                 update_nameservers(service);
3767
3768                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3769                         check_proxy_setup(service);
3770                 break;
3771         case CONNMAN_SERVICE_STATE_ONLINE:
3772                 break;
3773         case CONNMAN_SERVICE_STATE_DISCONNECT:
3774                 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
3775                         return -EINVAL;
3776                 break;
3777         case CONNMAN_SERVICE_STATE_FAILURE:
3778                 break;
3779         }
3780
3781         /* We keep that state */
3782         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
3783                 service->state_ipv4 = new_state;
3784         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
3785                 service->state_ipv6 = new_state;
3786
3787         return __connman_service_indicate_state(service);
3788 }
3789
3790 int __connman_service_request_login(struct connman_service *service)
3791 {
3792         DBG("service %p", service);
3793
3794         if (service == NULL)
3795                 return -EINVAL;
3796
3797         service->login_required = TRUE;
3798         login_changed(service);
3799
3800         return 0;
3801 }
3802
3803 static connman_bool_t prepare_network(struct connman_service *service)
3804 {
3805         enum connman_network_type type;
3806         unsigned int ssid_len;
3807
3808         type = connman_network_get_type(service->network);
3809
3810         switch (type) {
3811         case CONNMAN_NETWORK_TYPE_UNKNOWN:
3812         case CONNMAN_NETWORK_TYPE_VENDOR:
3813                 return FALSE;
3814         case CONNMAN_NETWORK_TYPE_WIFI:
3815                 if (connman_network_get_blob(service->network, "WiFi.SSID",
3816                                                         &ssid_len) == NULL)
3817                         return FALSE;
3818
3819                 if (service->passphrase != NULL)
3820                         connman_network_set_string(service->network,
3821                                 "WiFi.Passphrase", service->passphrase);
3822                 break;
3823         case CONNMAN_NETWORK_TYPE_ETHERNET:
3824         case CONNMAN_NETWORK_TYPE_WIMAX:
3825         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
3826         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
3827         case CONNMAN_NETWORK_TYPE_CELLULAR:
3828                 break;
3829         }
3830
3831         return TRUE;
3832 }
3833
3834 static void prepare_8021x(struct connman_service *service)
3835 {
3836         if (service->eap != NULL)
3837                 connman_network_set_string(service->network, "WiFi.EAP",
3838                                                                 service->eap);
3839
3840         if (service->identity != NULL)
3841                 connman_network_set_string(service->network, "WiFi.Identity",
3842                                                         service->identity);
3843
3844         if (service->ca_cert_file != NULL)
3845                 connman_network_set_string(service->network, "WiFi.CACertFile",
3846                                                         service->ca_cert_file);
3847
3848         if (service->client_cert_file != NULL)
3849                 connman_network_set_string(service->network,
3850                                                 "WiFi.ClientCertFile",
3851                                                 service->client_cert_file);
3852
3853         if (service->private_key_file != NULL)
3854                 connman_network_set_string(service->network,
3855                                                 "WiFi.PrivateKeyFile",
3856                                                 service->private_key_file);
3857
3858         if (service->private_key_passphrase != NULL)
3859                 connman_network_set_string(service->network,
3860                                         "WiFi.PrivateKeyPassphrase",
3861                                         service->private_key_passphrase);
3862
3863         if (service->phase2 != NULL)
3864                 connman_network_set_string(service->network, "WiFi.Phase2",
3865                                                         service->phase2);
3866 }
3867
3868 static int service_connect(struct connman_service *service)
3869 {
3870         int err;
3871
3872         switch (service->type) {
3873         case CONNMAN_SERVICE_TYPE_UNKNOWN:
3874         case CONNMAN_SERVICE_TYPE_SYSTEM:
3875         case CONNMAN_SERVICE_TYPE_GPS:
3876         case CONNMAN_SERVICE_TYPE_GADGET:
3877                 return -EINVAL;
3878         case CONNMAN_SERVICE_TYPE_ETHERNET:
3879         case CONNMAN_SERVICE_TYPE_WIMAX:
3880         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3881         case CONNMAN_SERVICE_TYPE_CELLULAR:
3882         case CONNMAN_SERVICE_TYPE_VPN:
3883                 break;
3884         case CONNMAN_SERVICE_TYPE_WIFI:
3885                 switch (service->security) {
3886                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3887                 case CONNMAN_SERVICE_SECURITY_NONE:
3888                         break;
3889                 case CONNMAN_SERVICE_SECURITY_WEP:
3890                 case CONNMAN_SERVICE_SECURITY_PSK:
3891                 case CONNMAN_SERVICE_SECURITY_WPA:
3892                 case CONNMAN_SERVICE_SECURITY_RSN:
3893                         if (service->passphrase == NULL) {
3894                                 if (service->network == NULL)
3895                                         return -EOPNOTSUPP;
3896
3897                                 if (service->wps == FALSE ||
3898                                         connman_network_get_bool(
3899                                                         service->network,
3900                                                         "WiFi.UseWPS") == FALSE)
3901                                         return -ENOKEY;
3902                         }
3903                         break;
3904                 case CONNMAN_SERVICE_SECURITY_8021X:
3905                         if (service->eap == NULL)
3906                                 return -EINVAL;
3907
3908                         /*
3909                          * never request credentials if using EAP-TLS
3910                          * (EAP-TLS networks need to be fully provisioned)
3911                          */
3912                         if (g_str_equal(service->eap, "tls") == TRUE)
3913                                 break;
3914
3915                         /*
3916                          * Return -ENOKEY if either identity or passphrase is
3917                          * missing. Agent provided credentials can be used as
3918                          * fallback if needed.
3919                          */
3920                         if ((service->identity == NULL &&
3921                                         service->agent_identity == NULL) ||
3922                                         (service->passphrase == NULL &&
3923                                         service->agent_passphrase == NULL))
3924                                 return -ENOKEY;
3925
3926                         break;
3927                 }
3928                 break;
3929         }
3930
3931         if (service->network != NULL) {
3932                 if (prepare_network(service) == FALSE)
3933                         return -EINVAL;
3934
3935                 switch (service->security) {
3936                 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3937                 case CONNMAN_SERVICE_SECURITY_NONE:
3938                 case CONNMAN_SERVICE_SECURITY_WEP:
3939                 case CONNMAN_SERVICE_SECURITY_PSK:
3940                 case CONNMAN_SERVICE_SECURITY_WPA:
3941                 case CONNMAN_SERVICE_SECURITY_RSN:
3942                         break;
3943                 case CONNMAN_SERVICE_SECURITY_8021X:
3944                         prepare_8021x(service);
3945                         break;
3946                 }
3947
3948                 if (__connman_stats_service_register(service) == 0) {
3949                         __connman_stats_get(service, FALSE,
3950                                                 &service->stats.data);
3951                         __connman_stats_get(service, TRUE,
3952                                                 &service->stats_roaming.data);
3953                 }
3954
3955                 if (service->ipconfig_ipv4)
3956                         __connman_ipconfig_enable(service->ipconfig_ipv4);
3957                 if (service->ipconfig_ipv6)
3958                         __connman_ipconfig_enable(service->ipconfig_ipv6);
3959
3960                 err = __connman_network_connect(service->network);
3961         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
3962                                         service->provider != NULL)
3963                 err = __connman_provider_connect(service->provider);
3964         else
3965                 return -EOPNOTSUPP;
3966
3967         if (err < 0) {
3968                 if (err != -EINPROGRESS) {
3969                         __connman_ipconfig_disable(service->ipconfig_ipv4);
3970                         __connman_ipconfig_disable(service->ipconfig_ipv6);
3971                         __connman_stats_service_unregister(service);
3972                 }
3973         }
3974
3975         return err;
3976 }
3977
3978
3979 int __connman_service_connect(struct connman_service *service)
3980 {
3981         int err;
3982
3983         DBG("service %p state %s", service, state2string(service->state));
3984
3985         if (is_connected(service) == TRUE)
3986                 return -EISCONN;
3987
3988         if (is_connecting(service) == TRUE)
3989                 return -EALREADY;
3990
3991         switch (service->type) {
3992         case CONNMAN_SERVICE_TYPE_UNKNOWN:
3993         case CONNMAN_SERVICE_TYPE_SYSTEM:
3994         case CONNMAN_SERVICE_TYPE_GPS:
3995         case CONNMAN_SERVICE_TYPE_GADGET:
3996                 return -EINVAL;
3997         default:
3998                 err = service_connect(service);
3999         }
4000
4001         if (err >= 0)
4002                 return 0;
4003
4004         if (err == -EINPROGRESS) {
4005                 if (service->timeout == 0)
4006                         service->timeout = g_timeout_add_seconds(
4007                                 CONNECT_TIMEOUT, connect_timeout, service);
4008
4009                 return -EINPROGRESS;
4010         }
4011
4012         __connman_service_ipconfig_indicate_state(service,
4013                                         CONNMAN_SERVICE_STATE_FAILURE,
4014                                         CONNMAN_IPCONFIG_TYPE_IPV4);
4015         __connman_service_ipconfig_indicate_state(service,
4016                                         CONNMAN_SERVICE_STATE_FAILURE,
4017                                         CONNMAN_IPCONFIG_TYPE_IPV6);
4018
4019         __connman_network_disconnect(service->network);
4020
4021         if (service->userconnect == TRUE) {
4022                 if (err == -ENOKEY) {
4023                         if (__connman_agent_request_input(service,
4024                                                         request_input_cb,
4025                                                         NULL) == -EIO)
4026                                 return -EINPROGRESS;
4027                 }
4028                 reply_pending(service, err);
4029         }
4030
4031         return err;
4032 }
4033
4034 int __connman_service_disconnect(struct connman_service *service)
4035 {
4036         int err;
4037
4038         DBG("service %p", service);
4039
4040         if (service->network != NULL) {
4041                 err = __connman_network_disconnect(service->network);
4042         } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4043                                         service->provider != NULL)
4044                 err = __connman_provider_disconnect(service->provider);
4045         else
4046                 return -EOPNOTSUPP;
4047
4048         if (err < 0 && err != -EINPROGRESS)
4049                 return err;
4050
4051         __connman_6to4_remove(service->ipconfig_ipv4);
4052
4053         if (service->ipconfig_ipv4)
4054                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
4055                                                         NULL);
4056         else
4057                 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
4058                                                         NULL);
4059
4060         __connman_ipconfig_address_remove(service->ipconfig_ipv4);
4061         __connman_ipconfig_address_remove(service->ipconfig_ipv6);
4062
4063         __connman_ipconfig_disable(service->ipconfig_ipv4);
4064         __connman_ipconfig_disable(service->ipconfig_ipv6);
4065
4066         __connman_stats_service_unregister(service);
4067
4068         return err;
4069 }
4070
4071 int __connman_service_disconnect_all(void)
4072 {
4073         GSequenceIter *iter;
4074
4075         DBG("");
4076
4077         iter = g_sequence_get_begin_iter(service_list);
4078
4079         while (g_sequence_iter_is_end(iter) == FALSE) {
4080                 struct connman_service *service = g_sequence_get(iter);
4081
4082                 service->ignore = TRUE;
4083
4084                 set_reconnect_state(service, FALSE);
4085
4086                 __connman_service_disconnect(service);
4087
4088                 iter = g_sequence_iter_next(iter);
4089         }
4090
4091         return 0;
4092
4093 }
4094
4095 /**
4096  * __connman_service_lookup:
4097  * @pattern: search pattern
4098  * @path: return object path
4099  *
4100  * Look up a service path from a search pattern
4101  */
4102 int __connman_service_lookup(const char *pattern, const char **path)
4103 {
4104         GHashTableIter iter;
4105         gpointer key, value;
4106
4107         g_hash_table_iter_init(&iter, service_hash);
4108
4109         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4110                 GSequenceIter *iter = value;
4111                 struct connman_service *service = g_sequence_get(iter);
4112
4113                 if (g_strcmp0(service->identifier, pattern) == 0 ||
4114                                 g_strcmp0(service->name, pattern) == 0) {
4115                         *path = (const char *) service->path;
4116                         return 0;
4117                 }
4118         }
4119
4120         return -ENXIO;
4121 }
4122
4123 /**
4124  * lookup_by_identifier:
4125  * @identifier: service identifier
4126  *
4127  * Look up a service by identifier (reference count will not be increased)
4128  */
4129 static struct connman_service *lookup_by_identifier(const char *identifier)
4130 {
4131         GSequenceIter *iter;
4132
4133         iter = g_hash_table_lookup(service_hash, identifier);
4134         if (iter != NULL)
4135                 return g_sequence_get(iter);
4136
4137         return NULL;
4138 }
4139
4140 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4141                 const char *ssid, const char *mode, const char *security,
4142                 const char *group)
4143 {
4144         struct connman_network *network;
4145         char *name;
4146         int index;
4147         unsigned int i, ssid_len;
4148
4149         ssid_len = strlen(ssid);
4150         if (ssid_len < 1)
4151                 return NULL;
4152
4153         network = connman_network_create(group, CONNMAN_NETWORK_TYPE_WIFI);
4154         if (network == NULL)
4155                 return NULL;
4156
4157         connman_network_set_blob(network, "WiFi.SSID",
4158                                         (unsigned char *) ssid, ssid_len);
4159
4160         connman_network_set_string(network, "WiFi.Mode", mode);
4161         connman_network_set_string(network, "WiFi.Security", security);
4162
4163         name = g_try_malloc0(ssid_len + 1);
4164         if (name == NULL) {
4165                 connman_network_unref(network);
4166                 return NULL;
4167         }
4168
4169         for (i = 0; i < ssid_len; i++) {
4170                 if (g_ascii_isprint(ssid[i]))
4171                         name[i] = ssid[i];
4172                 else
4173                         name[i] = ' ';
4174         }
4175
4176         connman_network_set_name(network, name);
4177
4178         g_free(name);
4179
4180         index = connman_device_get_index(device);
4181         connman_network_set_index(network, index);
4182
4183         if (connman_device_add_network(device, network) < 0) {
4184                 connman_network_unref(network);
4185                 return NULL;
4186         }
4187
4188         connman_network_set_available(network, TRUE);
4189
4190         return network;
4191 }
4192
4193 int __connman_service_create_and_connect(DBusMessage *msg)
4194 {
4195         struct connman_service *service;
4196         struct connman_network *network;
4197         struct connman_device *device;
4198         DBusMessageIter iter, array;
4199         const char *mode = "managed", *security = "none", *group_security;
4200         const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4201         unsigned int ssid_len = 0;
4202         const char *ident;
4203         char *name, *group;
4204         int err;
4205
4206         dbus_message_iter_init(msg, &iter);
4207         dbus_message_iter_recurse(&iter, &array);
4208
4209         while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4210                 DBusMessageIter entry, value;
4211                 const char *key;
4212
4213                 dbus_message_iter_recurse(&array, &entry);
4214                 dbus_message_iter_get_basic(&entry, &key);
4215
4216                 dbus_message_iter_next(&entry);
4217                 dbus_message_iter_recurse(&entry, &value);
4218
4219                 switch (dbus_message_iter_get_arg_type(&value)) {
4220                 case DBUS_TYPE_STRING:
4221                         if (g_str_equal(key, "Type") == TRUE)
4222                                 dbus_message_iter_get_basic(&value, &type);
4223                         else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4224                                         g_str_equal(key, "Mode") == TRUE)
4225                                 dbus_message_iter_get_basic(&value, &mode);
4226                         else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4227                                         g_str_equal(key, "Security") == TRUE)
4228                                 dbus_message_iter_get_basic(&value, &security);
4229                         else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4230                                         g_str_equal(key, "Passphrase") == TRUE)
4231                                 dbus_message_iter_get_basic(&value, &passphrase);
4232                         else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4233                                         g_str_equal(key, "SSID") == TRUE)
4234                                 dbus_message_iter_get_basic(&value, &ssid);
4235                 }
4236
4237                 dbus_message_iter_next(&array);
4238         }
4239
4240         if (type == NULL)
4241                 return -EINVAL;
4242
4243         if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4244                 return -EOPNOTSUPP;
4245
4246         if (ssid == NULL)
4247                 return -EINVAL;
4248
4249         ssid_len = strlen(ssid);
4250         if (ssid_len < 1)
4251                 return -EINVAL;
4252
4253         if (g_strcmp0(security, "none") != 0 &&
4254                                 g_strcmp0(security, "wep") != 0 &&
4255                                 g_strcmp0(security, "psk") != 0 &&
4256                                 g_strcmp0(security, "wpa") != 0 &&
4257                                 g_strcmp0(security, "rsn") != 0 &&
4258                                 g_strcmp0(security, "ieee8021x") != 0)
4259                 return -EINVAL;
4260
4261         device = __connman_device_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4262         if (device == NULL)
4263                 return -EOPNOTSUPP;
4264
4265         ident = connman_device_get_ident(device);
4266         if (ident == NULL)
4267                 return -EOPNOTSUPP;
4268
4269
4270         if (!g_strcmp0(security, "wpa") ||
4271                 !g_strcmp0(security, "rsn"))
4272                 group_security = "psk";
4273         else
4274                 group_security = security;
4275
4276         group = wifi_build_group_name((unsigned char *) ssid,
4277                                                 ssid_len, mode, group_security);
4278         if (group == NULL)
4279                 return -EINVAL;
4280
4281         name = g_strdup_printf("%s_%s_%s", type, ident, group);
4282
4283         service = lookup_by_identifier(name);
4284
4285         if (service != NULL)
4286                 goto done;
4287
4288         network = create_hidden_wifi(device, ssid, mode, security, group);
4289         if (network != NULL)
4290                 connman_network_set_group(network, group);
4291
4292         service = lookup_by_identifier(name);
4293
4294 done:
4295         g_free(name);
4296         g_free(group);
4297
4298         if (service == NULL) {
4299                 err = -EOPNOTSUPP;
4300                 goto failed;
4301         }
4302
4303         service->network_created = TRUE;
4304
4305         if (is_connected(service) == TRUE) {
4306                 err = -EISCONN;
4307                 goto failed;
4308         }
4309
4310         if (is_connecting(service) == TRUE) {
4311                 err = -EALREADY;
4312                 goto failed;
4313         }
4314
4315         set_reconnect_state(service, FALSE);
4316
4317         __connman_device_disconnect(device);
4318
4319         if (passphrase != NULL) {
4320                 g_free(service->passphrase);
4321                 service->passphrase = g_strdup(passphrase);
4322         }
4323
4324         service->userconnect = TRUE;
4325
4326         err = __connman_service_connect(service);
4327         if (err < 0 && err != -EINPROGRESS)
4328                 goto failed;
4329
4330         service->pending = dbus_message_ref(msg);
4331
4332         return 0;
4333
4334 failed:
4335         if (service != NULL && service->network_created == TRUE) {
4336                 struct connman_network *network = service->network;
4337
4338                 if (network != NULL) {
4339                         connman_network_set_available(network, FALSE);
4340                         __connman_device_cleanup_networks(device);
4341                 } else
4342                         __connman_service_put(service);
4343         }
4344
4345         return err;
4346 }
4347
4348 static void provision_changed(gpointer value, gpointer user_data)
4349 {
4350         struct connman_service *service = value;
4351         char *path = user_data;
4352
4353         __connman_config_provision_service_ident(service, path);
4354 }
4355
4356 void __connman_service_provision_changed(const char *ident)
4357 {
4358         g_sequence_foreach(service_list, provision_changed, (void *)ident);
4359 }
4360
4361 int __connman_service_provision(DBusMessage *msg)
4362 {
4363         GKeyFile *keyfile = NULL;
4364         const char *config_str = NULL;
4365         char *group = NULL, *ident = NULL;
4366         int err = 0;
4367         struct connman_service *service;
4368
4369         DBG("");
4370
4371         dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
4372                                                         DBUS_TYPE_INVALID);
4373
4374         if (config_str == NULL || strlen(config_str) == 0)
4375                 return -EINVAL;
4376
4377         keyfile = g_key_file_new();
4378
4379         /* populate GKeyFile with config_str */
4380         if (g_key_file_load_from_data(keyfile, config_str,
4381                                         strlen(config_str), 0, NULL) == FALSE) {
4382                 err = -EINVAL;
4383                 goto done;
4384         }
4385
4386         /*
4387          * read only one group of settings (only one service supported, no
4388          * global settings)
4389          */
4390         group = g_key_file_get_start_group(keyfile);
4391
4392         if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
4393                 err = -EINVAL;
4394                 goto done;
4395         }
4396
4397         err = __connman_config_load_service(keyfile, group, TRUE);
4398         if (err < 0)
4399                 goto done;
4400
4401         ident = group + strlen("service_");
4402
4403         /* trigger service provisioning if service exists */
4404         service = lookup_by_identifier(ident);
4405         if (service != NULL)
4406                 __connman_config_provision_service(service);
4407
4408         g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
4409
4410 done:
4411         if (group != NULL)
4412                 g_free(group);
4413
4414         if (keyfile != NULL)
4415                 g_key_file_free(keyfile);
4416
4417         return err;
4418 }
4419
4420 /**
4421  * __connman_service_get:
4422  * @identifier: service identifier
4423  *
4424  * Look up a service by identifier or create a new one if not found
4425  */
4426 static struct connman_service *service_get(const char *identifier)
4427 {
4428         struct connman_service *service;
4429         GSequenceIter *iter;
4430
4431         iter = g_hash_table_lookup(service_hash, identifier);
4432         if (iter != NULL) {
4433                 service = g_sequence_get(iter);
4434                 if (service != NULL)
4435                         connman_service_ref(service);
4436                 return service;
4437         }
4438
4439         service = connman_service_create();
4440         if (service == NULL)
4441                 return NULL;
4442
4443         DBG("service %p", service);
4444
4445         service->identifier = g_strdup(identifier);
4446
4447         service->profile = g_strdup(__connman_profile_active_ident());
4448
4449         iter = g_sequence_insert_sorted(service_list, service,
4450                                                 service_compare, NULL);
4451
4452         g_hash_table_insert(service_hash, service->identifier, iter);
4453
4454         return service;
4455 }
4456
4457 static int service_register(struct connman_service *service)
4458 {
4459         const char *path = __connman_profile_active_path();
4460         GSequenceIter *iter;
4461
4462         DBG("service %p", service);
4463
4464         if (service->path != NULL)
4465                 return -EALREADY;
4466
4467         service->path = g_strdup_printf("%s/%s", path, service->identifier);
4468
4469         DBG("path %s", service->path);
4470
4471         __connman_config_provision_service(service);
4472
4473         __connman_storage_load_service(service);
4474
4475         g_dbus_register_interface(connection, service->path,
4476                                         CONNMAN_SERVICE_INTERFACE,
4477                                         service_methods, service_signals,
4478                                                         NULL, service, NULL);
4479
4480         iter = g_hash_table_lookup(service_hash, service->identifier);
4481         if (iter != NULL)
4482                 g_sequence_sort_changed(iter, service_compare, NULL);
4483
4484         __connman_profile_changed(TRUE);
4485
4486         return 0;
4487 }
4488
4489 static void service_up(struct connman_ipconfig *ipconfig)
4490 {
4491         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4492
4493         DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
4494
4495         link_changed(service);
4496
4497         service->stats.valid = FALSE;
4498         service->stats_roaming.valid = FALSE;
4499 }
4500
4501 static void service_down(struct connman_ipconfig *ipconfig)
4502 {
4503         DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
4504 }
4505
4506 static void service_lower_up(struct connman_ipconfig *ipconfig)
4507 {
4508         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4509
4510         DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4511
4512         stats_start(service);
4513 }
4514
4515 static void service_lower_down(struct connman_ipconfig *ipconfig)
4516 {
4517         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4518
4519         DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4520
4521         stats_stop(service);
4522         __connman_storage_save_service(service);
4523 }
4524
4525 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4526 {
4527         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4528         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4529         enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4530
4531         DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4532
4533         type = __connman_ipconfig_get_config_type(ipconfig);
4534         method = __connman_ipconfig_get_method(ipconfig);
4535
4536         DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4537                                                         type, method);
4538
4539         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4540                         method == CONNMAN_IPCONFIG_METHOD_AUTO)
4541                 __connman_service_ipconfig_indicate_state(service,
4542                                                 CONNMAN_SERVICE_STATE_READY,
4543                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
4544
4545         settings_changed(service, ipconfig);
4546 }
4547
4548 static void service_ip_release(struct connman_ipconfig *ipconfig)
4549 {
4550         struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4551         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4552         enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4553
4554         DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4555
4556         type = __connman_ipconfig_get_config_type(ipconfig);
4557         method = __connman_ipconfig_get_method(ipconfig);
4558
4559         DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4560                                                         type, method);
4561
4562         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4563                         method == CONNMAN_IPCONFIG_METHOD_OFF)
4564                 __connman_service_ipconfig_indicate_state(service,
4565                                         CONNMAN_SERVICE_STATE_DISCONNECT,
4566                                         CONNMAN_IPCONFIG_TYPE_IPV6);
4567
4568         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4569                         method == CONNMAN_IPCONFIG_METHOD_OFF)
4570                 __connman_service_ipconfig_indicate_state(service,
4571                                         CONNMAN_SERVICE_STATE_DISCONNECT,
4572                                         CONNMAN_IPCONFIG_TYPE_IPV4);
4573
4574         settings_changed(service, ipconfig);
4575 }
4576
4577 static const struct connman_ipconfig_ops service_ops = {
4578         .up             = service_up,
4579         .down           = service_down,
4580         .lower_up       = service_lower_up,
4581         .lower_down     = service_lower_down,
4582         .ip_bound       = service_ip_bound,
4583         .ip_release     = service_ip_release,
4584 };
4585
4586 static void setup_ip4config(struct connman_service *service, int index,
4587                         enum connman_ipconfig_method method)
4588 {
4589         if (index < 0)
4590                 return;
4591
4592         service->ipconfig_ipv4 = connman_ipconfig_create(index,
4593                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
4594         if (service->ipconfig_ipv4 == NULL)
4595                 return;
4596
4597         connman_ipconfig_set_method(service->ipconfig_ipv4, method);
4598
4599         connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4600
4601         connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4602 }
4603
4604 static void setup_ip6config(struct connman_service *service, int index)
4605 {
4606         if (index < 0)
4607                 return;
4608
4609         service->ipconfig_ipv6 = connman_ipconfig_create(index,
4610                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
4611         if (service->ipconfig_ipv6 == NULL)
4612                 return;
4613
4614         connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4615
4616         connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4617 }
4618
4619 void __connman_service_create_ip4config(struct connman_service *service,
4620                                                                 int index)
4621 {
4622         const char *ident = service->profile;
4623         GKeyFile *keyfile;
4624
4625         DBG("ipv4 %p", service->ipconfig_ipv4);
4626
4627         if (service->ipconfig_ipv4 != NULL)
4628                 return;
4629
4630         setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4631
4632         if (ident == NULL)
4633                 return;
4634
4635         keyfile = __connman_storage_open_profile(ident);
4636         if (keyfile == NULL)
4637                 return;
4638
4639         if (service->ipconfig_ipv4)
4640                 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4641                                         service->identifier, "IPv4.");
4642         g_key_file_free(keyfile);
4643 }
4644
4645 void __connman_service_create_ip6config(struct connman_service *service,
4646                                                                 int index)
4647 {
4648         const char *ident = service->profile;
4649         GKeyFile *keyfile;
4650
4651         DBG("ipv6 %p", service->ipconfig_ipv6);
4652
4653         if (service->ipconfig_ipv6 != NULL)
4654                 return;
4655
4656         setup_ip6config(service, index);
4657
4658         if (ident == NULL)
4659                 return;
4660
4661         keyfile = __connman_storage_open_profile(ident);
4662         if (keyfile == NULL)
4663                 return;
4664
4665         if (service->ipconfig_ipv6 != NULL)
4666                 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4667                                         service->identifier, "IPv6.");
4668
4669         g_key_file_free(keyfile);
4670 }
4671
4672 /**
4673  * __connman_service_lookup_from_network:
4674  * @network: network structure
4675  *
4676  * Look up a service by network (reference count will not be increased)
4677  */
4678 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4679 {
4680         struct connman_service *service;
4681         const char *ident, *group;
4682         char *name;
4683
4684         DBG("network %p", network);
4685
4686         ident = __connman_network_get_ident(network);
4687         if (ident == NULL)
4688                 return NULL;
4689
4690         group = connman_network_get_group(network);
4691         if (group == NULL)
4692                 return NULL;
4693
4694         name = g_strdup_printf("%s_%s_%s",
4695                         __connman_network_get_type(network), ident, group);
4696         service = lookup_by_identifier(name);
4697         g_free(name);
4698
4699         return service;
4700 }
4701
4702 struct connman_service *__connman_service_lookup_from_index(int index)
4703 {
4704         struct connman_service *service;
4705         GSequenceIter *iter;
4706
4707         iter = g_sequence_get_begin_iter(service_list);
4708
4709         while (g_sequence_iter_is_end(iter) == FALSE) {
4710                 service = g_sequence_get(iter);
4711
4712                 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
4713                                                         == index)
4714                         return service;
4715
4716                 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
4717                                                         == index)
4718                         return service;
4719
4720                 iter = g_sequence_iter_next(iter);
4721         }
4722
4723         return NULL;
4724 }
4725
4726 const char *__connman_service_get_ident(struct connman_service *service)
4727 {
4728         return service->identifier;
4729 }
4730
4731 const char *__connman_service_get_path(struct connman_service *service)
4732 {
4733         return service->path;
4734 }
4735
4736 unsigned int __connman_service_get_order(struct connman_service *service)
4737 {
4738         GSequenceIter *iter;
4739
4740         if (service == NULL)
4741                 return 0;
4742
4743         if (service->favorite == FALSE) {
4744                 service->order = 0;
4745                 goto done;
4746         }
4747
4748         iter = g_hash_table_lookup(service_hash, service->identifier);
4749         if (iter != NULL) {
4750                 if (g_sequence_iter_get_position(iter) == 0)
4751                         service->order = 1;
4752                 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
4753                         service->order = 10;
4754                 else
4755                         service->order = 0;
4756         }
4757
4758 done:
4759         return service->order;
4760 }
4761
4762 static enum connman_service_type convert_network_type(struct connman_network *network)
4763 {
4764         enum connman_network_type type = connman_network_get_type(network);
4765
4766         switch (type) {
4767         case CONNMAN_NETWORK_TYPE_UNKNOWN:
4768         case CONNMAN_NETWORK_TYPE_VENDOR:
4769                 break;
4770         case CONNMAN_NETWORK_TYPE_ETHERNET:
4771                 return CONNMAN_SERVICE_TYPE_ETHERNET;
4772         case CONNMAN_NETWORK_TYPE_WIFI:
4773                 return CONNMAN_SERVICE_TYPE_WIFI;
4774         case CONNMAN_NETWORK_TYPE_WIMAX:
4775                 return CONNMAN_SERVICE_TYPE_WIMAX;
4776         case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4777         case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4778                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
4779         case CONNMAN_NETWORK_TYPE_CELLULAR:
4780                 return CONNMAN_SERVICE_TYPE_CELLULAR;
4781         }
4782
4783         return CONNMAN_SERVICE_TYPE_UNKNOWN;
4784 }
4785
4786 static enum connman_service_security convert_wifi_security(const char *security)
4787 {
4788         if (security == NULL)
4789                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4790         else if (g_str_equal(security, "none") == TRUE)
4791                 return CONNMAN_SERVICE_SECURITY_NONE;
4792         else if (g_str_equal(security, "wep") == TRUE)
4793                 return CONNMAN_SERVICE_SECURITY_WEP;
4794         else if (g_str_equal(security, "psk") == TRUE)
4795                 return CONNMAN_SERVICE_SECURITY_PSK;
4796         else if (g_str_equal(security, "ieee8021x") == TRUE)
4797                 return CONNMAN_SERVICE_SECURITY_8021X;
4798         else if (g_str_equal(security, "wpa") == TRUE)
4799                 return CONNMAN_SERVICE_SECURITY_WPA;
4800         else if (g_str_equal(security, "rsn") == TRUE)
4801                 return CONNMAN_SERVICE_SECURITY_RSN;
4802         else
4803                 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4804 }
4805
4806 static void update_from_network(struct connman_service *service,
4807                                         struct connman_network *network)
4808 {
4809         connman_uint8_t strength = service->strength;
4810         GSequenceIter *iter;
4811         const char *str;
4812
4813         DBG("service %p network %p", service, network);
4814
4815         if (is_connected(service) == TRUE)
4816                 return;
4817
4818         if (is_connecting(service) == TRUE)
4819                 return;
4820
4821         str = connman_network_get_string(network, "Name");
4822         if (str != NULL) {
4823                 g_free(service->name);
4824                 service->name = g_strdup(str);
4825                 service->hidden = FALSE;
4826         } else {
4827                 g_free(service->name);
4828                 service->name = NULL;
4829                 service->hidden = TRUE;
4830         }
4831
4832         service->strength = connman_network_get_strength(network);
4833         service->roaming = connman_network_get_bool(network, "Roaming");
4834
4835         if (service->strength == 0) {
4836                 /*
4837                  * Filter out 0-values; it's unclear what they mean
4838                  * and they cause anomalous sorting of the priority list.
4839                  */
4840                 service->strength = strength;
4841         }
4842
4843         str = connman_network_get_string(network, "WiFi.Security");
4844         service->security = convert_wifi_security(str);
4845
4846         if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4847                 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4848
4849         if (service->strength > strength && service->network != NULL) {
4850                 service->network = network;
4851
4852                 strength_changed(service);
4853         }
4854
4855         if (service->network == NULL)
4856                 service->network = network;
4857
4858         iter = g_hash_table_lookup(service_hash, service->identifier);
4859         if (iter != NULL)
4860                 g_sequence_sort_changed(iter, service_compare, NULL);
4861 }
4862
4863 /**
4864  * __connman_service_create_from_network:
4865  * @network: network structure
4866  *
4867  * Look up service by network and if not found, create one
4868  */
4869 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
4870 {
4871         struct connman_service *service;
4872         struct connman_device *device;
4873         const char *ident, *group;
4874         char *name;
4875         int index;
4876
4877         DBG("network %p", network);
4878
4879         ident = __connman_network_get_ident(network);
4880         if (ident == NULL)
4881                 return NULL;
4882
4883         group = connman_network_get_group(network);
4884         if (group == NULL)
4885                 return NULL;
4886
4887         name = g_strdup_printf("%s_%s_%s",
4888                         __connman_network_get_type(network), ident, group);
4889         service = service_get(name);
4890         g_free(name);
4891
4892         if (service == NULL)
4893                 return NULL;
4894
4895         if (__connman_network_get_weakness(network) == TRUE)
4896                 return service;
4897
4898         if (service->path != NULL) {
4899                 update_from_network(service, network);
4900                 __connman_profile_changed(TRUE);
4901                 return service;
4902         }
4903
4904         service->type = convert_network_type(network);
4905
4906         switch (service->type) {
4907         case CONNMAN_SERVICE_TYPE_UNKNOWN:
4908         case CONNMAN_SERVICE_TYPE_SYSTEM:
4909         case CONNMAN_SERVICE_TYPE_ETHERNET:
4910         case CONNMAN_SERVICE_TYPE_WIMAX:
4911         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4912         case CONNMAN_SERVICE_TYPE_GPS:
4913         case CONNMAN_SERVICE_TYPE_VPN:
4914         case CONNMAN_SERVICE_TYPE_GADGET:
4915                 service->autoconnect = FALSE;
4916                 break;
4917         case CONNMAN_SERVICE_TYPE_WIFI:
4918         case CONNMAN_SERVICE_TYPE_CELLULAR:
4919                 service->autoconnect = TRUE;
4920                 break;
4921         }
4922
4923         service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4924         service->state = combine_state(service->state_ipv4, service->state_ipv6);
4925
4926         update_from_network(service, network);
4927
4928         index = connman_network_get_index(network);
4929
4930         if (service->ipconfig_ipv4 == NULL)
4931                 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4932
4933         if (service->ipconfig_ipv6 == NULL)
4934                 setup_ip6config(service, index);
4935
4936         service_register(service);
4937
4938         if (service->favorite == TRUE) {
4939                 device = connman_network_get_device(service->network);
4940                 if (device && __connman_device_scanning(device) == FALSE)
4941                         __connman_service_auto_connect();
4942         }
4943
4944         __connman_notifier_service_add(service);
4945
4946         return service;
4947 }
4948
4949 void __connman_service_update_from_network(struct connman_network *network)
4950 {
4951         struct connman_service *service;
4952         connman_uint8_t strength;
4953         connman_bool_t roaming;
4954         GSequenceIter *iter;
4955         const char *name;
4956         connman_bool_t stats_enable;
4957
4958         DBG("network %p", network);
4959
4960         service = __connman_service_lookup_from_network(network);
4961         if (service == NULL)
4962                 return;
4963
4964         if (service->network == NULL)
4965                 return;
4966
4967         name = connman_network_get_string(service->network, "Name");
4968         if (g_strcmp0(service->name, name) != 0) {
4969                 g_free(service->name);
4970                 service->name = g_strdup(name);
4971                 connman_dbus_property_changed_basic(service->path,
4972                                 CONNMAN_SERVICE_INTERFACE, "Name",
4973                                 DBUS_TYPE_STRING, &service->name);
4974         }
4975
4976         if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
4977                 service->wps = connman_network_get_bool(network, "WiFi.WPS");
4978
4979         strength = connman_network_get_strength(service->network);
4980         if (strength == service->strength)
4981                 goto roaming;
4982
4983         service->strength = strength;
4984
4985         strength_changed(service);
4986
4987 roaming:
4988         roaming = connman_network_get_bool(service->network, "Roaming");
4989         if (roaming == service->roaming)
4990                 return;
4991
4992         stats_enable = stats_enabled(service);
4993         if (stats_enable == TRUE)
4994                 stats_stop(service);
4995
4996         service->roaming = roaming;
4997
4998         if (stats_enable == TRUE)
4999                 stats_start(service);
5000
5001         roaming_changed(service);
5002
5003         iter = g_hash_table_lookup(service_hash, service->identifier);
5004         if (iter != NULL)
5005                 g_sequence_sort_changed(iter, service_compare, NULL);
5006 }
5007
5008 void __connman_service_remove_from_network(struct connman_network *network)
5009 {
5010         struct connman_service *service;
5011
5012         DBG("network %p", network);
5013
5014         service = __connman_service_lookup_from_network(network);
5015         if (service == NULL)
5016                 return;
5017
5018         __connman_service_put(service);
5019 }
5020
5021 /**
5022  * __connman_service_create_from_provider:
5023  * @provider: provider structure
5024  *
5025  * Look up service by provider and if not found, create one
5026  */
5027 struct connman_service *
5028 __connman_service_create_from_provider(struct connman_provider *provider)
5029 {
5030         struct connman_service *service;
5031         const char *ident, *str;
5032         char *name;
5033         int index = connman_provider_get_index(provider);
5034
5035         DBG("provider %p", provider);
5036
5037         ident = __connman_provider_get_ident(provider);
5038         if (ident == NULL)
5039                 return NULL;
5040
5041         name = g_strdup_printf("vpn_%s", ident);
5042         service = service_get(name);
5043         g_free(name);
5044
5045         if (service == NULL)
5046                 return NULL;
5047
5048         service->type = CONNMAN_SERVICE_TYPE_VPN;
5049         service->provider = connman_provider_ref(provider);
5050         service->autoconnect = FALSE;
5051
5052         service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5053
5054         str = connman_provider_get_string(provider, "Name");
5055         if (str != NULL) {
5056                 g_free(service->name);
5057                 service->name = g_strdup(str);
5058                 service->hidden = FALSE;
5059         } else {
5060                 g_free(service->name);
5061                 service->name = NULL;
5062                 service->hidden = TRUE;
5063         }
5064
5065         service->strength = 0;
5066
5067         if (service->ipconfig_ipv4 == NULL)
5068                 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5069
5070         if (service->ipconfig_ipv6 == NULL)
5071                 setup_ip6config(service, index);
5072
5073         service_register(service);
5074
5075         __connman_notifier_service_add(service);
5076
5077         return service;
5078 }
5079
5080 static int service_load(struct connman_service *service)
5081 {
5082         const char *ident = service->profile;
5083         GKeyFile *keyfile;
5084         GError *error = NULL;
5085         gchar *pathname, *data = NULL;
5086         gsize length;
5087         gchar *str;
5088         connman_bool_t autoconnect;
5089         unsigned int ssid_len;
5090         int err = 0;
5091
5092         DBG("service %p", service);
5093
5094         if (ident == NULL)
5095                 return -EINVAL;
5096
5097         pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5098         if (pathname == NULL)
5099                 return -ENOMEM;
5100
5101         keyfile = g_key_file_new();
5102
5103         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
5104                 g_free(pathname);
5105                 return -ENOENT;
5106         }
5107
5108         g_free(pathname);
5109
5110         if (g_key_file_load_from_data(keyfile, data, length,
5111                                                         0, NULL) == FALSE) {
5112                 g_free(data);
5113                 return -EILSEQ;
5114         }
5115
5116         g_free(data);
5117
5118         switch (service->type) {
5119         case CONNMAN_SERVICE_TYPE_UNKNOWN:
5120         case CONNMAN_SERVICE_TYPE_SYSTEM:
5121         case CONNMAN_SERVICE_TYPE_ETHERNET:
5122         case CONNMAN_SERVICE_TYPE_GPS:
5123         case CONNMAN_SERVICE_TYPE_VPN:
5124         case CONNMAN_SERVICE_TYPE_GADGET:
5125                 break;
5126         case CONNMAN_SERVICE_TYPE_WIFI:
5127                 if (service->name == NULL) {
5128                         gchar *name;
5129
5130                         name = g_key_file_get_string(keyfile,
5131                                         service->identifier, "Name", NULL);
5132                         if (name != NULL) {
5133                                 g_free(service->name);
5134                                 service->name = name;
5135                         }
5136
5137                         if (service->network != NULL)
5138                                 connman_network_set_name(service->network,
5139                                                                         name);
5140                 }
5141
5142                 if (service->network &&
5143                                 connman_network_get_blob(service->network,
5144                                         "WiFi.SSID", &ssid_len) == NULL) {
5145                         gchar *hex_ssid;
5146
5147                         hex_ssid = g_key_file_get_string(keyfile,
5148                                                         service->identifier,
5149                                                                 "SSID", NULL);
5150
5151                         if (hex_ssid != NULL) {
5152                                 gchar *ssid;
5153                                 unsigned int i, j = 0, hex;
5154                                 size_t hex_ssid_len = strlen(hex_ssid);
5155
5156                                 ssid = g_try_malloc0(hex_ssid_len / 2);
5157                                 if (ssid == NULL) {
5158                                         g_free(hex_ssid);
5159                                         err = -ENOMEM;
5160                                         goto done;
5161                                 }
5162
5163                                 for (i = 0; i < hex_ssid_len; i += 2) {
5164                                         sscanf(hex_ssid + i, "%02x", &hex);
5165                                         ssid[j++] = hex;
5166                                 }
5167
5168                                 connman_network_set_blob(service->network,
5169                                         "WiFi.SSID", ssid, hex_ssid_len / 2);
5170                         }
5171
5172                         g_free(hex_ssid);
5173                 }
5174                 /* fall through */
5175
5176         case CONNMAN_SERVICE_TYPE_WIMAX:
5177         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5178         case CONNMAN_SERVICE_TYPE_CELLULAR:
5179                 service->favorite = g_key_file_get_boolean(keyfile,
5180                                 service->identifier, "Favorite", NULL);
5181
5182                 autoconnect = g_key_file_get_boolean(keyfile,
5183                                 service->identifier, "AutoConnect", &error);
5184                 if (error == NULL)
5185                         service->autoconnect = autoconnect;
5186                 g_clear_error(&error);
5187
5188                 str = g_key_file_get_string(keyfile,
5189                                 service->identifier, "Failure", NULL);
5190                 if (str != NULL) {
5191                         if (service->favorite == FALSE)
5192                                 service->state_ipv4 = service->state_ipv6 =
5193                                         CONNMAN_SERVICE_STATE_FAILURE;
5194                         service->error = string2error(str);
5195                 }
5196                 break;
5197         }
5198
5199         str = g_key_file_get_string(keyfile,
5200                                 service->identifier, "Modified", NULL);
5201         if (str != NULL) {
5202                 g_time_val_from_iso8601(str, &service->modified);
5203                 g_free(str);
5204         }
5205
5206         str = g_key_file_get_string(keyfile,
5207                                 service->identifier, "Passphrase", NULL);
5208         if (str != NULL) {
5209                 g_free(service->passphrase);
5210                 service->passphrase = str;
5211         }
5212
5213         if (service->ipconfig_ipv4 != NULL)
5214                 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5215                                         service->identifier, "IPv4.");
5216
5217         if (service->ipconfig_ipv6 != NULL)
5218                 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5219                                         service->identifier, "IPv6.");
5220
5221         service->nameservers_config = g_key_file_get_string_list(keyfile,
5222                         service->identifier, "Nameservers", &length, NULL);
5223         if (service->nameservers_config != NULL && length == 0) {
5224                 g_strfreev(service->nameservers_config);
5225                 service->nameservers_config = NULL;
5226         }
5227
5228         service->domains = g_key_file_get_string_list(keyfile,
5229                         service->identifier, "Domains", &length, NULL);
5230         if (service->domains != NULL && length == 0) {
5231                 g_strfreev(service->domains);
5232                 service->domains = NULL;
5233         }
5234
5235         str = g_key_file_get_string(keyfile,
5236                                 service->identifier, "Proxy.Method", NULL);
5237         if (str != NULL)
5238                 service->proxy_config = string2proxymethod(str);
5239
5240         g_free(str);
5241
5242         service->proxies = g_key_file_get_string_list(keyfile,
5243                         service->identifier, "Proxy.Servers", &length, NULL);
5244         if (service->proxies != NULL && length == 0) {
5245                 g_strfreev(service->proxies);
5246                 service->proxies = NULL;
5247         }
5248
5249         service->excludes = g_key_file_get_string_list(keyfile,
5250                         service->identifier, "Proxy.Excludes", &length, NULL);
5251         if (service->excludes != NULL && length == 0) {
5252                 g_strfreev(service->excludes);
5253                 service->excludes = NULL;
5254         }
5255
5256         str = g_key_file_get_string(keyfile,
5257                                 service->identifier, "Proxy.URL", NULL);
5258         if (str != NULL) {
5259                 g_free(service->pac);
5260                 service->pac = str;
5261         }
5262
5263 done:
5264         g_key_file_free(keyfile);
5265
5266         return err;
5267 }
5268
5269 static int service_save(struct connman_service *service)
5270 {
5271         const char *ident = service->profile;
5272         GKeyFile *keyfile;
5273         gchar *pathname, *data = NULL;
5274         gsize length;
5275         gchar *str;
5276         const char *cst_str = NULL;
5277         int err = 0;
5278
5279         DBG("service %p", service);
5280
5281         if (ident == NULL)
5282                 return -EINVAL;
5283
5284         pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
5285         if (pathname == NULL)
5286                 return -ENOMEM;
5287
5288         keyfile = g_key_file_new();
5289
5290         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
5291                 goto update;
5292
5293         if (length > 0) {
5294                 if (g_key_file_load_from_data(keyfile, data, length,
5295                                                         0, NULL) == FALSE)
5296                         goto done;
5297         }
5298
5299         g_free(data);
5300
5301 update:
5302         if (service->name != NULL)
5303                 g_key_file_set_string(keyfile, service->identifier,
5304                                                 "Name", service->name);
5305
5306         switch (service->type) {
5307         case CONNMAN_SERVICE_TYPE_UNKNOWN:
5308         case CONNMAN_SERVICE_TYPE_SYSTEM:
5309         case CONNMAN_SERVICE_TYPE_ETHERNET:
5310         case CONNMAN_SERVICE_TYPE_GPS:
5311         case CONNMAN_SERVICE_TYPE_VPN:
5312         case CONNMAN_SERVICE_TYPE_GADGET:
5313                 break;
5314         case CONNMAN_SERVICE_TYPE_WIFI:
5315                 if (service->network) {
5316                         const unsigned char *ssid;
5317                         unsigned int ssid_len = 0;
5318
5319                         ssid = connman_network_get_blob(service->network,
5320                                                         "WiFi.SSID", &ssid_len);
5321
5322                         if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
5323                                 char *identifier = service->identifier;
5324                                 GString *str;
5325                                 unsigned int i;
5326
5327                                 str = g_string_sized_new(ssid_len * 2);
5328                                 if (str == NULL) {
5329                                         err = -ENOMEM;
5330                                         goto done;
5331                                 }
5332
5333                                 for (i = 0; i < ssid_len; i++)
5334                                         g_string_append_printf(str,
5335                                                         "%02x", ssid[i]);
5336
5337                                 g_key_file_set_string(keyfile, identifier,
5338                                                         "SSID", str->str);
5339
5340                                 g_string_free(str, TRUE);
5341                         }
5342                 }
5343                 /* fall through */
5344
5345         case CONNMAN_SERVICE_TYPE_WIMAX:
5346         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5347         case CONNMAN_SERVICE_TYPE_CELLULAR:
5348                 g_key_file_set_boolean(keyfile, service->identifier,
5349                                         "Favorite", service->favorite);
5350
5351                 if (service->favorite == TRUE)
5352                         g_key_file_set_boolean(keyfile, service->identifier,
5353                                         "AutoConnect", service->autoconnect);
5354
5355                 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
5356                         service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
5357                         const char *failure = error2string(service->error);
5358                         if (failure != NULL)
5359                                 g_key_file_set_string(keyfile,
5360                                                         service->identifier,
5361                                                         "Failure", failure);
5362                 } else {
5363                         g_key_file_remove_key(keyfile, service->identifier,
5364                                                         "Failure", NULL);
5365                 }
5366                 break;
5367         }
5368
5369         str = g_time_val_to_iso8601(&service->modified);
5370         if (str != NULL) {
5371                 g_key_file_set_string(keyfile, service->identifier,
5372                                                         "Modified", str);
5373                 g_free(str);
5374         }
5375
5376         if (service->passphrase != NULL && strlen(service->passphrase) > 0)
5377                 g_key_file_set_string(keyfile, service->identifier,
5378                                         "Passphrase", service->passphrase);
5379         else
5380                 g_key_file_remove_key(keyfile, service->identifier,
5381                                                         "Passphrase", NULL);
5382
5383         if (service->ipconfig_ipv4 != NULL)
5384                 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
5385                                         service->identifier, "IPv4.");
5386
5387         if (service->ipconfig_ipv6 != NULL)
5388                 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
5389                                                 service->identifier, "IPv6.");
5390
5391         if (service->nameservers_config != NULL) {
5392                 guint len = g_strv_length(service->nameservers_config);
5393
5394                 g_key_file_set_string_list(keyfile, service->identifier,
5395                                                                 "Nameservers",
5396                                 (const gchar **) service->nameservers_config, len);
5397         } else
5398                 g_key_file_remove_key(keyfile, service->identifier,
5399                                                         "Nameservers", NULL);
5400
5401         if (service->domains != NULL) {
5402                 guint len = g_strv_length(service->domains);
5403
5404                 g_key_file_set_string_list(keyfile, service->identifier,
5405                                                                 "Domains",
5406                                 (const gchar **) service->domains, len);
5407         } else
5408                 g_key_file_remove_key(keyfile, service->identifier,
5409                                                         "Domains", NULL);
5410
5411         cst_str = proxymethod2string(service->proxy_config);
5412         if (cst_str != NULL)
5413                 g_key_file_set_string(keyfile, service->identifier,
5414                                 "Proxy.Method", cst_str);
5415
5416         if (service->proxies != NULL) {
5417                 guint len = g_strv_length(service->proxies);
5418
5419                 g_key_file_set_string_list(keyfile, service->identifier,
5420                                 "Proxy.Servers",
5421                                 (const gchar **) service->proxies, len);
5422         } else
5423                 g_key_file_remove_key(keyfile, service->identifier,
5424                                                 "Proxy.Servers", NULL);
5425
5426         if (service->excludes != NULL) {
5427                 guint len = g_strv_length(service->excludes);
5428
5429                 g_key_file_set_string_list(keyfile, service->identifier,
5430                                 "Proxy.Excludes",
5431                                 (const gchar **) service->excludes, len);
5432         } else
5433                 g_key_file_remove_key(keyfile, service->identifier,
5434                                                 "Proxy.Excludes", NULL);
5435
5436         if (service->pac != NULL && strlen(service->pac) > 0)
5437                 g_key_file_set_string(keyfile, service->identifier,
5438                                         "Proxy.URL", service->pac);
5439         else
5440                 g_key_file_remove_key(keyfile, service->identifier,
5441                                                         "Proxy.URL", NULL);
5442
5443         data = g_key_file_to_data(keyfile, &length, NULL);
5444
5445         if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
5446                 connman_error("Failed to store service information");
5447
5448 done:
5449         g_free(data);
5450
5451         g_key_file_free(keyfile);
5452
5453         g_free(pathname);
5454
5455         return err;
5456 }
5457
5458 static struct connman_storage service_storage = {
5459         .name           = "service",
5460         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
5461         .service_load   = service_load,
5462         .service_save   = service_save,
5463 };
5464
5465 int __connman_service_init(void)
5466 {
5467         DBG("");
5468
5469         connection = connman_dbus_get_connection();
5470
5471         if (connman_storage_register(&service_storage) < 0)
5472                 connman_error("Failed to register service storage");
5473
5474         service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5475                                                                 NULL, NULL);
5476
5477         service_list = g_sequence_new(service_free);
5478
5479         return 0;
5480 }
5481
5482 void __connman_service_cleanup(void)
5483 {
5484         GSequence *list;
5485
5486         DBG("");
5487
5488         list = service_list;
5489         service_list = NULL;
5490         g_sequence_free(list);
5491
5492         g_hash_table_destroy(service_hash);
5493         service_hash = NULL;
5494
5495         g_slist_free(counter_list);
5496         counter_list = NULL;
5497
5498         connman_storage_unregister(&service_storage);
5499
5500         dbus_connection_unref(connection);
5501 }