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