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