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