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