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