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