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