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