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