5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
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.
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.
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
33 #include <connman/storage.h>
34 #include <connman/setting.h>
38 #define CONNECT_TIMEOUT 120
40 static DBusConnection *connection = NULL;
42 static GSequence *service_list = NULL;
43 static GHashTable *service_hash = NULL;
44 static GSList *counter_list = NULL;
45 static unsigned int autoconnect_timeout = 0;
46 static struct connman_service *current_default = NULL;
47 static connman_bool_t services_dirty = FALSE;
49 struct connman_stats {
51 connman_bool_t enabled;
52 struct connman_stats_data data_last;
53 struct connman_stats_data data;
57 struct connman_stats_counter {
58 connman_bool_t append_all;
59 struct connman_stats stats;
60 struct connman_stats stats_roaming;
63 struct connman_service {
65 int session_usage_count;
68 enum connman_service_type type;
69 enum connman_service_security security;
70 enum connman_service_state state;
71 enum connman_service_state state_ipv4;
72 enum connman_service_state state_ipv6;
73 enum connman_service_error error;
74 connman_uint8_t strength;
75 connman_bool_t favorite;
76 connman_bool_t immutable;
77 connman_bool_t hidden;
78 connman_bool_t ignore;
79 connman_bool_t autoconnect;
80 connman_bool_t userconnect;
85 char *agent_passphrase;
86 connman_bool_t roaming;
87 struct connman_ipconfig *ipconfig_ipv4;
88 struct connman_ipconfig *ipconfig_ipv6;
89 struct connman_network *network;
90 struct connman_provider *provider;
92 char **nameservers_config;
93 char **nameservers_auto;
97 char **timeservers_config;
98 /* 802.1x settings from the config files */
101 char *agent_identity;
103 char *client_cert_file;
104 char *private_key_file;
105 char *private_key_passphrase;
107 DBusMessage *pending;
109 struct connman_stats stats;
110 struct connman_stats stats_roaming;
111 GHashTable *counter_table;
112 enum connman_service_proxy_method proxy;
113 enum connman_service_proxy_method proxy_config;
118 int online_check_count;
119 connman_bool_t do_split_routing;
120 connman_bool_t new_service;
121 connman_bool_t hidden_service;
128 struct connman_service *service;
131 static void compare_path(gpointer value, gpointer user_data)
133 struct connman_service *service = value;
134 struct find_data *data = user_data;
136 if (data->service != NULL)
139 if (g_strcmp0(service->path, data->path) == 0)
140 data->service = service;
143 static struct connman_service *find_service(const char *path)
145 struct find_data data = { .path = path, .service = NULL };
147 DBG("path %s", path);
149 g_sequence_foreach(service_list, compare_path, &data);
154 const char *__connman_service_type2string(enum connman_service_type type)
157 case CONNMAN_SERVICE_TYPE_UNKNOWN:
159 case CONNMAN_SERVICE_TYPE_SYSTEM:
161 case CONNMAN_SERVICE_TYPE_ETHERNET:
163 case CONNMAN_SERVICE_TYPE_WIFI:
165 case CONNMAN_SERVICE_TYPE_WIMAX:
167 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
169 case CONNMAN_SERVICE_TYPE_CELLULAR:
171 case CONNMAN_SERVICE_TYPE_GPS:
173 case CONNMAN_SERVICE_TYPE_VPN:
175 case CONNMAN_SERVICE_TYPE_GADGET:
182 enum connman_service_type __connman_service_string2type(const char *str)
185 return CONNMAN_SERVICE_TYPE_UNKNOWN;
187 if (strcmp(str, "ethernet") == 0)
188 return CONNMAN_SERVICE_TYPE_ETHERNET;
189 if (strcmp(str, "gadget") == 0)
190 return CONNMAN_SERVICE_TYPE_GADGET;
191 if (strcmp(str, "wifi") == 0)
192 return CONNMAN_SERVICE_TYPE_WIFI;
193 if (strcmp(str, "cellular") == 0)
194 return CONNMAN_SERVICE_TYPE_CELLULAR;
195 if (strcmp(str, "bluetooth") == 0)
196 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
197 if (strcmp(str, "wimax") == 0)
198 return CONNMAN_SERVICE_TYPE_WIMAX;
199 if (strcmp(str, "vpn") == 0)
200 return CONNMAN_SERVICE_TYPE_VPN;
201 if (strcmp(str, "gps") == 0)
202 return CONNMAN_SERVICE_TYPE_GPS;
203 if (strcmp(str, "system") == 0)
204 return CONNMAN_SERVICE_TYPE_SYSTEM;
206 return CONNMAN_SERVICE_TYPE_UNKNOWN;
209 static const char *security2string(enum connman_service_security security)
212 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
214 case CONNMAN_SERVICE_SECURITY_NONE:
216 case CONNMAN_SERVICE_SECURITY_WEP:
218 case CONNMAN_SERVICE_SECURITY_PSK:
219 case CONNMAN_SERVICE_SECURITY_WPA:
220 case CONNMAN_SERVICE_SECURITY_RSN:
222 case CONNMAN_SERVICE_SECURITY_8021X:
229 static const char *state2string(enum connman_service_state state)
232 case CONNMAN_SERVICE_STATE_UNKNOWN:
234 case CONNMAN_SERVICE_STATE_IDLE:
236 case CONNMAN_SERVICE_STATE_ASSOCIATION:
237 return "association";
238 case CONNMAN_SERVICE_STATE_CONFIGURATION:
239 return "configuration";
240 case CONNMAN_SERVICE_STATE_READY:
242 case CONNMAN_SERVICE_STATE_ONLINE:
244 case CONNMAN_SERVICE_STATE_DISCONNECT:
246 case CONNMAN_SERVICE_STATE_FAILURE:
253 static const char *error2string(enum connman_service_error error)
256 case CONNMAN_SERVICE_ERROR_UNKNOWN:
258 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
259 return "out-of-range";
260 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
261 return "pin-missing";
262 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
263 return "dhcp-failed";
264 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
265 return "connect-failed";
266 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
267 return "login-failed";
268 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
269 return "auth-failed";
270 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
271 return "invalid-key";
277 static enum connman_service_error string2error(const char *error)
279 if (g_strcmp0(error, "dhcp-failed") == 0)
280 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
281 else if (g_strcmp0(error, "pin-missing") == 0)
282 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
283 else if (g_strcmp0(error, "invalid-key") == 0)
284 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
286 return CONNMAN_SERVICE_ERROR_UNKNOWN;
289 static const char *proxymethod2string(enum connman_service_proxy_method method)
292 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
294 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
296 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
298 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
305 static enum connman_service_proxy_method string2proxymethod(const char *method)
307 if (g_strcmp0(method, "direct") == 0)
308 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
309 else if (g_strcmp0(method, "auto") == 0)
310 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
311 else if (g_strcmp0(method, "manual") == 0)
312 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
314 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
317 static int service_load(struct connman_service *service)
320 GError *error = NULL;
323 connman_bool_t autoconnect;
324 unsigned int ssid_len;
327 DBG("service %p", service);
329 keyfile = connman_storage_load_service(service->identifier);
330 if (keyfile == NULL) {
331 service->new_service = TRUE;
334 service->new_service = FALSE;
336 switch (service->type) {
337 case CONNMAN_SERVICE_TYPE_UNKNOWN:
338 case CONNMAN_SERVICE_TYPE_SYSTEM:
339 case CONNMAN_SERVICE_TYPE_GPS:
340 case CONNMAN_SERVICE_TYPE_GADGET:
342 case CONNMAN_SERVICE_TYPE_VPN:
343 service->do_split_routing = g_key_file_get_boolean(keyfile,
344 service->identifier, "SplitRouting", NULL);
346 case CONNMAN_SERVICE_TYPE_WIFI:
347 if (service->name == NULL) {
350 name = g_key_file_get_string(keyfile,
351 service->identifier, "Name", NULL);
353 g_free(service->name);
354 service->name = name;
357 if (service->network != NULL)
358 connman_network_set_name(service->network,
362 if (service->network &&
363 connman_network_get_blob(service->network,
364 "WiFi.SSID", &ssid_len) == NULL) {
367 hex_ssid = g_key_file_get_string(keyfile,
371 if (hex_ssid != NULL) {
373 unsigned int i, j = 0, hex;
374 size_t hex_ssid_len = strlen(hex_ssid);
376 ssid = g_try_malloc0(hex_ssid_len / 2);
383 for (i = 0; i < hex_ssid_len; i += 2) {
384 sscanf(hex_ssid + i, "%02x", &hex);
388 connman_network_set_blob(service->network,
389 "WiFi.SSID", ssid, hex_ssid_len / 2);
396 case CONNMAN_SERVICE_TYPE_WIMAX:
397 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
398 case CONNMAN_SERVICE_TYPE_CELLULAR:
399 service->favorite = g_key_file_get_boolean(keyfile,
400 service->identifier, "Favorite", NULL);
402 str = g_key_file_get_string(keyfile,
403 service->identifier, "Failure", NULL);
405 if (service->favorite == FALSE)
406 service->state_ipv4 = service->state_ipv6 =
407 CONNMAN_SERVICE_STATE_FAILURE;
408 service->error = string2error(str);
413 case CONNMAN_SERVICE_TYPE_ETHERNET:
414 autoconnect = g_key_file_get_boolean(keyfile,
415 service->identifier, "AutoConnect", &error);
417 service->autoconnect = autoconnect;
418 g_clear_error(&error);
422 str = g_key_file_get_string(keyfile,
423 service->identifier, "Modified", NULL);
425 g_time_val_from_iso8601(str, &service->modified);
429 str = g_key_file_get_string(keyfile,
430 service->identifier, "Passphrase", NULL);
432 g_free(service->passphrase);
433 service->passphrase = str;
436 if (service->ipconfig_ipv4 != NULL)
437 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
438 service->identifier, "IPv4.");
440 if (service->ipconfig_ipv6 != NULL)
441 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
442 service->identifier, "IPv6.");
444 service->nameservers_config = g_key_file_get_string_list(keyfile,
445 service->identifier, "Nameservers", &length, NULL);
446 if (service->nameservers_config != NULL && length == 0) {
447 g_strfreev(service->nameservers_config);
448 service->nameservers_config = NULL;
451 service->timeservers_config = g_key_file_get_string_list(keyfile,
452 service->identifier, "Timeservers", &length, NULL);
453 if (service->timeservers_config != NULL && length == 0) {
454 g_strfreev(service->timeservers_config);
455 service->timeservers_config = NULL;
458 service->domains = g_key_file_get_string_list(keyfile,
459 service->identifier, "Domains", &length, NULL);
460 if (service->domains != NULL && length == 0) {
461 g_strfreev(service->domains);
462 service->domains = NULL;
465 str = g_key_file_get_string(keyfile,
466 service->identifier, "Proxy.Method", NULL);
468 service->proxy_config = string2proxymethod(str);
472 service->proxies = g_key_file_get_string_list(keyfile,
473 service->identifier, "Proxy.Servers", &length, NULL);
474 if (service->proxies != NULL && length == 0) {
475 g_strfreev(service->proxies);
476 service->proxies = NULL;
479 service->excludes = g_key_file_get_string_list(keyfile,
480 service->identifier, "Proxy.Excludes", &length, NULL);
481 if (service->excludes != NULL && length == 0) {
482 g_strfreev(service->excludes);
483 service->excludes = NULL;
486 str = g_key_file_get_string(keyfile,
487 service->identifier, "Proxy.URL", NULL);
489 g_free(service->pac);
493 service->hidden_service = g_key_file_get_boolean(keyfile,
494 service->identifier, "Hidden", NULL);
497 g_key_file_free(keyfile);
502 static int service_save(struct connman_service *service)
507 const char *cst_str = NULL;
510 DBG("service %p new %d", service, service->new_service);
512 if (service->new_service == TRUE)
515 keyfile = __connman_storage_open_service(service->identifier);
519 if (service->name != NULL)
520 g_key_file_set_string(keyfile, service->identifier,
521 "Name", service->name);
523 switch (service->type) {
524 case CONNMAN_SERVICE_TYPE_UNKNOWN:
525 case CONNMAN_SERVICE_TYPE_SYSTEM:
526 case CONNMAN_SERVICE_TYPE_GPS:
527 case CONNMAN_SERVICE_TYPE_GADGET:
529 case CONNMAN_SERVICE_TYPE_VPN:
530 g_key_file_set_boolean(keyfile, service->identifier,
531 "SplitRouting", service->do_split_routing);
533 case CONNMAN_SERVICE_TYPE_WIFI:
534 if (service->network) {
535 const unsigned char *ssid;
536 unsigned int ssid_len = 0;
538 ssid = connman_network_get_blob(service->network,
539 "WiFi.SSID", &ssid_len);
541 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
542 char *identifier = service->identifier;
546 ssid_str = g_string_sized_new(ssid_len * 2);
547 if (ssid_str == NULL) {
552 for (i = 0; i < ssid_len; i++)
553 g_string_append_printf(ssid_str,
556 g_key_file_set_string(keyfile, identifier,
557 "SSID", ssid_str->str);
559 g_string_free(ssid_str, TRUE);
562 freq = connman_network_get_frequency(service->network);
563 g_key_file_set_integer(keyfile, service->identifier,
568 case CONNMAN_SERVICE_TYPE_WIMAX:
569 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
570 case CONNMAN_SERVICE_TYPE_CELLULAR:
571 g_key_file_set_boolean(keyfile, service->identifier,
572 "Favorite", service->favorite);
574 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
575 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
576 const char *failure = error2string(service->error);
578 g_key_file_set_string(keyfile,
582 g_key_file_remove_key(keyfile, service->identifier,
587 case CONNMAN_SERVICE_TYPE_ETHERNET:
588 if (service->favorite == TRUE)
589 g_key_file_set_boolean(keyfile, service->identifier,
590 "AutoConnect", service->autoconnect);
594 str = g_time_val_to_iso8601(&service->modified);
596 g_key_file_set_string(keyfile, service->identifier,
601 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
602 g_key_file_set_string(keyfile, service->identifier,
603 "Passphrase", service->passphrase);
605 g_key_file_remove_key(keyfile, service->identifier,
608 if (service->ipconfig_ipv4 != NULL)
609 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
610 service->identifier, "IPv4.");
612 if (service->ipconfig_ipv6 != NULL)
613 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
614 service->identifier, "IPv6.");
616 if (service->nameservers_config != NULL) {
617 guint len = g_strv_length(service->nameservers_config);
619 g_key_file_set_string_list(keyfile, service->identifier,
621 (const gchar **) service->nameservers_config, len);
623 g_key_file_remove_key(keyfile, service->identifier,
624 "Nameservers", NULL);
626 if (service->timeservers_config != NULL) {
627 guint len = g_strv_length(service->timeservers_config);
629 g_key_file_set_string_list(keyfile, service->identifier,
631 (const gchar **) service->timeservers_config, len);
633 g_key_file_remove_key(keyfile, service->identifier,
634 "Timeservers", NULL);
636 if (service->domains != NULL) {
637 guint len = g_strv_length(service->domains);
639 g_key_file_set_string_list(keyfile, service->identifier,
641 (const gchar **) service->domains, len);
643 g_key_file_remove_key(keyfile, service->identifier,
646 cst_str = proxymethod2string(service->proxy_config);
648 g_key_file_set_string(keyfile, service->identifier,
649 "Proxy.Method", cst_str);
651 if (service->proxies != NULL) {
652 guint len = g_strv_length(service->proxies);
654 g_key_file_set_string_list(keyfile, service->identifier,
656 (const gchar **) service->proxies, len);
658 g_key_file_remove_key(keyfile, service->identifier,
659 "Proxy.Servers", NULL);
661 if (service->excludes != NULL) {
662 guint len = g_strv_length(service->excludes);
664 g_key_file_set_string_list(keyfile, service->identifier,
666 (const gchar **) service->excludes, len);
668 g_key_file_remove_key(keyfile, service->identifier,
669 "Proxy.Excludes", NULL);
671 if (service->pac != NULL && strlen(service->pac) > 0)
672 g_key_file_set_string(keyfile, service->identifier,
673 "Proxy.URL", service->pac);
675 g_key_file_remove_key(keyfile, service->identifier,
678 if (service->hidden_service == TRUE)
679 g_key_file_set_boolean(keyfile, service->identifier, "Hidden",
682 if (service->config_file != NULL && strlen(service->config_file) > 0)
683 g_key_file_set_string(keyfile, service->identifier,
684 "Config.file", service->config_file);
686 if (service->config_entry != NULL &&
687 strlen(service->config_entry) > 0)
688 g_key_file_set_string(keyfile, service->identifier,
689 "Config.ident", service->config_entry);
692 __connman_storage_save_service(keyfile, service->identifier);
694 g_key_file_free(keyfile);
699 void __connman_service_save(struct connman_service *service)
701 service_save(service);
704 static enum connman_service_state combine_state(
705 enum connman_service_state state_a,
706 enum connman_service_state state_b)
708 enum connman_service_state result;
710 if (state_a == state_b) {
715 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
720 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
725 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
730 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
735 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
740 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
745 if (state_a == CONNMAN_SERVICE_STATE_READY) {
750 if (state_b == CONNMAN_SERVICE_STATE_READY) {
755 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
760 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
765 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
770 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
775 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
780 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
785 result = CONNMAN_SERVICE_STATE_FAILURE;
791 static connman_bool_t is_connecting_state(struct connman_service *service,
792 enum connman_service_state state)
795 case CONNMAN_SERVICE_STATE_UNKNOWN:
796 case CONNMAN_SERVICE_STATE_IDLE:
797 case CONNMAN_SERVICE_STATE_FAILURE:
798 if (service->network != NULL)
799 return connman_network_get_connecting(service->network);
800 case CONNMAN_SERVICE_STATE_DISCONNECT:
801 case CONNMAN_SERVICE_STATE_READY:
802 case CONNMAN_SERVICE_STATE_ONLINE:
804 case CONNMAN_SERVICE_STATE_ASSOCIATION:
805 case CONNMAN_SERVICE_STATE_CONFIGURATION:
812 static connman_bool_t is_connected_state(const struct connman_service *service,
813 enum connman_service_state state)
816 case CONNMAN_SERVICE_STATE_UNKNOWN:
817 case CONNMAN_SERVICE_STATE_IDLE:
818 case CONNMAN_SERVICE_STATE_ASSOCIATION:
819 case CONNMAN_SERVICE_STATE_CONFIGURATION:
820 case CONNMAN_SERVICE_STATE_DISCONNECT:
821 case CONNMAN_SERVICE_STATE_FAILURE:
823 case CONNMAN_SERVICE_STATE_READY:
824 case CONNMAN_SERVICE_STATE_ONLINE:
831 static connman_bool_t is_idle_state(const struct connman_service *service,
832 enum connman_service_state state)
835 case CONNMAN_SERVICE_STATE_UNKNOWN:
836 case CONNMAN_SERVICE_STATE_ASSOCIATION:
837 case CONNMAN_SERVICE_STATE_CONFIGURATION:
838 case CONNMAN_SERVICE_STATE_READY:
839 case CONNMAN_SERVICE_STATE_ONLINE:
840 case CONNMAN_SERVICE_STATE_DISCONNECT:
841 case CONNMAN_SERVICE_STATE_FAILURE:
843 case CONNMAN_SERVICE_STATE_IDLE:
850 static connman_bool_t is_connecting(struct connman_service *service)
852 return is_connecting_state(service, service->state);
855 static connman_bool_t is_connected(struct connman_service *service)
857 return is_connected_state(service, service->state);
860 static const char *nameserver_get_ifname(struct connman_service *service)
864 if (service->ipconfig_ipv4)
865 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv4);
866 else if (service->ipconfig_ipv6)
867 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv6);
874 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
875 case CONNMAN_SERVICE_STATE_UNKNOWN:
876 case CONNMAN_SERVICE_STATE_IDLE:
877 case CONNMAN_SERVICE_STATE_ASSOCIATION:
878 case CONNMAN_SERVICE_STATE_CONFIGURATION:
879 case CONNMAN_SERVICE_STATE_FAILURE:
880 case CONNMAN_SERVICE_STATE_DISCONNECT:
882 case CONNMAN_SERVICE_STATE_READY:
883 case CONNMAN_SERVICE_STATE_ONLINE:
890 static void remove_nameservers(struct connman_service *service,
891 const char* interface, char **ns)
893 const char *ifname = interface;
899 if (interface == NULL)
900 ifname = nameserver_get_ifname(service);
905 for (i = 0; ns[i] != NULL; i++)
906 connman_resolver_remove(ifname, NULL, ns[i]);
909 static void remove_searchdomains(struct connman_service *service,
910 const char *interface, char **sd)
912 const char *ifname = interface;
918 if (interface == NULL)
919 ifname = nameserver_get_ifname(service);
924 for (i = 0; sd[i] != NULL; i++)
925 connman_resolver_remove(ifname, sd[i], NULL);
928 static void update_nameservers(struct connman_service *service)
932 if (service->ipconfig_ipv4)
933 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv4);
934 else if (service->ipconfig_ipv6)
935 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv6);
942 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
943 case CONNMAN_SERVICE_STATE_UNKNOWN:
944 case CONNMAN_SERVICE_STATE_IDLE:
945 case CONNMAN_SERVICE_STATE_ASSOCIATION:
946 case CONNMAN_SERVICE_STATE_CONFIGURATION:
948 case CONNMAN_SERVICE_STATE_FAILURE:
949 case CONNMAN_SERVICE_STATE_DISCONNECT:
950 connman_resolver_remove_all(ifname);
952 case CONNMAN_SERVICE_STATE_READY:
953 case CONNMAN_SERVICE_STATE_ONLINE:
957 if (service->nameservers_config != NULL) {
960 remove_nameservers(service, ifname, service->nameservers);
962 i = g_strv_length(service->nameservers_config);
965 connman_resolver_append(ifname, NULL,
966 service->nameservers_config[i]);
968 } else if (service->nameservers != NULL) {
971 i = g_strv_length(service->nameservers);
974 connman_resolver_append(ifname, NULL,
975 service->nameservers[i]);
979 if (service->domains != NULL) {
980 char *searchdomains[2] = {NULL, NULL};
983 searchdomains[0] = service->domainname;
984 remove_searchdomains(service, ifname, searchdomains);
986 i = g_strv_length(service->domains);
989 connman_resolver_append(ifname, service->domains[i],
992 } else if (service->domainname != NULL)
993 connman_resolver_append(ifname, service->domainname, NULL);
995 connman_resolver_flush();
999 * The is_auto variable is set to true when IPv6 autoconf nameservers are
1000 * inserted to resolver via netlink message (see rtnl.c:rtnl_newnduseropt()
1001 * for details) and not through service.c
1003 int __connman_service_nameserver_append(struct connman_service *service,
1004 const char *nameserver, gboolean is_auto)
1009 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
1011 if (nameserver == NULL)
1014 if (is_auto == TRUE)
1015 nameservers = service->nameservers_auto;
1017 nameservers = service->nameservers;
1019 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
1020 if (g_strcmp0(nameservers[i], nameserver) == 0)
1023 if (nameservers != NULL) {
1024 len = g_strv_length(nameservers);
1025 nameservers = g_try_renew(char *, nameservers, len + 2);
1028 nameservers = g_try_new0(char *, len + 2);
1031 if (nameservers == NULL)
1034 nameservers[len] = g_strdup(nameserver);
1035 if (nameservers[len] == NULL)
1038 nameservers[len + 1] = NULL;
1040 if (is_auto == TRUE) {
1041 service->nameservers_auto = nameservers;
1043 service->nameservers = nameservers;
1044 update_nameservers(service);
1050 int __connman_service_nameserver_remove(struct connman_service *service,
1051 const char *nameserver, gboolean is_auto)
1053 char **servers, **nameservers;
1054 gboolean found = FALSE;
1057 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
1059 if (nameserver == NULL)
1062 if (is_auto == TRUE)
1063 nameservers = service->nameservers_auto;
1065 nameservers = service->nameservers;
1067 if (nameservers == NULL)
1070 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
1071 if (g_strcmp0(nameservers[i], nameserver) == 0) {
1079 len = g_strv_length(nameservers);
1082 g_strfreev(nameservers);
1083 if (is_auto == TRUE)
1084 service->nameservers_auto = NULL;
1086 service->nameservers = NULL;
1091 servers = g_try_new0(char *, len);
1092 if (servers == NULL)
1095 for (i = 0, j = 0; i < len; i++) {
1096 if (g_strcmp0(nameservers[i], nameserver) != 0) {
1097 servers[j] = g_strdup(nameservers[i]);
1098 if (servers[j] == NULL)
1103 servers[len - 1] = NULL;
1105 g_strfreev(nameservers);
1106 nameservers = servers;
1108 if (is_auto == TRUE) {
1109 service->nameservers_auto = nameservers;
1111 service->nameservers = nameservers;
1112 update_nameservers(service);
1118 void __connman_service_nameserver_clear(struct connman_service *service)
1120 g_strfreev(service->nameservers);
1121 service->nameservers = NULL;
1123 update_nameservers(service);
1126 static void add_nameserver_route(int family, int index, char *nameserver,
1131 if (connman_inet_compare_subnet(index, nameserver) == TRUE)
1134 if (connman_inet_add_host_route(index, nameserver, gw) < 0)
1135 /* For P-t-P link the above route add will fail */
1136 connman_inet_add_host_route(index, nameserver, NULL);
1140 if (connman_inet_add_ipv6_host_route(index, nameserver,
1142 connman_inet_add_ipv6_host_route(index, nameserver,
1148 static void nameserver_add_routes(int index, char **nameservers,
1153 for (i = 0; nameservers[i] != NULL; i++) {
1154 family = connman_inet_check_ipaddress(nameservers[i]);
1158 add_nameserver_route(family, index, nameservers[i], gw);
1162 static void nameserver_del_routes(int index, char **nameservers,
1163 enum connman_ipconfig_type type)
1167 for (i = 0; nameservers[i] != NULL; i++) {
1168 family = connman_inet_check_ipaddress(nameservers[i]);
1174 if (type != CONNMAN_IPCONFIG_TYPE_IPV6)
1175 connman_inet_del_host_route(index,
1179 if (type != CONNMAN_IPCONFIG_TYPE_IPV4)
1180 connman_inet_del_ipv6_host_route(index,
1187 void __connman_service_nameserver_add_routes(struct connman_service *service,
1192 if (service == NULL)
1195 if (service->network != NULL)
1196 index = connman_network_get_index(service->network);
1197 else if (service->provider != NULL)
1198 index = connman_provider_get_index(service->provider);
1200 if (service->nameservers_config != NULL) {
1202 * Configured nameserver takes preference over the
1203 * discoverd nameserver gathered from DHCP, VPN, etc.
1205 nameserver_add_routes(index, service->nameservers_config, gw);
1206 } else if (service->nameservers != NULL) {
1208 * We add nameservers host routes for nameservers that
1209 * are not on our subnet. For those who are, the subnet
1210 * route will be installed by the time the dns proxy code
1211 * tries to reach them. The subnet route is installed
1212 * when setting the interface IP address.
1214 nameserver_add_routes(index, service->nameservers, gw);
1218 void __connman_service_nameserver_del_routes(struct connman_service *service,
1219 enum connman_ipconfig_type type)
1223 if (service == NULL)
1226 if (service->network != NULL)
1227 index = connman_network_get_index(service->network);
1228 else if (service->provider != NULL)
1229 index = connman_provider_get_index(service->provider);
1231 if (service->nameservers_config != NULL)
1232 nameserver_del_routes(index, service->nameservers_config,
1234 else if (service->nameservers != NULL)
1235 nameserver_del_routes(index, service->nameservers, type);
1238 static struct connman_stats *stats_get(struct connman_service *service)
1240 if (service->roaming == TRUE)
1241 return &service->stats_roaming;
1243 return &service->stats;
1246 static connman_bool_t stats_enabled(struct connman_service *service)
1248 struct connman_stats *stats = stats_get(service);
1250 return stats->enabled;
1253 static void stats_start(struct connman_service *service)
1255 struct connman_stats *stats = stats_get(service);
1257 DBG("service %p", service);
1259 if (stats->timer == NULL)
1262 stats->enabled = TRUE;
1263 stats->data_last.time = stats->data.time;
1265 g_timer_start(stats->timer);
1268 static void stats_stop(struct connman_service *service)
1270 struct connman_stats *stats = stats_get(service);
1271 unsigned int seconds;
1273 DBG("service %p", service);
1275 if (stats->timer == NULL)
1278 if (stats->enabled == FALSE)
1281 g_timer_stop(stats->timer);
1283 seconds = g_timer_elapsed(stats->timer, NULL);
1284 stats->data.time = stats->data_last.time + seconds;
1286 stats->enabled = FALSE;
1289 static void reset_stats(struct connman_service *service)
1291 DBG("service %p", service);
1294 service->stats.valid = FALSE;
1296 service->stats.data.rx_packets = 0;
1297 service->stats.data.tx_packets = 0;
1298 service->stats.data.rx_bytes = 0;
1299 service->stats.data.tx_bytes = 0;
1300 service->stats.data.rx_errors = 0;
1301 service->stats.data.tx_errors = 0;
1302 service->stats.data.rx_dropped = 0;
1303 service->stats.data.tx_dropped = 0;
1304 service->stats.data.time = 0;
1305 service->stats.data_last.time = 0;
1307 g_timer_reset(service->stats.timer);
1310 service->stats_roaming.valid = FALSE;
1312 service->stats_roaming.data.rx_packets = 0;
1313 service->stats_roaming.data.tx_packets = 0;
1314 service->stats_roaming.data.rx_bytes = 0;
1315 service->stats_roaming.data.tx_bytes = 0;
1316 service->stats_roaming.data.rx_errors = 0;
1317 service->stats_roaming.data.tx_errors = 0;
1318 service->stats_roaming.data.rx_dropped = 0;
1319 service->stats_roaming.data.tx_dropped = 0;
1320 service->stats_roaming.data.time = 0;
1321 service->stats_roaming.data_last.time = 0;
1323 g_timer_reset(service->stats_roaming.timer);
1326 struct connman_service *__connman_service_get_default(void)
1328 struct connman_service *service;
1329 GSequenceIter *iter;
1331 iter = g_sequence_get_begin_iter(service_list);
1333 if (g_sequence_iter_is_end(iter) == TRUE)
1336 service = g_sequence_get(iter);
1338 if (is_connected(service) == FALSE)
1344 static void default_changed(void)
1346 struct connman_service *service = __connman_service_get_default();
1348 if (service == current_default)
1351 __connman_service_timeserver_changed(current_default, NULL);
1353 current_default = service;
1355 __connman_notifier_default_changed(service);
1358 static void state_changed(struct connman_service *service)
1362 __connman_notifier_service_state_changed(service, service->state);
1364 str = state2string(service->state);
1368 connman_dbus_property_changed_basic(service->path,
1369 CONNMAN_SERVICE_INTERFACE, "State",
1370 DBUS_TYPE_STRING, &str);
1373 static void strength_changed(struct connman_service *service)
1375 if (service->strength == 0)
1378 connman_dbus_property_changed_basic(service->path,
1379 CONNMAN_SERVICE_INTERFACE, "Strength",
1380 DBUS_TYPE_BYTE, &service->strength);
1383 static void favorite_changed(struct connman_service *service)
1385 if (service->path == NULL)
1388 connman_dbus_property_changed_basic(service->path,
1389 CONNMAN_SERVICE_INTERFACE, "Favorite",
1390 DBUS_TYPE_BOOLEAN, &service->favorite);
1393 static void immutable_changed(struct connman_service *service)
1395 if (service->path == NULL)
1398 connman_dbus_property_changed_basic(service->path,
1399 CONNMAN_SERVICE_INTERFACE, "Immutable",
1400 DBUS_TYPE_BOOLEAN, &service->immutable);
1403 static void roaming_changed(struct connman_service *service)
1405 if (service->path == NULL)
1408 connman_dbus_property_changed_basic(service->path,
1409 CONNMAN_SERVICE_INTERFACE, "Roaming",
1410 DBUS_TYPE_BOOLEAN, &service->roaming);
1413 static void autoconnect_changed(struct connman_service *service)
1415 if (service->path == NULL)
1418 connman_dbus_property_changed_basic(service->path,
1419 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
1420 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1423 static void append_security(DBusMessageIter *iter, void *user_data)
1425 struct connman_service *service = user_data;
1428 str = security2string(service->security);
1430 dbus_message_iter_append_basic(iter,
1431 DBUS_TYPE_STRING, &str);
1434 * Some access points incorrectly advertise WPS even when they
1435 * are configured as open or no security, so filter
1438 if (service->wps == TRUE) {
1439 switch (service->security) {
1440 case CONNMAN_SERVICE_SECURITY_PSK:
1441 case CONNMAN_SERVICE_SECURITY_WPA:
1442 case CONNMAN_SERVICE_SECURITY_RSN:
1444 dbus_message_iter_append_basic(iter,
1445 DBUS_TYPE_STRING, &str);
1447 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1448 case CONNMAN_SERVICE_SECURITY_NONE:
1449 case CONNMAN_SERVICE_SECURITY_WEP:
1450 case CONNMAN_SERVICE_SECURITY_8021X:
1456 static void append_ethernet(DBusMessageIter *iter, void *user_data)
1458 struct connman_service *service = user_data;
1460 if (service->ipconfig_ipv4 != NULL)
1461 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
1463 else if (service->ipconfig_ipv6 != NULL)
1464 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
1468 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1470 struct connman_service *service = user_data;
1472 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1473 state2string(service->state_ipv4));
1475 if (is_connected_state(service, service->state_ipv4) == FALSE)
1478 if (service->ipconfig_ipv4 != NULL)
1479 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1482 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1484 struct connman_service *service = user_data;
1486 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1487 state2string(service->state_ipv6));
1489 if (is_connected_state(service, service->state_ipv6) == FALSE)
1492 if (service->ipconfig_ipv6 != NULL)
1493 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1494 service->ipconfig_ipv4);
1497 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1499 struct connman_service *service = user_data;
1501 if (service->ipconfig_ipv4 != NULL)
1502 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1506 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1508 struct connman_service *service = user_data;
1510 if (service->ipconfig_ipv6 != NULL)
1511 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1515 static void append_nameservers(DBusMessageIter *iter, char **servers)
1521 for (i = 0; servers[i] != NULL; i++) {
1522 DBG("servers[%d] %s", i, servers[i]);
1523 dbus_message_iter_append_basic(iter,
1524 DBUS_TYPE_STRING, &servers[i]);
1528 static void append_dns(DBusMessageIter *iter, void *user_data)
1530 struct connman_service *service = user_data;
1532 if (is_connected(service) == FALSE)
1535 if (service->nameservers_config != NULL) {
1536 append_nameservers(iter, service->nameservers_config);
1539 if (service->nameservers != NULL)
1540 append_nameservers(iter, service->nameservers);
1542 if (service->nameservers_auto != NULL)
1543 append_nameservers(iter, service->nameservers_auto);
1547 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1549 struct connman_service *service = user_data;
1551 if (service->nameservers_config == NULL)
1554 append_nameservers(iter, service->nameservers_config);
1557 static void append_ts(DBusMessageIter *iter, void *user_data)
1559 GSList *list = user_data;
1561 while (list != NULL) {
1562 char *timeserver = list->data;
1564 if (timeserver != NULL)
1565 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
1568 list = g_slist_next(list);
1572 static void append_tsconfig(DBusMessageIter *iter, void *user_data)
1574 struct connman_service *service = user_data;
1577 if (service->timeservers_config == NULL)
1580 for (i = 0; service->timeservers_config[i]; i++) {
1581 dbus_message_iter_append_basic(iter,
1583 &service->timeservers_config[i]);
1587 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1589 struct connman_service *service = user_data;
1592 if (service->domains == NULL)
1595 for (i = 0; service->domains[i]; i++)
1596 dbus_message_iter_append_basic(iter,
1597 DBUS_TYPE_STRING, &service->domains[i]);
1600 static void append_domain(DBusMessageIter *iter, void *user_data)
1602 struct connman_service *service = user_data;
1604 if (is_connected(service) == FALSE &&
1605 is_connecting(service) == FALSE)
1608 if (service->domains != NULL)
1609 append_domainconfig(iter, user_data);
1610 else if (service->domainname != NULL)
1611 dbus_message_iter_append_basic(iter,
1612 DBUS_TYPE_STRING, &service->domainname);
1615 static void append_proxies(DBusMessageIter *iter, void *user_data)
1617 struct connman_service *service = user_data;
1620 if (service->proxies == NULL)
1623 for (i = 0; service->proxies[i]; i++)
1624 dbus_message_iter_append_basic(iter,
1625 DBUS_TYPE_STRING, &service->proxies[i]);
1628 static void append_excludes(DBusMessageIter *iter, void *user_data)
1630 struct connman_service *service = user_data;
1633 if (service->excludes == NULL)
1636 for (i = 0; service->excludes[i]; i++)
1637 dbus_message_iter_append_basic(iter,
1638 DBUS_TYPE_STRING, &service->excludes[i]);
1641 static void append_proxy(DBusMessageIter *iter, void *user_data)
1643 struct connman_service *service = user_data;
1644 enum connman_service_proxy_method proxy;
1645 const char *pac = NULL;
1646 const char *method = proxymethod2string(
1647 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1651 if (is_connected(service) == FALSE)
1654 proxy = connman_service_get_proxy_method(service);
1657 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1659 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1661 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1662 connman_dbus_dict_append_array(iter, "Servers",
1663 DBUS_TYPE_STRING, append_proxies,
1666 connman_dbus_dict_append_array(iter, "Excludes",
1667 DBUS_TYPE_STRING, append_excludes,
1670 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1671 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1672 if (service->ipconfig_ipv4 != NULL)
1673 pac = __connman_ipconfig_get_proxy_autoconfig(
1674 service->ipconfig_ipv4);
1675 else if (service->ipconfig_ipv6 != NULL)
1676 pac = __connman_ipconfig_get_proxy_autoconfig(
1677 service->ipconfig_ipv6);
1679 if (service->pac == NULL && pac == NULL)
1682 if (service->pac != NULL)
1685 connman_dbus_dict_append_basic(iter, "URL",
1686 DBUS_TYPE_STRING, &pac);
1690 method = proxymethod2string(proxy);
1693 connman_dbus_dict_append_basic(iter, "Method",
1694 DBUS_TYPE_STRING, &method);
1697 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1699 struct connman_service *service = user_data;
1702 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1705 switch (service->proxy_config) {
1706 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1708 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1710 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1711 if (service->proxies != NULL)
1712 connman_dbus_dict_append_array(iter, "Servers",
1714 append_proxies, service);
1716 if (service->excludes != NULL)
1717 connman_dbus_dict_append_array(iter, "Excludes",
1719 append_excludes, service);
1721 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1722 if (service->pac != NULL)
1723 connman_dbus_dict_append_basic(iter, "URL",
1724 DBUS_TYPE_STRING, &service->pac);
1728 method = proxymethod2string(service->proxy_config);
1730 connman_dbus_dict_append_basic(iter, "Method",
1731 DBUS_TYPE_STRING, &method);
1734 static void append_provider(DBusMessageIter *iter, void *user_data)
1736 struct connman_service *service = user_data;
1738 DBG("%p %p", service, service->provider);
1740 if (is_connected(service) == FALSE)
1743 if (service->provider != NULL)
1744 __connman_provider_append_properties(service->provider, iter);
1748 static void settings_changed(struct connman_service *service,
1749 struct connman_ipconfig *ipconfig)
1751 connman_dbus_property_changed_dict(service->path,
1752 CONNMAN_SERVICE_INTERFACE, "IPv4",
1753 append_ipv4, service);
1755 connman_dbus_property_changed_dict(service->path,
1756 CONNMAN_SERVICE_INTERFACE, "IPv6",
1757 append_ipv6, service);
1759 __connman_notifier_ipconfig_changed(service, ipconfig);
1762 static void ipv4_configuration_changed(struct connman_service *service)
1764 connman_dbus_property_changed_dict(service->path,
1765 CONNMAN_SERVICE_INTERFACE,
1766 "IPv4.Configuration",
1771 static void ipv6_configuration_changed(struct connman_service *service)
1773 connman_dbus_property_changed_dict(service->path,
1774 CONNMAN_SERVICE_INTERFACE,
1775 "IPv6.Configuration",
1780 static void dns_changed(struct connman_service *service)
1782 connman_dbus_property_changed_array(service->path,
1783 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1784 DBUS_TYPE_STRING, append_dns, service);
1787 static void dns_configuration_changed(struct connman_service *service)
1789 connman_dbus_property_changed_array(service->path,
1790 CONNMAN_SERVICE_INTERFACE,
1791 "Nameservers.Configuration",
1792 DBUS_TYPE_STRING, append_dnsconfig, service);
1794 dns_changed(service);
1797 static void domain_changed(struct connman_service *service)
1799 connman_dbus_property_changed_array(service->path,
1800 CONNMAN_SERVICE_INTERFACE, "Domains",
1801 DBUS_TYPE_STRING, append_domain, service);
1804 static void domain_configuration_changed(struct connman_service *service)
1806 connman_dbus_property_changed_array(service->path,
1807 CONNMAN_SERVICE_INTERFACE,
1808 "Domains.Configuration",
1809 DBUS_TYPE_STRING, append_domainconfig, service);
1812 static void proxy_changed(struct connman_service *service)
1814 connman_dbus_property_changed_dict(service->path,
1815 CONNMAN_SERVICE_INTERFACE, "Proxy",
1816 append_proxy, service);
1819 static void proxy_configuration_changed(struct connman_service *service)
1821 connman_dbus_property_changed_dict(service->path,
1822 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1823 append_proxyconfig, service);
1825 proxy_changed(service);
1828 static void timeservers_configuration_changed(struct connman_service *service)
1830 connman_dbus_property_changed_array(service->path,
1831 CONNMAN_SERVICE_INTERFACE,
1832 "Timeservers.Configuration",
1834 append_tsconfig, service);
1837 static void link_changed(struct connman_service *service)
1839 connman_dbus_property_changed_dict(service->path,
1840 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1841 append_ethernet, service);
1844 static void stats_append_counters(DBusMessageIter *dict,
1845 struct connman_stats_data *stats,
1846 struct connman_stats_data *counters,
1847 connman_bool_t append_all)
1849 if (counters->rx_packets != stats->rx_packets || append_all) {
1850 counters->rx_packets = stats->rx_packets;
1851 connman_dbus_dict_append_basic(dict, "RX.Packets",
1852 DBUS_TYPE_UINT32, &stats->rx_packets);
1855 if (counters->tx_packets != stats->tx_packets || append_all) {
1856 counters->tx_packets = stats->tx_packets;
1857 connman_dbus_dict_append_basic(dict, "TX.Packets",
1858 DBUS_TYPE_UINT32, &stats->tx_packets);
1861 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1862 counters->rx_bytes = stats->rx_bytes;
1863 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1864 DBUS_TYPE_UINT32, &stats->rx_bytes);
1867 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1868 counters->tx_bytes = stats->tx_bytes;
1869 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1870 DBUS_TYPE_UINT32, &stats->tx_bytes);
1873 if (counters->rx_errors != stats->rx_errors || append_all) {
1874 counters->rx_errors = stats->rx_errors;
1875 connman_dbus_dict_append_basic(dict, "RX.Errors",
1876 DBUS_TYPE_UINT32, &stats->rx_errors);
1879 if (counters->tx_errors != stats->tx_errors || append_all) {
1880 counters->tx_errors = stats->tx_errors;
1881 connman_dbus_dict_append_basic(dict, "TX.Errors",
1882 DBUS_TYPE_UINT32, &stats->tx_errors);
1885 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1886 counters->rx_dropped = stats->rx_dropped;
1887 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1888 DBUS_TYPE_UINT32, &stats->rx_dropped);
1891 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1892 counters->tx_dropped = stats->tx_dropped;
1893 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1894 DBUS_TYPE_UINT32, &stats->tx_dropped);
1897 if (counters->time != stats->time || append_all) {
1898 counters->time = stats->time;
1899 connman_dbus_dict_append_basic(dict, "Time",
1900 DBUS_TYPE_UINT32, &stats->time);
1904 static void stats_append(struct connman_service *service,
1905 const char *counter,
1906 struct connman_stats_counter *counters,
1907 connman_bool_t append_all)
1909 DBusMessageIter array, dict;
1912 DBG("service %p counter %s", service, counter);
1914 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1918 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1919 &service->path, DBUS_TYPE_INVALID);
1921 dbus_message_iter_init_append(msg, &array);
1924 connman_dbus_dict_open(&array, &dict);
1926 stats_append_counters(&dict, &service->stats.data,
1927 &counters->stats.data, append_all);
1929 connman_dbus_dict_close(&array, &dict);
1931 /* roaming counter */
1932 connman_dbus_dict_open(&array, &dict);
1934 stats_append_counters(&dict, &service->stats_roaming.data,
1935 &counters->stats_roaming.data, append_all);
1937 connman_dbus_dict_close(&array, &dict);
1939 __connman_counter_send_usage(counter, msg);
1942 static void stats_update(struct connman_service *service,
1943 unsigned int rx_packets, unsigned int tx_packets,
1944 unsigned int rx_bytes, unsigned int tx_bytes,
1945 unsigned int rx_errors, unsigned int tx_errors,
1946 unsigned int rx_dropped, unsigned int tx_dropped)
1948 struct connman_stats *stats = stats_get(service);
1949 struct connman_stats_data *data_last = &stats->data_last;
1950 struct connman_stats_data *data = &stats->data;
1951 unsigned int seconds;
1953 DBG("service %p", service);
1955 if (stats->valid == TRUE) {
1957 rx_packets - data_last->rx_packets;
1959 tx_packets - data_last->tx_packets;
1961 rx_bytes - data_last->rx_bytes;
1963 tx_bytes - data_last->tx_bytes;
1965 rx_errors - data_last->rx_errors;
1967 tx_errors - data_last->tx_errors;
1969 rx_dropped - data_last->rx_dropped;
1971 tx_dropped - data_last->tx_dropped;
1973 stats->valid = TRUE;
1976 data_last->rx_packets = rx_packets;
1977 data_last->tx_packets = tx_packets;
1978 data_last->rx_bytes = rx_bytes;
1979 data_last->tx_bytes = tx_bytes;
1980 data_last->rx_errors = rx_errors;
1981 data_last->tx_errors = tx_errors;
1982 data_last->rx_dropped = rx_dropped;
1983 data_last->tx_dropped = tx_dropped;
1985 seconds = g_timer_elapsed(stats->timer, NULL);
1986 stats->data.time = stats->data_last.time + seconds;
1989 void __connman_service_notify(struct connman_service *service,
1990 unsigned int rx_packets, unsigned int tx_packets,
1991 unsigned int rx_bytes, unsigned int tx_bytes,
1992 unsigned int rx_errors, unsigned int tx_errors,
1993 unsigned int rx_dropped, unsigned int tx_dropped)
1995 GHashTableIter iter;
1996 gpointer key, value;
1997 const char *counter;
1998 struct connman_stats_counter *counters;
1999 struct connman_stats_data *data;
2002 if (service == NULL)
2005 if (is_connected(service) == FALSE)
2008 stats_update(service,
2009 rx_packets, tx_packets,
2011 rx_errors, tx_errors,
2012 rx_dropped, tx_dropped);
2014 data = &stats_get(service)->data;
2015 err = __connman_stats_update(service, service->roaming, data);
2017 connman_error("Failed to store statistics for %s",
2018 service->identifier);
2020 g_hash_table_iter_init(&iter, service->counter_table);
2021 while (g_hash_table_iter_next(&iter, &key, &value)) {
2025 stats_append(service, counter, counters, counters->append_all);
2026 counters->append_all = FALSE;
2030 int __connman_service_counter_register(const char *counter)
2032 struct connman_service *service;
2033 GSequenceIter *iter;
2034 struct connman_stats_counter *counters;
2036 DBG("counter %s", counter);
2038 counter_list = g_slist_append(counter_list, (gpointer)counter);
2040 iter = g_sequence_get_begin_iter(service_list);
2042 while (g_sequence_iter_is_end(iter) == FALSE) {
2043 service = g_sequence_get(iter);
2045 counters = g_try_new0(struct connman_stats_counter, 1);
2046 if (counters == NULL)
2049 counters->append_all = TRUE;
2051 g_hash_table_replace(service->counter_table, (gpointer)counter,
2054 iter = g_sequence_iter_next(iter);
2060 void __connman_service_counter_unregister(const char *counter)
2062 struct connman_service *service;
2063 GSequenceIter *iter;
2065 DBG("counter %s", counter);
2067 iter = g_sequence_get_begin_iter(service_list);
2069 while (g_sequence_iter_is_end(iter) == FALSE) {
2070 service = g_sequence_get(iter);
2072 g_hash_table_remove(service->counter_table, counter);
2074 iter = g_sequence_iter_next(iter);
2077 counter_list = g_slist_remove(counter_list, counter);
2080 GSequence *__connman_service_get_list(struct connman_session *session,
2081 service_match_cb service_match,
2082 create_service_entry_cb create_service_entry,
2083 GDestroyNotify destroy_service_entry)
2086 GSequenceIter *iter;
2087 struct connman_service *service;
2088 struct service_entry *entry;
2090 list = g_sequence_new(destroy_service_entry);
2094 iter = g_sequence_get_begin_iter(service_list);
2096 while (g_sequence_iter_is_end(iter) == FALSE) {
2097 service = g_sequence_get(iter);
2099 if (service_match(session, service) == TRUE) {
2100 entry = create_service_entry(service, service->name,
2105 g_sequence_append(list, entry);
2108 iter = g_sequence_iter_next(iter);
2114 void __connman_service_session_inc(struct connman_service *service)
2116 DBG("service %p ref count %d", service,
2117 service->session_usage_count + 1);
2119 __sync_fetch_and_add(&service->session_usage_count, 1);
2122 connman_bool_t __connman_service_session_dec(struct connman_service *service)
2124 DBG("service %p ref count %d", service,
2125 service->session_usage_count - 1);
2127 if (__sync_fetch_and_sub(&service->session_usage_count, 1) != 1)
2133 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
2134 struct connman_service *service)
2139 str = __connman_service_type2string(service->type);
2141 connman_dbus_dict_append_basic(dict, "Type",
2142 DBUS_TYPE_STRING, &str);
2144 connman_dbus_dict_append_array(dict, "Security",
2145 DBUS_TYPE_STRING, append_security, service);
2147 str = state2string(service->state);
2149 connman_dbus_dict_append_basic(dict, "State",
2150 DBUS_TYPE_STRING, &str);
2152 str = error2string(service->error);
2154 connman_dbus_dict_append_basic(dict, "Error",
2155 DBUS_TYPE_STRING, &str);
2157 if (service->strength > 0)
2158 connman_dbus_dict_append_basic(dict, "Strength",
2159 DBUS_TYPE_BYTE, &service->strength);
2161 connman_dbus_dict_append_basic(dict, "Favorite",
2162 DBUS_TYPE_BOOLEAN, &service->favorite);
2164 connman_dbus_dict_append_basic(dict, "Immutable",
2165 DBUS_TYPE_BOOLEAN, &service->immutable);
2167 if (service->favorite == TRUE)
2168 connman_dbus_dict_append_basic(dict, "AutoConnect",
2169 DBUS_TYPE_BOOLEAN, &service->autoconnect);
2171 connman_dbus_dict_append_basic(dict, "AutoConnect",
2172 DBUS_TYPE_BOOLEAN, &service->favorite);
2174 if (service->name != NULL)
2175 connman_dbus_dict_append_basic(dict, "Name",
2176 DBUS_TYPE_STRING, &service->name);
2178 switch (service->type) {
2179 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2180 case CONNMAN_SERVICE_TYPE_SYSTEM:
2181 case CONNMAN_SERVICE_TYPE_GPS:
2182 case CONNMAN_SERVICE_TYPE_VPN:
2183 case CONNMAN_SERVICE_TYPE_GADGET:
2185 case CONNMAN_SERVICE_TYPE_CELLULAR:
2186 connman_dbus_dict_append_basic(dict, "Roaming",
2187 DBUS_TYPE_BOOLEAN, &service->roaming);
2189 connman_dbus_dict_append_dict(dict, "Ethernet",
2190 append_ethernet, service);
2192 case CONNMAN_SERVICE_TYPE_WIFI:
2193 case CONNMAN_SERVICE_TYPE_ETHERNET:
2194 case CONNMAN_SERVICE_TYPE_WIMAX:
2195 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2196 connman_dbus_dict_append_dict(dict, "Ethernet",
2197 append_ethernet, service);
2201 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
2203 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
2204 append_ipv4config, service);
2206 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
2208 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
2209 append_ipv6config, service);
2211 connman_dbus_dict_append_array(dict, "Nameservers",
2212 DBUS_TYPE_STRING, append_dns, service);
2214 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
2215 DBUS_TYPE_STRING, append_dnsconfig, service);
2217 if (service->state == CONNMAN_SERVICE_STATE_READY ||
2218 service->state == CONNMAN_SERVICE_STATE_ONLINE)
2219 list = __connman_timeserver_get_all(service);
2223 connman_dbus_dict_append_array(dict, "Timeservers",
2224 DBUS_TYPE_STRING, append_ts, list);
2226 g_slist_free_full(list, g_free);
2228 connman_dbus_dict_append_array(dict, "Timeservers.Configuration",
2229 DBUS_TYPE_STRING, append_tsconfig, service);
2231 connman_dbus_dict_append_array(dict, "Domains",
2232 DBUS_TYPE_STRING, append_domain, service);
2234 connman_dbus_dict_append_array(dict, "Domains.Configuration",
2235 DBUS_TYPE_STRING, append_domainconfig, service);
2237 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
2239 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
2240 append_proxyconfig, service);
2242 connman_dbus_dict_append_dict(dict, "Provider",
2243 append_provider, service);
2246 static void append_struct_service(DBusMessageIter *iter,
2247 connman_dbus_append_cb_t function,
2248 struct connman_service *service)
2250 DBusMessageIter entry, dict;
2252 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
2254 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2257 connman_dbus_dict_open(&entry, &dict);
2258 if (function != NULL)
2259 function(&dict, service);
2260 connman_dbus_dict_close(&entry, &dict);
2262 dbus_message_iter_close_container(iter, &entry);
2265 static void append_dict_properties(DBusMessageIter *dict, void *user_data)
2267 struct connman_service *service = user_data;
2269 append_properties(dict, TRUE, service);
2272 static void append_struct(gpointer value, gpointer user_data)
2274 struct connman_service *service = value;
2275 DBusMessageIter *iter = user_data;
2277 if (service->path == NULL)
2280 append_struct_service(iter, append_dict_properties, service);
2283 void __connman_service_list_struct(DBusMessageIter *iter)
2285 g_sequence_foreach(service_list, append_struct, iter);
2288 connman_bool_t __connman_service_is_hidden(struct connman_service *service)
2290 return service->hidden;
2294 __connman_service_is_split_routing(struct connman_service *service)
2296 return service->do_split_routing;
2299 int __connman_service_get_index(struct connman_service *service)
2301 if (service == NULL)
2304 if (service->network != NULL)
2305 return connman_network_get_index(service->network);
2306 else if (service->provider != NULL)
2307 return connman_provider_get_index(service->provider);
2312 void __connman_service_set_hidden(struct connman_service *service)
2314 if (service == NULL || service->hidden == TRUE)
2317 service->hidden_service = TRUE;
2320 void __connman_service_set_domainname(struct connman_service *service,
2321 const char *domainname)
2323 if (service == NULL || service->hidden == TRUE)
2326 g_free(service->domainname);
2327 service->domainname = g_strdup(domainname);
2329 domain_changed(service);
2332 const char *connman_service_get_domainname(struct connman_service *service)
2334 if (service == NULL)
2337 if (service->domains != NULL)
2338 return service->domains[0];
2340 return service->domainname;
2343 char **connman_service_get_nameservers(struct connman_service *service)
2345 if (service == NULL)
2348 if (service->nameservers_config != NULL)
2349 return g_strdupv(service->nameservers_config);
2350 else if (service->nameservers != NULL ||
2351 service->nameservers_auto != NULL) {
2352 int len = 0, len_auto = 0, i;
2355 if (service->nameservers != NULL)
2356 len = g_strv_length(service->nameservers);
2357 if (service->nameservers_auto != NULL)
2358 len_auto = g_strv_length(service->nameservers_auto);
2360 nameservers = g_try_new0(char *, len + len_auto + 1);
2361 if (nameservers == NULL)
2364 for (i = 0; i < len; i++)
2365 nameservers[i] = g_strdup(service->nameservers[i]);
2367 for (i = 0; i < len_auto; i++)
2368 nameservers[i + len] =
2369 g_strdup(service->nameservers_auto[i]);
2377 char **connman_service_get_timeservers_config(struct connman_service *service)
2379 if (service == NULL)
2382 return service->timeservers_config;
2385 char **connman_service_get_timeservers(struct connman_service *service)
2387 if (service == NULL)
2390 if (service->timeservers != NULL)
2391 return service->timeservers;
2396 void connman_service_set_proxy_method(struct connman_service *service,
2397 enum connman_service_proxy_method method)
2399 if (service == NULL || service->hidden == TRUE)
2402 service->proxy = method;
2404 proxy_changed(service);
2406 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
2407 __connman_notifier_proxy_changed(service);
2410 enum connman_service_proxy_method connman_service_get_proxy_method(
2411 struct connman_service *service)
2413 if (service == NULL)
2414 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2416 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
2417 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
2418 service->pac == NULL)
2419 return service->proxy;
2421 return service->proxy_config;
2424 return service->proxy;
2427 char **connman_service_get_proxy_servers(struct connman_service *service)
2429 return g_strdupv(service->proxies);
2432 char **connman_service_get_proxy_excludes(struct connman_service *service)
2434 return g_strdupv(service->excludes);
2437 const char *connman_service_get_proxy_url(struct connman_service *service)
2439 if (service == NULL)
2442 return service->pac;
2445 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
2448 if (service == NULL || service->hidden == TRUE)
2451 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
2453 if (service->ipconfig_ipv4) {
2454 if (__connman_ipconfig_set_proxy_autoconfig(
2455 service->ipconfig_ipv4, url) < 0)
2457 } else if (service->ipconfig_ipv6) {
2458 if (__connman_ipconfig_set_proxy_autoconfig(
2459 service->ipconfig_ipv6, url) < 0)
2464 proxy_changed(service);
2466 __connman_notifier_proxy_changed(service);
2469 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
2471 if (service == NULL)
2474 if (service->ipconfig_ipv4)
2475 return __connman_ipconfig_get_proxy_autoconfig(
2476 service->ipconfig_ipv4);
2477 else if (service->ipconfig_ipv6)
2478 return __connman_ipconfig_get_proxy_autoconfig(
2479 service->ipconfig_ipv6);
2483 int __connman_service_timeserver_append(struct connman_service *service,
2484 const char *timeserver)
2488 DBG("service %p timeserver %s", service, timeserver);
2490 if (timeserver == NULL)
2493 if (service->timeservers != NULL) {
2496 for (i = 0; service->timeservers[i] != NULL; i++)
2497 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2500 len = g_strv_length(service->timeservers);
2501 service->timeservers = g_try_renew(char *, service->timeservers,
2505 service->timeservers = g_try_new0(char *, len + 2);
2508 if (service->timeservers == NULL)
2511 service->timeservers[len] = g_strdup(timeserver);
2512 service->timeservers[len + 1] = NULL;
2517 int __connman_service_timeserver_remove(struct connman_service *service,
2518 const char *timeserver)
2521 int len, i, j, found = 0;
2523 DBG("service %p timeserver %s", service, timeserver);
2525 if (timeserver == NULL)
2528 if (service->timeservers == NULL)
2531 for (i = 0; service->timeservers != NULL &&
2532 service->timeservers[i] != NULL; i++)
2533 if (g_strcmp0(service->timeservers[i], timeserver) == 0) {
2541 len = g_strv_length(service->timeservers);
2544 g_strfreev(service->timeservers);
2545 service->timeservers = NULL;
2550 servers = g_try_new0(char *, len);
2551 if (servers == NULL)
2554 for (i = 0, j = 0; i < len; i++) {
2555 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2556 servers[j] = g_strdup(service->timeservers[i]);
2557 if (servers[j] == NULL)
2562 servers[len - 1] = NULL;
2564 g_strfreev(service->timeservers);
2565 service->timeservers = servers;
2570 void __connman_service_timeserver_changed(struct connman_service *service,
2573 if (service == NULL)
2576 connman_dbus_property_changed_array(service->path,
2577 CONNMAN_SERVICE_INTERFACE, "Timeservers",
2578 DBUS_TYPE_STRING, append_ts, ts_list);
2581 void __connman_service_set_pac(struct connman_service *service,
2584 if (service->hidden == TRUE)
2586 g_free(service->pac);
2587 service->pac = g_strdup(pac);
2589 proxy_changed(service);
2592 void __connman_service_set_identity(struct connman_service *service,
2593 const char *identity)
2595 if (service->immutable || service->hidden == TRUE)
2598 g_free(service->identity);
2599 service->identity = g_strdup(identity);
2601 if (service->network != NULL)
2602 connman_network_set_string(service->network,
2607 void __connman_service_set_agent_identity(struct connman_service *service,
2608 const char *agent_identity)
2610 if (service->hidden == TRUE)
2612 g_free(service->agent_identity);
2613 service->agent_identity = g_strdup(agent_identity);
2615 if (service->network != NULL)
2616 connman_network_set_string(service->network,
2617 "WiFi.AgentIdentity",
2618 service->agent_identity);
2621 static int check_passphrase(struct connman_service *service,
2622 enum connman_service_security security,
2623 const char *passphrase)
2628 if (passphrase == NULL) {
2630 * This will prevent __connman_service_set_passphrase() to
2631 * wipe the passphrase out in case of -ENOKEY error for a
2632 * favorite service. */
2633 if (service->favorite == TRUE)
2639 length = strlen(passphrase);
2642 case CONNMAN_SERVICE_SECURITY_PSK:
2643 case CONNMAN_SERVICE_SECURITY_WPA:
2644 case CONNMAN_SERVICE_SECURITY_RSN:
2645 /* A raw key is always 64 bytes length,
2646 * its content is in hex representation.
2647 * A PSK key must be between [8..63].
2650 for (i = 0; i < 64; i++)
2651 if (!isxdigit((unsigned char)
2654 } else if (length < 8 || length > 63)
2657 case CONNMAN_SERVICE_SECURITY_WEP:
2658 /* length of WEP key is 10 or 26
2659 * length of WEP passphrase is 5 or 13
2661 if (length == 10 || length == 26) {
2662 for (i = 0; i < length; i++)
2663 if (!isxdigit((unsigned char)
2666 } else if (length != 5 && length != 13)
2669 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2670 case CONNMAN_SERVICE_SECURITY_NONE:
2671 case CONNMAN_SERVICE_SECURITY_8021X:
2678 int __connman_service_set_passphrase(struct connman_service *service,
2679 const char *passphrase)
2683 if (service->immutable == TRUE || service->hidden == TRUE)
2686 err = check_passphrase(service, service->security, passphrase);
2689 g_free(service->passphrase);
2690 service->passphrase = g_strdup(passphrase);
2692 if (service->network != NULL)
2693 connman_network_set_string(service->network,
2695 service->passphrase);
2696 service_save(service);
2702 const char *__connman_service_get_passphrase(struct connman_service *service)
2704 if (service == NULL)
2707 return service->passphrase;
2710 void __connman_service_set_agent_passphrase(struct connman_service *service,
2711 const char *agent_passphrase)
2713 if (service->hidden == TRUE)
2715 g_free(service->agent_passphrase);
2716 service->agent_passphrase = g_strdup(agent_passphrase);
2718 if (service->network != NULL)
2719 connman_network_set_string(service->network,
2720 "WiFi.AgentPassphrase",
2721 service->agent_passphrase);
2724 static DBusMessage *get_properties(DBusConnection *conn,
2725 DBusMessage *msg, void *user_data)
2727 struct connman_service *service = user_data;
2729 DBusMessageIter array, dict;
2731 DBG("service %p", service);
2733 reply = dbus_message_new_method_return(msg);
2737 dbus_message_iter_init_append(reply, &array);
2739 connman_dbus_dict_open(&array, &dict);
2740 append_properties(&dict, FALSE, service);
2741 connman_dbus_dict_close(&array, &dict);
2746 static int update_proxy_configuration(struct connman_service *service,
2747 DBusMessageIter *array)
2749 DBusMessageIter dict;
2750 enum connman_service_proxy_method method;
2751 GString *servers_str = NULL;
2752 GString *excludes_str = NULL;
2753 const char *url = NULL;
2755 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2757 dbus_message_iter_recurse(array, &dict);
2759 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2760 DBusMessageIter entry, variant;
2764 dbus_message_iter_recurse(&dict, &entry);
2766 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2769 dbus_message_iter_get_basic(&entry, &key);
2770 dbus_message_iter_next(&entry);
2772 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2775 dbus_message_iter_recurse(&entry, &variant);
2777 type = dbus_message_iter_get_arg_type(&variant);
2779 if (g_str_equal(key, "Method") == TRUE) {
2782 if (type != DBUS_TYPE_STRING)
2785 dbus_message_iter_get_basic(&variant, &val);
2786 method = string2proxymethod(val);
2787 } else if (g_str_equal(key, "URL") == TRUE) {
2788 if (type != DBUS_TYPE_STRING)
2791 dbus_message_iter_get_basic(&variant, &url);
2792 } else if (g_str_equal(key, "Servers") == TRUE) {
2793 DBusMessageIter str_array;
2795 if (type != DBUS_TYPE_ARRAY)
2798 servers_str = g_string_new(NULL);
2799 if (servers_str == NULL)
2802 dbus_message_iter_recurse(&variant, &str_array);
2804 while (dbus_message_iter_get_arg_type(&str_array) ==
2808 dbus_message_iter_get_basic(&str_array, &val);
2810 if (servers_str->len > 0)
2811 g_string_append_printf(servers_str,
2814 g_string_append(servers_str, val);
2816 dbus_message_iter_next(&str_array);
2818 } else if (g_str_equal(key, "Excludes") == TRUE) {
2819 DBusMessageIter str_array;
2821 if (type != DBUS_TYPE_ARRAY)
2824 excludes_str = g_string_new(NULL);
2825 if (excludes_str == NULL)
2828 dbus_message_iter_recurse(&variant, &str_array);
2830 while (dbus_message_iter_get_arg_type(&str_array) ==
2834 dbus_message_iter_get_basic(&str_array, &val);
2836 if (excludes_str->len > 0)
2837 g_string_append_printf(excludes_str,
2840 g_string_append(excludes_str, val);
2842 dbus_message_iter_next(&str_array);
2846 dbus_message_iter_next(&dict);
2850 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2852 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2853 if (servers_str == NULL && service->proxies == NULL)
2856 if (servers_str != NULL) {
2857 g_strfreev(service->proxies);
2859 if (servers_str->len > 0)
2860 service->proxies = g_strsplit_set(
2861 servers_str->str, " ", 0);
2863 service->proxies = NULL;
2866 if (excludes_str != NULL) {
2867 g_strfreev(service->excludes);
2869 if (excludes_str->len > 0)
2870 service->excludes = g_strsplit_set(
2871 excludes_str->str, " ", 0);
2873 service->excludes = NULL;
2876 if (service->proxies == NULL)
2877 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2880 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2881 g_free(service->pac);
2883 if (url != NULL && strlen(url) > 0)
2884 service->pac = g_strdup(url);
2886 service->pac = NULL;
2888 /* if we are connected:
2889 - if service->pac == NULL
2890 - if __connman_ipconfig_get_proxy_autoconfig(
2891 service->ipconfig) == NULL
2892 --> We should start WPAD */
2895 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2899 if (servers_str != NULL)
2900 g_string_free(servers_str, TRUE);
2902 if (excludes_str != NULL)
2903 g_string_free(excludes_str, TRUE);
2905 service->proxy_config = method;
2910 if (servers_str != NULL)
2911 g_string_free(servers_str, TRUE);
2913 if (excludes_str != NULL)
2914 g_string_free(excludes_str, TRUE);
2919 static int set_ipconfig(struct connman_service *service,
2920 struct connman_ipconfig *ipconfig,
2921 DBusMessageIter *array,
2922 enum connman_service_state state,
2923 enum connman_service_state *new_state)
2925 enum connman_ipconfig_method old_method;
2926 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2927 enum connman_ipconfig_type type;
2930 if (ipconfig == NULL)
2933 old_method = __connman_ipconfig_get_method(ipconfig);
2935 if (is_connecting_state(service, state) ||
2936 is_connected_state(service, state))
2937 __connman_network_clear_ipconfig(service->network, ipconfig);
2939 err = __connman_ipconfig_set_config(ipconfig, array);
2940 method = __connman_ipconfig_get_method(ipconfig);
2941 type = __connman_ipconfig_get_config_type(ipconfig);
2943 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2944 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2945 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2946 *new_state = service->state_ipv4;
2947 __connman_ipconfig_enable(ipconfig);
2948 __connman_service_auto_connect();
2951 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2952 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2953 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2954 *new_state = service->state_ipv6;
2955 __connman_ipconfig_enable(ipconfig);
2956 __connman_service_auto_connect();
2960 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2961 type, method, state2string(*new_state));
2966 static DBusMessage *set_property(DBusConnection *conn,
2967 DBusMessage *msg, void *user_data)
2969 struct connman_service *service = user_data;
2970 DBusMessageIter iter, value;
2974 DBG("service %p", service);
2976 if (dbus_message_iter_init(msg, &iter) == FALSE)
2977 return __connman_error_invalid_arguments(msg);
2979 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
2980 return __connman_error_invalid_arguments(msg);
2982 dbus_message_iter_get_basic(&iter, &name);
2983 dbus_message_iter_next(&iter);
2985 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
2986 return __connman_error_invalid_arguments(msg);
2988 dbus_message_iter_recurse(&iter, &value);
2990 type = dbus_message_iter_get_arg_type(&value);
2992 if (g_str_equal(name, "AutoConnect") == TRUE) {
2993 connman_bool_t autoconnect;
2995 if (type != DBUS_TYPE_BOOLEAN)
2996 return __connman_error_invalid_arguments(msg);
2998 if (service->favorite == FALSE)
2999 return __connman_error_invalid_service(msg);
3001 dbus_message_iter_get_basic(&value, &autoconnect);
3003 if (service->autoconnect == autoconnect)
3004 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3006 service->autoconnect = autoconnect;
3008 autoconnect_changed(service);
3010 service_save(service);
3011 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
3012 DBusMessageIter entry;
3017 if (type != DBUS_TYPE_ARRAY)
3018 return __connman_error_invalid_arguments(msg);
3020 str = g_string_new(NULL);
3022 return __connman_error_invalid_arguments(msg);
3024 index = connman_network_get_index(service->network);
3025 gw = __connman_ipconfig_get_gateway_from_index(index,
3026 CONNMAN_IPCONFIG_TYPE_ALL);
3028 if (gw && strlen(gw))
3029 __connman_service_nameserver_del_routes(service,
3030 CONNMAN_IPCONFIG_TYPE_ALL);
3032 dbus_message_iter_recurse(&value, &entry);
3034 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3036 dbus_message_iter_get_basic(&entry, &val);
3037 dbus_message_iter_next(&entry);
3038 if (connman_inet_check_ipaddress(val) > 0) {
3040 g_string_append_printf(str, " %s", val);
3042 g_string_append(str, val);
3046 remove_nameservers(service, NULL, service->nameservers_config);
3047 g_strfreev(service->nameservers_config);
3050 service->nameservers_config =
3051 g_strsplit_set(str->str, " ", 0);
3053 service->nameservers_config = NULL;
3056 g_string_free(str, TRUE);
3058 if (gw && strlen(gw))
3059 __connman_service_nameserver_add_routes(service, gw);
3061 update_nameservers(service);
3062 dns_configuration_changed(service);
3064 service_save(service);
3065 } else if (g_str_equal(name, "Timeservers.Configuration") == TRUE) {
3066 DBusMessageIter entry;
3067 GSList *list = NULL;
3070 if (type != DBUS_TYPE_ARRAY)
3071 return __connman_error_invalid_arguments(msg);
3073 dbus_message_iter_recurse(&value, &entry);
3075 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3079 dbus_message_iter_get_basic(&entry, &val);
3081 new_head = __connman_timeserver_add_list(list, val);
3082 if (list != new_head) {
3087 dbus_message_iter_next(&entry);
3090 g_strfreev(service->timeservers_config);
3091 service->timeservers_config = NULL;
3094 service->timeservers_config = g_new0(char *, count+1);
3096 while (list != NULL) {
3098 service->timeservers_config[count] = list->data;
3099 list = g_slist_delete_link(list, list);
3103 service_save(service);
3104 timeservers_configuration_changed(service);
3106 __connman_timeserver_sync(service);
3107 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
3108 DBusMessageIter entry;
3111 if (type != DBUS_TYPE_ARRAY)
3112 return __connman_error_invalid_arguments(msg);
3114 str = g_string_new(NULL);
3116 return __connman_error_invalid_arguments(msg);
3118 dbus_message_iter_recurse(&value, &entry);
3120 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3122 dbus_message_iter_get_basic(&entry, &val);
3123 dbus_message_iter_next(&entry);
3125 g_string_append_printf(str, " %s", val);
3127 g_string_append(str, val);
3130 remove_searchdomains(service, NULL, service->domains);
3131 g_strfreev(service->domains);
3134 service->domains = g_strsplit_set(str->str, " ", 0);
3136 service->domains = NULL;
3138 g_string_free(str, TRUE);
3140 update_nameservers(service);
3141 domain_configuration_changed(service);
3143 service_save(service);
3144 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
3147 if (type != DBUS_TYPE_ARRAY)
3148 return __connman_error_invalid_arguments(msg);
3150 err = update_proxy_configuration(service, &value);
3153 return __connman_error_failed(msg, -err);
3155 proxy_configuration_changed(service);
3157 __connman_notifier_proxy_changed(service);
3159 service_save(service);
3160 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
3161 g_str_equal(name, "IPv6.Configuration")) {
3163 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
3164 enum connman_service_state state =
3165 CONNMAN_SERVICE_STATE_UNKNOWN;
3170 if (service->ipconfig_ipv4 == NULL &&
3171 service->ipconfig_ipv6 == NULL)
3172 return __connman_error_invalid_property(msg);
3174 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
3175 ipv4 = service->ipconfig_ipv4;
3176 err = set_ipconfig(service, ipv4, &value,
3177 service->state_ipv4, &state);
3179 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
3180 ipv6 = service->ipconfig_ipv6;
3181 err = set_ipconfig(service, ipv6, &value,
3182 service->state_ipv6, &state);
3186 if (is_connected_state(service, state) ||
3187 is_connecting_state(service, state))
3188 __connman_network_set_ipconfig(service->network,
3190 return __connman_error_failed(msg, -err);
3194 ipv4_configuration_changed(service);
3196 ipv6_configuration_changed(service);
3198 if (is_connecting(service) || is_connected(service))
3199 __connman_network_set_ipconfig(service->network,
3202 service_save(service);
3204 return __connman_error_invalid_property(msg);
3206 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3209 static void set_error(struct connman_service *service,
3210 enum connman_service_error error)
3214 if (service->error == error)
3217 service->error = error;
3219 if (service->path == NULL)
3222 str = error2string(service->error);
3227 connman_dbus_property_changed_basic(service->path,
3228 CONNMAN_SERVICE_INTERFACE, "Error",
3229 DBUS_TYPE_STRING, &str);
3232 static void set_idle(struct connman_service *service)
3234 service->state = service->state_ipv4 = service->state_ipv6 =
3235 CONNMAN_SERVICE_STATE_IDLE;
3236 set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
3237 state_changed(service);
3240 static DBusMessage *clear_property(DBusConnection *conn,
3241 DBusMessage *msg, void *user_data)
3243 struct connman_service *service = user_data;
3246 DBG("service %p", service);
3248 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
3251 if (g_str_equal(name, "Error") == TRUE) {
3254 g_get_current_time(&service->modified);
3255 service_save(service);
3257 return __connman_error_invalid_property(msg);
3259 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3262 static connman_bool_t is_ipconfig_usable(struct connman_service *service)
3264 if (__connman_ipconfig_is_usable(service->ipconfig_ipv4) == FALSE &&
3265 __connman_ipconfig_is_usable(service->ipconfig_ipv6)
3272 static connman_bool_t is_ignore(struct connman_service *service)
3274 if (service->autoconnect == FALSE)
3277 if (service->roaming == TRUE)
3280 if (service->ignore == TRUE)
3283 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
3286 if (is_ipconfig_usable(service) == FALSE)
3292 struct preferred_tech_data {
3293 GSequence *preferred_list;
3294 enum connman_service_type type;
3297 static void preferred_tech_add_by_type(gpointer data, gpointer user_data)
3299 struct connman_service *service = data;
3300 struct preferred_tech_data *tech_data = user_data;
3302 if (service->type == tech_data->type) {
3303 g_sequence_append(tech_data->preferred_list, service);
3305 DBG("type %d service %p %s", tech_data->type, service,
3310 static GSequence* preferred_tech_list_get(GSequence *list)
3312 unsigned int *tech_array;
3313 struct preferred_tech_data tech_data;
3316 tech_array = connman_setting_get_uint_list("PreferredTechnologies");
3317 if (tech_array == NULL)
3320 tech_data.preferred_list = g_sequence_new(NULL);
3322 for (i = 0; tech_array[i] != 0; i += 1) {
3323 tech_data.type = tech_array[i];
3324 g_sequence_foreach(service_list, preferred_tech_add_by_type,
3328 return tech_data.preferred_list;
3331 static connman_bool_t auto_connect_service(GSequenceIter* iter,
3332 connman_bool_t preferred)
3334 struct connman_service *service = NULL;
3336 while (g_sequence_iter_is_end(iter) == FALSE) {
3337 service = g_sequence_get(iter);
3339 if (service->pending != NULL)
3342 if (is_connecting(service) == TRUE)
3345 if (service->favorite == FALSE) {
3346 if (preferred == TRUE)
3351 if (is_connected(service) == TRUE) {
3352 if (preferred == TRUE && service->state !=
3353 CONNMAN_SERVICE_STATE_ONLINE)
3358 if (is_ignore(service) == FALSE && service->state ==
3359 CONNMAN_SERVICE_STATE_IDLE)
3365 iter = g_sequence_iter_next(iter);
3368 if (service != NULL) {
3370 DBG("service %p %s %s", service, service->name,
3371 (preferred == TRUE)? "preferred": "auto");
3373 service->userconnect = FALSE;
3374 __connman_service_connect(service);
3380 static gboolean run_auto_connect(gpointer data)
3382 GSequenceIter *iter = NULL;
3383 GSequence *preferred_tech;
3385 autoconnect_timeout = 0;
3389 preferred_tech = preferred_tech_list_get(service_list);
3390 if (preferred_tech != NULL)
3391 iter = g_sequence_get_begin_iter(preferred_tech);
3393 if (iter == NULL || auto_connect_service(iter, TRUE) == FALSE)
3394 iter = g_sequence_get_begin_iter(service_list);
3397 auto_connect_service(iter, FALSE);
3399 if (preferred_tech != NULL)
3400 g_sequence_free(preferred_tech);
3405 void __connman_service_auto_connect(void)
3409 if (__connman_session_mode() == TRUE) {
3410 DBG("Session mode enabled: auto connect disabled");
3414 if (autoconnect_timeout != 0)
3417 autoconnect_timeout = g_timeout_add_seconds(0, run_auto_connect, NULL);
3420 static void remove_timeout(struct connman_service *service)
3422 if (service->timeout > 0) {
3423 g_source_remove(service->timeout);
3424 service->timeout = 0;
3428 void __connman_service_reply_dbus_pending(DBusMessage *pending, int error)
3430 if (pending != NULL) {
3434 reply = __connman_error_failed(pending, error);
3436 g_dbus_send_message(connection, reply);
3438 const char *sender, *path;
3440 sender = dbus_message_get_interface(pending);
3441 path = dbus_message_get_path(pending);
3443 DBG("sender %s path %s", sender, path);
3445 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
3446 g_dbus_send_reply(connection, pending,
3447 DBUS_TYPE_OBJECT_PATH, &path,
3450 g_dbus_send_reply(connection, pending,
3454 dbus_message_unref(pending);
3458 static void reply_pending(struct connman_service *service, int error)
3460 remove_timeout(service);
3462 if (service->pending != NULL) {
3463 __connman_service_reply_dbus_pending(service->pending, error);
3464 service->pending = NULL;
3468 static void check_pending_msg(struct connman_service *service)
3470 if (service->pending == NULL)
3473 DBG("service %p pending msg %p already exists", service,
3475 dbus_message_unref(service->pending);
3478 void __connman_service_set_hidden_data(struct connman_service *service,
3481 DBusMessage *pending = user_data;
3483 DBG("service %p pending %p", service, pending);
3485 if (pending == NULL)
3488 check_pending_msg(service);
3490 service->pending = pending;
3493 void __connman_service_return_error(struct connman_service *service,
3494 int error, gpointer user_data)
3496 DBG("service %p error %d user_data %p", service, error, user_data);
3498 __connman_service_set_hidden_data(service, user_data);
3500 reply_pending(service, error);
3503 static gboolean connect_timeout(gpointer user_data)
3505 struct connman_service *service = user_data;
3506 connman_bool_t autoconnect = FALSE;
3508 DBG("service %p", service);
3510 service->timeout = 0;
3512 if (service->network != NULL)
3513 __connman_network_disconnect(service->network);
3514 else if (service->provider != NULL)
3515 __connman_provider_disconnect(service->provider);
3517 __connman_ipconfig_disable(service->ipconfig_ipv4);
3518 __connman_ipconfig_disable(service->ipconfig_ipv6);
3520 __connman_stats_service_unregister(service);
3522 if (service->pending != NULL) {
3525 reply = __connman_error_operation_timeout(service->pending);
3527 g_dbus_send_message(connection, reply);
3529 dbus_message_unref(service->pending);
3530 service->pending = NULL;
3534 __connman_service_ipconfig_indicate_state(service,
3535 CONNMAN_SERVICE_STATE_FAILURE,
3536 CONNMAN_IPCONFIG_TYPE_IPV4);
3537 __connman_service_ipconfig_indicate_state(service,
3538 CONNMAN_SERVICE_STATE_FAILURE,
3539 CONNMAN_IPCONFIG_TYPE_IPV6);
3541 if (autoconnect == TRUE && service->userconnect == FALSE)
3542 __connman_service_auto_connect();
3547 static void set_reconnect_state(struct connman_service *service,
3548 connman_bool_t reconnect)
3550 struct connman_device *device;
3552 if (service->network == NULL)
3555 device = connman_network_get_device(service->network);
3559 __connman_device_set_reconnect(device, reconnect);
3562 static connman_bool_t get_reconnect_state(struct connman_service *service)
3564 struct connman_device *device;
3566 if (service->network == NULL)
3569 device = connman_network_get_device(service->network);
3573 return __connman_device_get_reconnect(device);
3576 static DBusMessage *connect_service(DBusConnection *conn,
3577 DBusMessage *msg, void *user_data)
3579 struct connman_service *service = user_data;
3580 GSequenceIter *iter;
3583 DBG("service %p", service);
3585 if (service->pending != NULL)
3586 return __connman_error_in_progress(msg);
3588 iter = g_sequence_get_begin_iter(service_list);
3590 while (g_sequence_iter_is_end(iter) == FALSE) {
3591 struct connman_service *temp = g_sequence_get(iter);
3593 if (service->type == temp->type && is_connecting(temp) == TRUE)
3594 return __connman_error_in_progress(msg);
3596 iter = g_sequence_iter_next(iter);
3599 service->ignore = FALSE;
3601 service->userconnect = TRUE;
3603 service->pending = dbus_message_ref(msg);
3605 set_reconnect_state(service, FALSE);
3607 err = __connman_service_connect(service);
3609 if (service->pending == NULL)
3612 if (err != -EINPROGRESS) {
3613 dbus_message_unref(service->pending);
3614 service->pending = NULL;
3616 return __connman_error_failed(msg, -err);
3622 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3625 static DBusMessage *disconnect_service(DBusConnection *conn,
3626 DBusMessage *msg, void *user_data)
3628 struct connman_service *service = user_data;
3631 DBG("service %p", service);
3633 reply_pending(service, ECONNABORTED);
3635 service->ignore = TRUE;
3637 set_reconnect_state(service, FALSE);
3639 err = __connman_service_disconnect(service);
3641 if (err != -EINPROGRESS)
3642 return __connman_error_failed(msg, -err);
3647 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3650 gboolean __connman_service_remove(struct connman_service *service)
3652 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
3655 if (service->immutable == TRUE || service->hidden == TRUE)
3658 if (service->favorite == FALSE && service->state !=
3659 CONNMAN_SERVICE_STATE_FAILURE)
3662 set_reconnect_state(service, FALSE);
3664 __connman_service_disconnect(service);
3666 g_free(service->passphrase);
3667 service->passphrase = NULL;
3669 g_free(service->agent_passphrase);
3670 service->agent_passphrase = NULL;
3672 g_free(service->identity);
3673 service->identity = NULL;
3675 g_free(service->agent_identity);
3676 service->agent_identity = NULL;
3678 g_free(service->eap);
3679 service->eap = NULL;
3683 __connman_service_set_favorite(service, FALSE);
3685 service_save(service);
3690 static DBusMessage *remove_service(DBusConnection *conn,
3691 DBusMessage *msg, void *user_data)
3693 struct connman_service *service = user_data;
3695 DBG("service %p", service);
3697 if (__connman_service_remove(service) == FALSE)
3698 return __connman_error_not_supported(msg);
3700 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3703 static gboolean check_suitable_state(enum connman_service_state a,
3704 enum connman_service_state b)
3707 * Special check so that "ready" service can be moved before
3710 if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
3711 b == CONNMAN_SERVICE_STATE_READY) ||
3712 (b == CONNMAN_SERVICE_STATE_ONLINE &&
3713 a == CONNMAN_SERVICE_STATE_READY))
3719 static void downgrade_state(struct connman_service *service)
3721 if (service == NULL)
3724 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
3725 service->state_ipv6);
3727 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
3728 __connman_service_ipconfig_indicate_state(service,
3729 CONNMAN_SERVICE_STATE_READY,
3730 CONNMAN_IPCONFIG_TYPE_IPV4);
3732 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
3733 __connman_service_ipconfig_indicate_state(service,
3734 CONNMAN_SERVICE_STATE_READY,
3735 CONNMAN_IPCONFIG_TYPE_IPV6);
3738 static void apply_relevant_default_downgrade(struct connman_service *service)
3740 struct connman_service *def_service;
3742 def_service = __connman_service_get_default();
3743 if (def_service == NULL)
3746 if (def_service == service &&
3747 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3748 def_service->state = CONNMAN_SERVICE_STATE_READY;
3751 static void switch_default_service(struct connman_service *default_service,
3752 struct connman_service *downgrade_service)
3754 GSequenceIter *src, *dst;
3756 apply_relevant_default_downgrade(default_service);
3757 src = g_hash_table_lookup(service_hash, downgrade_service->identifier);
3758 dst = g_hash_table_lookup(service_hash, default_service->identifier);
3759 g_sequence_move(src, dst);
3760 downgrade_state(downgrade_service);
3763 static DBusMessage *move_service(DBusConnection *conn,
3764 DBusMessage *msg, void *user_data,
3767 struct connman_service *service = user_data;
3768 struct connman_service *target;
3770 enum connman_ipconfig_method target4, target6;
3771 enum connman_ipconfig_method service4, service6;
3773 DBG("service %p", service);
3775 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3778 if (service->favorite == FALSE)
3779 return __connman_error_not_supported(msg);
3781 target = find_service(path);
3782 if (target == NULL || target->favorite == FALSE || target == service)
3783 return __connman_error_invalid_service(msg);
3785 if (target->type == CONNMAN_SERVICE_TYPE_VPN) {
3787 * We only allow VPN route splitting if there are
3788 * routes defined for a given VPN.
3790 if (__connman_provider_check_routes(target->provider)
3792 connman_info("Cannot move service. "
3793 "No routes defined for provider %s",
3794 __connman_provider_get_ident(target->provider));
3795 return __connman_error_invalid_service(msg);
3798 target->do_split_routing = TRUE;
3800 target->do_split_routing = FALSE;
3802 service->do_split_routing = FALSE;
3804 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3805 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3806 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3807 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3809 DBG("target %s method %d/%d state %d/%d split %d", target->identifier,
3810 target4, target6, target->state_ipv4, target->state_ipv6,
3811 target->do_split_routing);
3813 DBG("service %s method %d/%d state %d/%d", service->identifier,
3815 service->state_ipv4, service->state_ipv6);
3818 * If method is OFF, then we do not need to check the corresponding
3821 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3822 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3823 if (check_suitable_state(target->state_ipv6,
3824 service->state_ipv6) == FALSE)
3825 return __connman_error_invalid_service(msg);
3829 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3830 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3831 if (check_suitable_state(target->state_ipv4,
3832 service->state_ipv4) == FALSE)
3833 return __connman_error_invalid_service(msg);
3837 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3838 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3839 if (check_suitable_state(target->state_ipv6,
3840 service->state_ipv6) == FALSE)
3841 return __connman_error_invalid_service(msg);
3845 if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3846 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3847 if (check_suitable_state(target->state_ipv4,
3848 service->state_ipv4) == FALSE)
3849 return __connman_error_invalid_service(msg);
3853 g_get_current_time(&service->modified);
3854 service_save(service);
3855 service_save(target);
3858 * If the service which goes down is the default service and is
3859 * online, we downgrade directly its state to ready so:
3860 * the service which goes up, needs to recompute its state which
3861 * is triggered via downgrading it - if relevant - to state ready.
3864 switch_default_service(target, service);
3866 switch_default_service(service, target);
3868 __connman_connection_update_gateway();
3870 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3873 static DBusMessage *move_before(DBusConnection *conn,
3874 DBusMessage *msg, void *user_data)
3876 return move_service(conn, msg, user_data, TRUE);
3879 static DBusMessage *move_after(DBusConnection *conn,
3880 DBusMessage *msg, void *user_data)
3882 return move_service(conn, msg, user_data, FALSE);
3885 static DBusMessage *reset_counters(DBusConnection *conn,
3886 DBusMessage *msg, void *user_data)
3888 struct connman_service *service = user_data;
3890 reset_stats(service);
3892 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3895 static struct _services_notify {
3901 static void service_append_added_foreach(gpointer data, gpointer user_data)
3903 struct connman_service *service = data;
3904 DBusMessageIter *iter = user_data;
3906 if (service == NULL || service->path == NULL) {
3907 DBG("service %p or path is NULL", service);
3911 if (g_hash_table_lookup(services_notify->add, service->path) != NULL) {
3912 DBG("new %s", service->path);
3914 append_struct(service, iter);
3915 g_hash_table_remove(services_notify->add, service->path);
3917 DBG("changed %s", service->path);
3919 append_struct_service(iter, NULL, service);
3923 static void service_append_ordered(DBusMessageIter *iter, void *user_data)
3925 if (service_list != NULL)
3926 g_sequence_foreach(service_list,
3927 service_append_added_foreach, iter);
3930 static void append_removed(gpointer key, gpointer value, gpointer user_data)
3932 char *objpath = key;
3933 DBusMessageIter *iter = user_data;
3935 DBG("removed %s", objpath);
3936 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &objpath);
3939 static gboolean service_send_changed(gpointer data)
3941 DBusMessage *signal;
3942 DBusMessageIter iter, array;
3946 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
3947 CONNMAN_MANAGER_INTERFACE, "ServicesChanged");
3951 __connman_dbus_append_objpath_dict_array(signal,
3952 service_append_ordered, NULL);
3954 dbus_message_iter_init_append(signal, &iter);
3955 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
3956 DBUS_TYPE_OBJECT_PATH_AS_STRING, &array);
3958 g_hash_table_foreach(services_notify->remove, append_removed, &array);
3960 dbus_message_iter_close_container(&iter, &array);
3962 dbus_connection_send(connection, signal, NULL);
3963 dbus_message_unref(signal);
3965 g_hash_table_remove_all(services_notify->remove);
3966 g_hash_table_remove_all(services_notify->add);
3968 services_notify->id = 0;
3972 static void service_schedule_changed(void)
3974 if (services_notify->id != 0)
3977 services_notify->id = g_timeout_add(100, service_send_changed, NULL);
3980 static void service_schedule_added(struct connman_service *service)
3982 DBG("service %p", service);
3984 g_hash_table_remove(services_notify->remove, service->path);
3985 g_hash_table_replace(services_notify->add, service->path, service);
3987 service_schedule_changed();
3990 static void service_schedule_removed(struct connman_service *service)
3992 DBG("service %p %s", service, service->path);
3994 if (service == NULL || service->path == NULL) {
3995 DBG("service %p or path is NULL", service);
3999 g_hash_table_remove(services_notify->add, service->path);
4000 g_hash_table_replace(services_notify->remove, g_strdup(service->path),
4003 service_schedule_changed();
4006 static const GDBusMethodTable service_methods[] = {
4007 { GDBUS_DEPRECATED_METHOD("GetProperties",
4008 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
4010 { GDBUS_METHOD("SetProperty",
4011 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
4012 NULL, set_property) },
4013 { GDBUS_METHOD("ClearProperty",
4014 GDBUS_ARGS({ "name", "s" }), NULL,
4016 { GDBUS_ASYNC_METHOD("Connect", NULL, NULL,
4018 { GDBUS_METHOD("Disconnect", NULL, NULL,
4019 disconnect_service) },
4020 { GDBUS_METHOD("Remove", NULL, NULL, remove_service) },
4021 { GDBUS_METHOD("MoveBefore",
4022 GDBUS_ARGS({ "service", "o" }), NULL,
4024 { GDBUS_METHOD("MoveAfter",
4025 GDBUS_ARGS({ "service", "o" }), NULL,
4027 { GDBUS_METHOD("ResetCounters", NULL, NULL, reset_counters) },
4031 static const GDBusSignalTable service_signals[] = {
4032 { GDBUS_SIGNAL("PropertyChanged",
4033 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
4037 static void service_free(gpointer user_data)
4039 struct connman_service *service = user_data;
4040 char *path = service->path;
4042 DBG("service %p", service);
4044 reply_pending(service, ENOENT);
4046 g_hash_table_remove(service_hash, service->identifier);
4048 __connman_notifier_service_remove(service);
4049 service_schedule_removed(service);
4051 stats_stop(service);
4053 service->path = NULL;
4056 __connman_connection_update_gateway();
4058 g_dbus_unregister_interface(connection, path,
4059 CONNMAN_SERVICE_INTERFACE);
4063 g_hash_table_destroy(service->counter_table);
4065 if (service->network != NULL) {
4066 __connman_network_disconnect(service->network);
4067 connman_network_unref(service->network);
4068 service->network = NULL;
4071 if (service->provider != NULL)
4072 connman_provider_unref(service->provider);
4074 if (service->ipconfig_ipv4 != NULL) {
4075 __connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
4076 __connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
4077 __connman_ipconfig_unref(service->ipconfig_ipv4);
4078 service->ipconfig_ipv4 = NULL;
4081 if (service->ipconfig_ipv6 != NULL) {
4082 __connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
4083 __connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
4084 __connman_ipconfig_unref(service->ipconfig_ipv6);
4085 service->ipconfig_ipv6 = NULL;
4088 g_strfreev(service->timeservers);
4089 g_strfreev(service->timeservers_config);
4090 g_strfreev(service->nameservers);
4091 g_strfreev(service->nameservers_config);
4092 g_strfreev(service->nameservers_auto);
4093 g_strfreev(service->domains);
4094 g_strfreev(service->proxies);
4095 g_strfreev(service->excludes);
4097 g_free(service->domainname);
4098 g_free(service->pac);
4099 g_free(service->name);
4100 g_free(service->passphrase);
4101 g_free(service->agent_passphrase);
4102 g_free(service->identifier);
4103 g_free(service->eap);
4104 g_free(service->identity);
4105 g_free(service->agent_identity);
4106 g_free(service->ca_cert_file);
4107 g_free(service->client_cert_file);
4108 g_free(service->private_key_file);
4109 g_free(service->private_key_passphrase);
4110 g_free(service->phase2);
4111 g_free(service->config_file);
4112 g_free(service->config_entry);
4114 if (service->stats.timer != NULL)
4115 g_timer_destroy(service->stats.timer);
4116 if (service->stats_roaming.timer != NULL)
4117 g_timer_destroy(service->stats_roaming.timer);
4122 static void stats_init(struct connman_service *service)
4125 service->stats.valid = FALSE;
4126 service->stats.enabled = FALSE;
4127 service->stats.timer = g_timer_new();
4130 service->stats_roaming.valid = FALSE;
4131 service->stats_roaming.enabled = FALSE;
4132 service->stats_roaming.timer = g_timer_new();
4135 static void service_initialize(struct connman_service *service)
4137 DBG("service %p", service);
4139 service->refcount = 1;
4140 service->session_usage_count = 0;
4142 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4144 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
4145 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
4147 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
4148 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
4149 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
4151 service->favorite = FALSE;
4152 service->immutable = FALSE;
4153 service->hidden = FALSE;
4155 service->ignore = FALSE;
4157 service->userconnect = FALSE;
4161 stats_init(service);
4163 service->provider = NULL;
4165 service->wps = FALSE;
4169 * connman_service_create:
4171 * Allocate a new service.
4173 * Returns: a newly-allocated #connman_service structure
4175 struct connman_service *connman_service_create(void)
4178 struct connman_stats_counter *counters;
4179 const char *counter;
4181 struct connman_service *service;
4183 service = g_try_new0(struct connman_service, 1);
4184 if (service == NULL)
4187 DBG("service %p", service);
4189 service->counter_table = g_hash_table_new_full(g_str_hash,
4190 g_str_equal, NULL, g_free);
4192 for (list = counter_list; list; list = list->next) {
4193 counter = list->data;
4195 counters = g_try_new0(struct connman_stats_counter, 1);
4196 if (counters == NULL) {
4197 g_hash_table_destroy(service->counter_table);
4202 counters->append_all = TRUE;
4204 g_hash_table_replace(service->counter_table, (gpointer)counter,
4208 service_initialize(service);
4214 * connman_service_ref:
4215 * @service: service structure
4217 * Increase reference counter of service
4219 struct connman_service *
4220 connman_service_ref_debug(struct connman_service *service,
4221 const char *file, int line, const char *caller)
4223 DBG("%p ref %d by %s:%d:%s()", service, service->refcount + 1,
4224 file, line, caller);
4226 __sync_fetch_and_add(&service->refcount, 1);
4232 * connman_service_unref:
4233 * @service: service structure
4235 * Decrease reference counter of service and release service if no
4238 void connman_service_unref_debug(struct connman_service *service,
4239 const char *file, int line, const char *caller)
4241 GSequenceIter *iter;
4243 DBG("%p ref %d by %s:%d:%s()", service, service->refcount - 1,
4244 file, line, caller);
4246 if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
4249 iter = g_hash_table_lookup(service_hash, service->identifier);
4251 reply_pending(service, ECONNABORTED);
4253 __connman_service_disconnect(service);
4255 g_sequence_remove(iter);
4257 service_free(service);
4261 static gint service_compare(gconstpointer a, gconstpointer b,
4264 struct connman_service *service_a = (void *) a;
4265 struct connman_service *service_b = (void *) b;
4266 enum connman_service_state state_a, state_b;
4268 state_a = service_a->state;
4269 state_b = service_b->state;
4271 if (state_a != state_b) {
4272 gboolean a_connected = is_connected(service_a);
4273 gboolean b_connected = is_connected(service_b);
4275 if (a_connected == TRUE && b_connected == TRUE) {
4276 /* We prefer online over ready state */
4277 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
4280 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
4284 if (a_connected == TRUE)
4286 if (b_connected == TRUE)
4289 if (is_connecting(service_a) == TRUE)
4291 if (is_connecting(service_b) == TRUE)
4295 if (service_a->order > service_b->order)
4298 if (service_a->order < service_b->order)
4301 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
4304 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
4307 if (service_a->type != service_b->type) {
4308 switch (service_a->type) {
4309 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4310 case CONNMAN_SERVICE_TYPE_SYSTEM:
4311 case CONNMAN_SERVICE_TYPE_ETHERNET:
4312 case CONNMAN_SERVICE_TYPE_GPS:
4313 case CONNMAN_SERVICE_TYPE_VPN:
4314 case CONNMAN_SERVICE_TYPE_GADGET:
4316 case CONNMAN_SERVICE_TYPE_WIFI:
4318 case CONNMAN_SERVICE_TYPE_WIMAX:
4319 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4320 case CONNMAN_SERVICE_TYPE_CELLULAR:
4325 return (gint) service_b->strength - (gint) service_a->strength;
4329 * connman_service_get_type:
4330 * @service: service structure
4332 * Get the type of service
4334 enum connman_service_type connman_service_get_type(struct connman_service *service)
4336 if (service == NULL)
4337 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4339 return service->type;
4343 * connman_service_get_interface:
4344 * @service: service structure
4346 * Get network interface of service
4348 char *connman_service_get_interface(struct connman_service *service)
4352 if (service == NULL)
4355 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
4356 if (service->ipconfig_ipv4)
4357 index = __connman_ipconfig_get_index(
4358 service->ipconfig_ipv4);
4359 else if (service->ipconfig_ipv6)
4360 index = __connman_ipconfig_get_index(
4361 service->ipconfig_ipv6);
4365 return connman_inet_ifname(index);
4368 if (service->network == NULL)
4371 index = connman_network_get_index(service->network);
4373 return connman_inet_ifname(index);
4377 * connman_service_get_network:
4378 * @service: service structure
4380 * Get the service network
4382 struct connman_network *
4383 __connman_service_get_network(struct connman_service *service)
4385 if (service == NULL)
4388 return service->network;
4391 struct connman_ipconfig *
4392 __connman_service_get_ip4config(struct connman_service *service)
4394 if (service == NULL)
4397 return service->ipconfig_ipv4;
4400 struct connman_ipconfig *
4401 __connman_service_get_ip6config(struct connman_service *service)
4403 if (service == NULL)
4406 return service->ipconfig_ipv6;
4409 struct connman_ipconfig *
4410 __connman_service_get_ipconfig(struct connman_service *service, int family)
4412 if (family == AF_INET)
4413 return __connman_service_get_ip4config(service);
4414 else if (family == AF_INET6)
4415 return __connman_service_get_ip6config(service);
4421 connman_bool_t __connman_service_is_connected_state(struct connman_service *service,
4422 enum connman_ipconfig_type type)
4424 if (service == NULL)
4428 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
4430 case CONNMAN_IPCONFIG_TYPE_IPV4:
4431 return is_connected_state(service, service->state_ipv4);
4432 case CONNMAN_IPCONFIG_TYPE_IPV6:
4433 return is_connected_state(service, service->state_ipv6);
4438 enum connman_service_security __connman_service_get_security(struct connman_service *service)
4440 if (service == NULL)
4441 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4443 return service->security;
4446 const char *__connman_service_get_phase2(struct connman_service *service)
4448 if (service == NULL)
4451 return service->phase2;
4454 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
4456 if (service == NULL)
4459 return service->wps;
4462 void __connman_service_mark_dirty()
4464 services_dirty = TRUE;
4468 * __connman_service_set_favorite_delayed:
4469 * @service: service structure
4470 * @favorite: favorite value
4471 * @delay_ordering: do not order service sequence
4473 * Change the favorite setting of service
4475 int __connman_service_set_favorite_delayed(struct connman_service *service,
4476 connman_bool_t favorite,
4477 gboolean delay_ordering)
4479 GSequenceIter *iter;
4481 if (service->hidden == TRUE)
4483 iter = g_hash_table_lookup(service_hash, service->identifier);
4487 if (service->favorite == favorite)
4490 service->favorite = favorite;
4492 if (delay_ordering == FALSE)
4493 service->order = __connman_service_get_order(service);
4495 favorite_changed(service);
4497 if (delay_ordering == FALSE) {
4499 if (g_sequence_get_length(service_list) > 1) {
4500 g_sequence_sort_changed(iter, service_compare, NULL);
4501 service_schedule_changed();
4504 __connman_connection_update_gateway();
4511 * __connman_service_set_favorite:
4512 * @service: service structure
4513 * @favorite: favorite value
4515 * Change the favorite setting of service
4517 int __connman_service_set_favorite(struct connman_service *service,
4518 connman_bool_t favorite)
4520 return __connman_service_set_favorite_delayed(service, favorite,
4524 int __connman_service_set_immutable(struct connman_service *service,
4525 connman_bool_t immutable)
4527 if (service->hidden == TRUE)
4529 service->immutable = immutable;
4531 immutable_changed(service);
4536 void __connman_service_set_string(struct connman_service *service,
4537 const char *key, const char *value)
4539 if (service->hidden == TRUE)
4541 if (g_str_equal(key, "EAP") == TRUE) {
4542 g_free(service->eap);
4543 service->eap = g_strdup(value);
4544 } else if (g_str_equal(key, "Identity") == TRUE) {
4545 g_free(service->identity);
4546 service->identity = g_strdup(value);
4547 } else if (g_str_equal(key, "CACertFile") == TRUE) {
4548 g_free(service->ca_cert_file);
4549 service->ca_cert_file = g_strdup(value);
4550 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
4551 g_free(service->client_cert_file);
4552 service->client_cert_file = g_strdup(value);
4553 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
4554 g_free(service->private_key_file);
4555 service->private_key_file = g_strdup(value);
4556 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
4557 g_free(service->private_key_passphrase);
4558 service->private_key_passphrase = g_strdup(value);
4559 } else if (g_str_equal(key, "Phase2") == TRUE) {
4560 g_free(service->phase2);
4561 service->phase2 = g_strdup(value);
4562 } else if (g_str_equal(key, "Passphrase") == TRUE) {
4563 g_free(service->passphrase);
4564 service->passphrase = g_strdup(value);
4568 void __connman_service_set_userconnect(struct connman_service *service,
4569 connman_bool_t userconnect)
4571 if (service != NULL)
4572 service->userconnect = userconnect;
4575 static void service_complete(struct connman_service *service)
4577 reply_pending(service, EIO);
4579 if (service->userconnect == FALSE)
4580 __connman_service_auto_connect();
4582 g_get_current_time(&service->modified);
4583 service_save(service);
4586 static void report_error_cb(struct connman_service *service,
4587 gboolean retry, void *user_data)
4590 __connman_service_connect(service);
4592 /* It is not relevant to stay on Failure state
4593 * when failing is due to wrong user input */
4594 service->state = CONNMAN_SERVICE_STATE_IDLE;
4596 service_complete(service);
4597 __connman_connection_update_gateway();
4601 int __connman_service_add_passphrase(struct connman_service *service,
4602 const gchar *passphrase)
4606 switch (service->security) {
4607 case CONNMAN_SERVICE_SECURITY_WEP:
4608 case CONNMAN_SERVICE_SECURITY_PSK:
4609 err = __connman_service_set_passphrase(service, passphrase);
4611 case CONNMAN_SERVICE_SECURITY_8021X:
4612 __connman_service_set_agent_passphrase(service,
4615 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4616 case CONNMAN_SERVICE_SECURITY_NONE:
4617 case CONNMAN_SERVICE_SECURITY_WPA:
4618 case CONNMAN_SERVICE_SECURITY_RSN:
4619 DBG("service security '%s' (%d) not handled",
4620 security2string(service->security),
4628 static int check_wpspin(struct connman_service *service, const char *wpspin)
4636 length = strlen(wpspin);
4638 /* If 0, it will mean user wants to use PBC method */
4640 connman_network_set_string(service->network,
4641 "WiFi.PinWPS", NULL);
4645 /* A WPS PIN is always 8 chars length,
4646 * its content is in digit representation.
4651 for (i = 0; i < 8; i++)
4652 if (!isdigit((unsigned char) wpspin[i]))
4655 connman_network_set_string(service->network, "WiFi.PinWPS", wpspin);
4660 static void request_input_cb (struct connman_service *service,
4661 connman_bool_t values_received,
4662 const char *name, int name_len,
4663 const char *identity, const char *passphrase,
4664 gboolean wps, const char *wpspin,
4665 const char *error, void *user_data)
4667 struct connman_device *device;
4670 DBG ("RequestInput return, %p", service);
4672 if (error != NULL) {
4673 DBG("error: %s", error);
4675 if (g_strcmp0(error,
4676 "net.connman.Agent.Error.Canceled") == 0) {
4679 if (service->hidden == TRUE)
4680 __connman_service_return_error(service,
4681 ECANCELED, user_data);
4684 if (service->hidden == TRUE)
4685 __connman_service_return_error(service,
4686 ETIMEDOUT, user_data);
4690 if (service->hidden == TRUE && name_len > 0 && name_len <= 32) {
4691 device = connman_network_get_device(service->network);
4692 err = __connman_device_request_hidden_scan(device,
4694 identity, passphrase,
4697 __connman_service_return_error(service, -err,
4701 if (values_received == FALSE || service->hidden == TRUE) {
4706 if (wps == TRUE && service->network != NULL) {
4707 err = check_wpspin(service, wpspin);
4711 connman_network_set_bool(service->network, "WiFi.UseWPS", wps);
4714 if (identity != NULL)
4715 __connman_service_set_agent_identity(service, identity);
4717 if (passphrase != NULL)
4718 err = __connman_service_add_passphrase(service, passphrase);
4722 /* We forget any previous error. */
4723 set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
4725 __connman_service_connect(service);
4727 /* Never cache agent provided credentials */
4728 __connman_service_set_agent_identity(service, NULL);
4729 __connman_service_set_agent_passphrase(service, NULL);
4730 } else if (err == -ENOKEY) {
4731 __connman_service_indicate_error(service,
4732 CONNMAN_SERVICE_ERROR_INVALID_KEY);
4733 __connman_agent_report_error(service,
4734 error2string(service->error),
4735 report_error_cb, NULL);
4737 /* It is not relevant to stay on Failure state
4738 * when failing is due to wrong user input */
4739 service->state = CONNMAN_SERVICE_STATE_IDLE;
4741 if (service->hidden == FALSE) {
4743 * If there was a real error when requesting
4744 * hidden scan, then that error is returned already
4745 * to the user somewhere above so do not try to
4748 __connman_service_return_error(service, -err,
4752 service_complete(service);
4753 __connman_connection_update_gateway();
4757 static void downgrade_connected_services(void)
4759 struct connman_service *up_service;
4760 GSequenceIter *iter;
4762 iter = g_sequence_get_begin_iter(service_list);
4763 while (g_sequence_iter_is_end(iter) == FALSE) {
4764 up_service = g_sequence_get(iter);
4766 if (is_connected(up_service) == FALSE) {
4767 iter = g_sequence_iter_next(iter);
4771 if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE)
4774 downgrade_state(up_service);
4776 iter = g_sequence_iter_next(iter);
4780 static int service_update_preferred_order(struct connman_service *default_service,
4781 struct connman_service *new_service,
4782 enum connman_service_state new_state)
4784 unsigned int *tech_array;
4787 if (default_service == NULL || default_service == new_service ||
4788 default_service->state != new_state )
4791 tech_array = connman_setting_get_uint_list("PreferredTechnologies");
4792 if (tech_array != NULL) {
4794 for (i = 0; tech_array[i] != 0; i += 1) {
4795 if (default_service->type == tech_array[i])
4798 if (new_service->type == tech_array[i]) {
4799 switch_default_service(new_service,
4810 static int service_indicate_state(struct connman_service *service)
4812 enum connman_service_state old_state, new_state;
4813 struct connman_service *def_service;
4815 GSequenceIter *iter;
4817 if (service == NULL)
4820 old_state = service->state;
4821 new_state = combine_state(service->state_ipv4, service->state_ipv6);
4823 DBG("service %p old %s - new %s/%s => %s",
4825 state2string(old_state),
4826 state2string(service->state_ipv4),
4827 state2string(service->state_ipv6),
4828 state2string(new_state));
4830 if (old_state == new_state)
4833 def_service = __connman_service_get_default();
4835 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4836 result = service_update_preferred_order(def_service,
4837 service, new_state);
4838 if (result == -EALREADY)
4840 if (result == -EAGAIN)
4841 __connman_service_auto_connect();
4844 if (old_state == CONNMAN_SERVICE_STATE_ONLINE)
4845 __connman_notifier_leave_online(service->type);
4847 service->state = new_state;
4848 state_changed(service);
4850 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
4851 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
4852 reply_pending(service, ECONNABORTED);
4854 __connman_service_disconnect(service);
4857 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
4858 if (service->new_service == FALSE &&
4859 __connman_stats_service_register(service) == 0) {
4861 * For new services the statistics are updated after
4862 * we have successfully connected.
4864 __connman_stats_get(service, FALSE,
4865 &service->stats.data);
4866 __connman_stats_get(service, TRUE,
4867 &service->stats_roaming.data);
4871 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
4872 connman_bool_t reconnect;
4874 reconnect = get_reconnect_state(service);
4875 if (reconnect == TRUE)
4876 __connman_service_auto_connect();
4879 if (new_state == CONNMAN_SERVICE_STATE_READY) {
4880 enum connman_ipconfig_method method;
4882 if (service->new_service == TRUE &&
4883 __connman_stats_service_register(service) == 0) {
4885 * This is normally done after configuring state
4886 * but for new service do this after we have connected
4889 __connman_stats_get(service, FALSE,
4890 &service->stats.data);
4891 __connman_stats_get(service, TRUE,
4892 &service->stats_roaming.data);
4895 service->new_service = FALSE;
4897 service_update_preferred_order(def_service, service, new_state);
4899 set_reconnect_state(service, TRUE);
4901 __connman_service_set_favorite(service, TRUE);
4903 reply_pending(service, 0);
4905 service->userconnect = FALSE;
4907 g_get_current_time(&service->modified);
4908 service_save(service);
4910 update_nameservers(service);
4911 dns_changed(service);
4912 domain_changed(service);
4914 __connman_notifier_connect(service->type);
4916 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4917 connman_network_get_bool(service->network,
4918 "WiFi.UseWPS") == TRUE) {
4921 pass = connman_network_get_string(service->network,
4924 __connman_service_set_passphrase(service, pass);
4926 connman_network_set_bool(service->network,
4927 "WiFi.UseWPS", FALSE);
4932 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4933 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4934 __connman_ipconfig_disable_ipv6(
4935 service->ipconfig_ipv6);
4937 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4938 def_service = __connman_service_get_default();
4940 if (__connman_notifier_is_connected() == FALSE &&
4941 def_service != NULL &&
4942 def_service->provider != NULL)
4943 __connman_provider_disconnect(def_service->provider);
4947 __connman_wispr_stop(service);
4949 __connman_wpad_stop(service);
4951 update_nameservers(service);
4952 dns_changed(service);
4953 domain_changed(service);
4955 __connman_notifier_disconnect(service->type);
4958 * Previous services which are connected and which states
4959 * are set to online should reset relevantly ipconfig_state
4960 * to ready so wispr/portal will be rerun on those
4962 downgrade_connected_services();
4965 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4966 if (service->userconnect == TRUE &&
4967 __connman_agent_report_error(service,
4968 error2string(service->error),
4969 report_error_cb, NULL) == -EINPROGRESS)
4971 service_complete(service);
4973 set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
4975 iter = g_hash_table_lookup(service_hash, service->identifier);
4976 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
4977 g_sequence_sort_changed(iter, service_compare, NULL);
4978 service_schedule_changed();
4981 __connman_connection_update_gateway();
4983 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4984 __connman_notifier_enter_online(service->type);
4991 int __connman_service_indicate_error(struct connman_service *service,
4992 enum connman_service_error error)
4994 DBG("service %p error %d", service, error);
4996 if (service == NULL)
4999 set_error(service, error);
5001 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
5002 __connman_service_set_passphrase(service, NULL);
5004 __connman_service_ipconfig_indicate_state(service,
5005 CONNMAN_SERVICE_STATE_FAILURE,
5006 CONNMAN_IPCONFIG_TYPE_IPV4);
5007 __connman_service_ipconfig_indicate_state(service,
5008 CONNMAN_SERVICE_STATE_FAILURE,
5009 CONNMAN_IPCONFIG_TYPE_IPV6);
5013 int __connman_service_clear_error(struct connman_service *service)
5015 DBG("service %p", service);
5017 if (service == NULL)
5020 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
5023 service->state_ipv4 = service->state_ipv6 =
5024 CONNMAN_SERVICE_STATE_UNKNOWN;
5025 set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
5027 if (service->favorite == TRUE)
5028 set_reconnect_state(service, TRUE);
5030 __connman_service_ipconfig_indicate_state(service,
5031 CONNMAN_SERVICE_STATE_IDLE,
5032 CONNMAN_IPCONFIG_TYPE_IPV6);
5035 * Toggling the IPv6 state to IDLE could trigger the auto connect
5036 * machinery and consequently the IPv4 state.
5038 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
5039 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
5042 return __connman_service_ipconfig_indicate_state(service,
5043 CONNMAN_SERVICE_STATE_IDLE,
5044 CONNMAN_IPCONFIG_TYPE_IPV4);
5047 int __connman_service_indicate_default(struct connman_service *service)
5049 DBG("service %p", service);
5056 enum connman_service_state __connman_service_ipconfig_get_state(
5057 struct connman_service *service,
5058 enum connman_ipconfig_type type)
5060 if (service == NULL)
5061 return CONNMAN_SERVICE_STATE_UNKNOWN;
5063 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
5064 return service->state_ipv4;
5066 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
5067 return service->state_ipv6;
5069 return CONNMAN_SERVICE_STATE_UNKNOWN;
5072 static void check_proxy_setup(struct connman_service *service)
5075 * We start WPAD if we haven't got a PAC URL from DHCP and
5076 * if our proxy manual configuration is either empty or set
5077 * to AUTO with an empty URL.
5080 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
5083 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
5084 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
5085 service->pac != NULL))
5088 if (__connman_wpad_start(service) < 0) {
5089 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
5090 __connman_notifier_proxy_changed(service);
5097 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
5101 * How many networks are connected at the same time. If more than 1,
5102 * then set the rp_filter setting properly (loose mode routing) so that network
5103 * connectivity works ok. This is only done for IPv4 networks as IPv6
5104 * does not have rp_filter knob.
5106 static int connected_networks_count;
5107 static int original_rp_filter;
5109 static void service_rp_filter(struct connman_service *service,
5112 enum connman_ipconfig_method method;
5114 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
5117 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
5118 case CONNMAN_IPCONFIG_METHOD_OFF:
5119 case CONNMAN_IPCONFIG_METHOD_AUTO:
5121 case CONNMAN_IPCONFIG_METHOD_FIXED:
5122 case CONNMAN_IPCONFIG_METHOD_MANUAL:
5123 case CONNMAN_IPCONFIG_METHOD_DHCP:
5127 if (connected == TRUE) {
5128 if (connected_networks_count == 1) {
5130 filter_value = __connman_ipconfig_set_rp_filter();
5131 if (filter_value < 0)
5134 original_rp_filter = filter_value;
5136 connected_networks_count++;
5139 if (connected_networks_count == 2)
5140 __connman_ipconfig_unset_rp_filter(original_rp_filter);
5142 connected_networks_count--;
5143 if (connected_networks_count < 0)
5144 connected_networks_count = 0;
5147 DBG("%s %s ipconfig %p method %d count %d filter %d",
5148 connected ? "connected" : "disconnected", service->identifier,
5149 service->ipconfig_ipv4, method,
5150 connected_networks_count, original_rp_filter);
5153 static gboolean redo_wispr(gpointer user_data)
5155 struct connman_service *service = user_data;
5159 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV6);
5164 int __connman_service_online_check_failed(struct connman_service *service,
5165 enum connman_ipconfig_type type)
5167 DBG("service %p type %d count %d", service, type,
5168 service->online_check_count);
5170 /* currently we only retry IPv6 stuff */
5171 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 ||
5172 service->online_check_count != 1) {
5173 connman_warn("Online check failed for %p %s", service,
5178 service->online_check_count = 0;
5181 * We set the timeout to 1 sec so that we have a chance to get
5182 * necessary IPv6 router advertisement messages that might have
5185 g_timeout_add_seconds(1, redo_wispr, service);
5190 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
5191 enum connman_service_state new_state,
5192 enum connman_ipconfig_type type)
5194 struct connman_ipconfig *ipconfig = NULL;
5195 enum connman_service_state old_state;
5198 if (service == NULL)
5201 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
5202 old_state = service->state_ipv4;
5203 ipconfig = service->ipconfig_ipv4;
5204 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
5205 old_state = service->state_ipv6;
5206 ipconfig = service->ipconfig_ipv6;
5209 if (ipconfig == NULL)
5213 if (old_state == new_state)
5216 DBG("service %p (%s) state %d (%s) type %d (%s)",
5217 service, service ? service->identifier : NULL,
5218 new_state, state2string(new_state),
5219 type, __connman_ipconfig_type2string(type));
5221 switch (new_state) {
5222 case CONNMAN_SERVICE_STATE_UNKNOWN:
5223 case CONNMAN_SERVICE_STATE_IDLE:
5224 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
5227 case CONNMAN_SERVICE_STATE_ASSOCIATION:
5229 case CONNMAN_SERVICE_STATE_CONFIGURATION:
5230 __connman_ipconfig_enable(ipconfig);
5232 case CONNMAN_SERVICE_STATE_READY:
5233 update_nameservers(service);
5235 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
5236 check_proxy_setup(service);
5237 service_rp_filter(service, TRUE);
5239 service->online_check_count = 1;
5240 __connman_wispr_start(service, type);
5243 case CONNMAN_SERVICE_STATE_ONLINE:
5245 case CONNMAN_SERVICE_STATE_DISCONNECT:
5246 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
5249 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
5250 service_rp_filter(service, FALSE);
5253 case CONNMAN_SERVICE_STATE_FAILURE:
5257 /* We keep that state */
5258 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
5259 service->state_ipv4 = new_state;
5260 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
5261 service->state_ipv6 = new_state;
5263 ret = service_indicate_state(service);
5266 * If the ipconfig method is OFF, then we set the state to IDLE
5267 * so that it will not affect the combined state in the future.
5269 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
5270 enum connman_ipconfig_method method;
5271 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
5272 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
5273 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
5274 service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
5275 ret = service_indicate_state(service);
5278 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
5279 enum connman_ipconfig_method method;
5280 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
5281 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
5282 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
5283 service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5284 ret = service_indicate_state(service);
5291 static connman_bool_t prepare_network(struct connman_service *service)
5293 enum connman_network_type type;
5294 unsigned int ssid_len;
5296 type = connman_network_get_type(service->network);
5299 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5300 case CONNMAN_NETWORK_TYPE_VENDOR:
5302 case CONNMAN_NETWORK_TYPE_WIFI:
5303 if (connman_network_get_blob(service->network, "WiFi.SSID",
5307 if (service->passphrase != NULL)
5308 connman_network_set_string(service->network,
5309 "WiFi.Passphrase", service->passphrase);
5311 case CONNMAN_NETWORK_TYPE_ETHERNET:
5312 case CONNMAN_NETWORK_TYPE_WIMAX:
5313 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5314 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5315 case CONNMAN_NETWORK_TYPE_CELLULAR:
5322 static void prepare_8021x(struct connman_service *service)
5324 if (service->eap != NULL)
5325 connman_network_set_string(service->network, "WiFi.EAP",
5328 if (service->identity != NULL)
5329 connman_network_set_string(service->network, "WiFi.Identity",
5332 if (service->ca_cert_file != NULL)
5333 connman_network_set_string(service->network, "WiFi.CACertFile",
5334 service->ca_cert_file);
5336 if (service->client_cert_file != NULL)
5337 connman_network_set_string(service->network,
5338 "WiFi.ClientCertFile",
5339 service->client_cert_file);
5341 if (service->private_key_file != NULL)
5342 connman_network_set_string(service->network,
5343 "WiFi.PrivateKeyFile",
5344 service->private_key_file);
5346 if (service->private_key_passphrase != NULL)
5347 connman_network_set_string(service->network,
5348 "WiFi.PrivateKeyPassphrase",
5349 service->private_key_passphrase);
5351 if (service->phase2 != NULL)
5352 connman_network_set_string(service->network, "WiFi.Phase2",
5356 static int service_connect(struct connman_service *service)
5360 if (service->hidden == TRUE)
5363 switch (service->type) {
5364 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5365 case CONNMAN_SERVICE_TYPE_SYSTEM:
5366 case CONNMAN_SERVICE_TYPE_GPS:
5367 case CONNMAN_SERVICE_TYPE_GADGET:
5369 case CONNMAN_SERVICE_TYPE_ETHERNET:
5370 case CONNMAN_SERVICE_TYPE_WIMAX:
5371 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5372 case CONNMAN_SERVICE_TYPE_CELLULAR:
5373 case CONNMAN_SERVICE_TYPE_VPN:
5375 case CONNMAN_SERVICE_TYPE_WIFI:
5376 switch (service->security) {
5377 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
5378 case CONNMAN_SERVICE_SECURITY_NONE:
5380 case CONNMAN_SERVICE_SECURITY_WEP:
5381 case CONNMAN_SERVICE_SECURITY_PSK:
5382 case CONNMAN_SERVICE_SECURITY_WPA:
5383 case CONNMAN_SERVICE_SECURITY_RSN:
5384 if (service->passphrase == NULL) {
5385 if (service->network == NULL)
5388 if (service->wps == FALSE ||
5389 connman_network_get_bool(
5391 "WiFi.UseWPS") == FALSE)
5393 } else if (service->error ==
5394 CONNMAN_SERVICE_ERROR_INVALID_KEY)
5397 case CONNMAN_SERVICE_SECURITY_8021X:
5398 if (service->eap == NULL)
5402 * never request credentials if using EAP-TLS
5403 * (EAP-TLS networks need to be fully provisioned)
5405 if (g_str_equal(service->eap, "tls") == TRUE)
5409 * Return -ENOKEY if either identity or passphrase is
5410 * missing. Agent provided credentials can be used as
5411 * fallback if needed.
5413 if ((service->identity == NULL &&
5414 service->agent_identity == NULL) ||
5415 (service->passphrase == NULL &&
5416 service->agent_passphrase == NULL))
5424 if (service->network != NULL) {
5425 if (prepare_network(service) == FALSE)
5428 switch (service->security) {
5429 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
5430 case CONNMAN_SERVICE_SECURITY_NONE:
5431 case CONNMAN_SERVICE_SECURITY_WEP:
5432 case CONNMAN_SERVICE_SECURITY_PSK:
5433 case CONNMAN_SERVICE_SECURITY_WPA:
5434 case CONNMAN_SERVICE_SECURITY_RSN:
5436 case CONNMAN_SERVICE_SECURITY_8021X:
5437 prepare_8021x(service);
5441 if (__connman_stats_service_register(service) == 0) {
5442 __connman_stats_get(service, FALSE,
5443 &service->stats.data);
5444 __connman_stats_get(service, TRUE,
5445 &service->stats_roaming.data);
5448 if (service->ipconfig_ipv4)
5449 __connman_ipconfig_enable(service->ipconfig_ipv4);
5450 if (service->ipconfig_ipv6)
5451 __connman_ipconfig_enable(service->ipconfig_ipv6);
5453 err = __connman_network_connect(service->network);
5454 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5455 service->provider != NULL)
5456 err = __connman_provider_connect(service->provider);
5461 if (err != -EINPROGRESS) {
5462 __connman_ipconfig_disable(service->ipconfig_ipv4);
5463 __connman_ipconfig_disable(service->ipconfig_ipv6);
5464 __connman_stats_service_unregister(service);
5472 int __connman_service_connect(struct connman_service *service)
5476 DBG("service %p state %s", service, state2string(service->state));
5478 if (is_connected(service) == TRUE)
5481 if (is_connecting(service) == TRUE)
5484 switch (service->type) {
5485 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5486 case CONNMAN_SERVICE_TYPE_SYSTEM:
5487 case CONNMAN_SERVICE_TYPE_GPS:
5488 case CONNMAN_SERVICE_TYPE_GADGET:
5491 if (is_ipconfig_usable(service) == FALSE)
5494 err = service_connect(service);
5500 if (err == -EINPROGRESS) {
5501 if (service->timeout == 0)
5502 service->timeout = g_timeout_add_seconds(
5503 CONNECT_TIMEOUT, connect_timeout, service);
5505 return -EINPROGRESS;
5508 if (service->network != NULL)
5509 __connman_network_disconnect(service->network);
5510 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5511 service->provider != NULL)
5512 __connman_provider_disconnect(service->provider);
5514 if (service->userconnect == TRUE) {
5515 if (err == -ENOKEY || err == -EPERM) {
5516 DBusMessage *pending = NULL;
5519 * We steal the reply here. The idea is that the
5520 * connecting client will see the connection status
5521 * after the real hidden network is connected or
5522 * connection failed.
5524 if (service->hidden == TRUE) {
5525 pending = service->pending;
5526 service->pending = NULL;
5529 err = __connman_agent_request_passphrase_input(service,
5530 request_input_cb, pending);
5531 if (service->hidden == TRUE && err != -EINPROGRESS)
5532 service->pending = pending;
5536 reply_pending(service, -err);
5542 int __connman_service_disconnect(struct connman_service *service)
5546 DBG("service %p", service);
5548 if (service->network != NULL) {
5549 err = __connman_network_disconnect(service->network);
5550 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5551 service->provider != NULL)
5552 err = __connman_provider_disconnect(service->provider);
5556 if (err < 0 && err != -EINPROGRESS)
5559 __connman_6to4_remove(service->ipconfig_ipv4);
5561 if (service->ipconfig_ipv4)
5562 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
5565 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
5568 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
5569 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
5571 __connman_ipconfig_disable(service->ipconfig_ipv4);
5572 __connman_ipconfig_disable(service->ipconfig_ipv6);
5574 __connman_stats_service_unregister(service);
5579 int __connman_service_disconnect_all(void)
5581 GSequenceIter *iter;
5582 GSList *services = NULL, *list;
5586 iter = g_sequence_get_begin_iter(service_list);
5588 while (g_sequence_iter_is_end(iter) == FALSE) {
5589 struct connman_service *service = g_sequence_get(iter);
5591 services = g_slist_prepend(services, service);
5593 iter = g_sequence_iter_next(iter);
5596 for (list = services; list != NULL; list = list->next) {
5597 struct connman_service *service = list->data;
5599 service->ignore = TRUE;
5601 set_reconnect_state(service, FALSE);
5603 __connman_service_disconnect(service);
5613 * lookup_by_identifier:
5614 * @identifier: service identifier
5616 * Look up a service by identifier (reference count will not be increased)
5618 static struct connman_service *lookup_by_identifier(const char *identifier)
5620 GSequenceIter *iter;
5622 iter = g_hash_table_lookup(service_hash, identifier);
5624 return g_sequence_get(iter);
5629 struct provision_user_data {
5634 static void provision_changed(gpointer value, gpointer user_data)
5636 struct connman_service *service = value;
5637 struct provision_user_data *data = user_data;
5638 const char *path = data->ident;
5641 ret = __connman_config_provision_service_ident(service, path,
5642 service->config_file, service->config_entry);
5647 int __connman_service_provision_changed(const char *ident)
5649 struct provision_user_data data = {
5654 g_sequence_foreach(service_list, provision_changed, (void *)&data);
5657 * Because the provision_changed() might have set some services
5658 * as favorite, we must sort the sequence now.
5660 if (services_dirty == TRUE) {
5661 services_dirty = FALSE;
5663 if (g_sequence_get_length(service_list) > 1) {
5664 g_sequence_sort(service_list, service_compare, NULL);
5665 service_schedule_changed();
5668 __connman_connection_update_gateway();
5674 void __connman_service_set_config(struct connman_service *service,
5675 const char *file_id, const char *entry)
5677 if (service == NULL)
5680 g_free(service->config_file);
5681 service->config_file = g_strdup(file_id);
5683 g_free(service->config_entry);
5684 service->config_entry = g_strdup(entry);
5688 * __connman_service_get:
5689 * @identifier: service identifier
5691 * Look up a service by identifier or create a new one if not found
5693 static struct connman_service *service_get(const char *identifier)
5695 struct connman_service *service;
5696 GSequenceIter *iter;
5698 iter = g_hash_table_lookup(service_hash, identifier);
5700 service = g_sequence_get(iter);
5701 if (service != NULL)
5702 connman_service_ref(service);
5706 service = connman_service_create();
5707 if (service == NULL)
5710 DBG("service %p", service);
5712 service->identifier = g_strdup(identifier);
5714 iter = g_sequence_insert_sorted(service_list, service,
5715 service_compare, NULL);
5717 g_hash_table_insert(service_hash, service->identifier, iter);
5722 static int service_register(struct connman_service *service)
5724 GSequenceIter *iter;
5726 DBG("service %p", service);
5728 if (service->path != NULL)
5731 service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
5732 service->identifier);
5734 DBG("path %s", service->path);
5736 __connman_config_provision_service(service);
5738 service_load(service);
5740 g_dbus_register_interface(connection, service->path,
5741 CONNMAN_SERVICE_INTERFACE,
5742 service_methods, service_signals,
5743 NULL, service, NULL);
5745 iter = g_hash_table_lookup(service_hash, service->identifier);
5746 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
5747 g_sequence_sort_changed(iter, service_compare, NULL);
5748 service_schedule_changed();
5751 __connman_connection_update_gateway();
5756 static void service_up(struct connman_ipconfig *ipconfig)
5758 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5760 DBG("%s up", __connman_ipconfig_get_ifname(ipconfig));
5762 link_changed(service);
5764 service->stats.valid = FALSE;
5765 service->stats_roaming.valid = FALSE;
5768 static void service_down(struct connman_ipconfig *ipconfig)
5770 DBG("%s down", __connman_ipconfig_get_ifname(ipconfig));
5773 static void service_lower_up(struct connman_ipconfig *ipconfig)
5775 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5777 DBG("%s lower up", __connman_ipconfig_get_ifname(ipconfig));
5779 stats_start(service);
5782 static void service_lower_down(struct connman_ipconfig *ipconfig)
5784 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5786 DBG("%s lower down", __connman_ipconfig_get_ifname(ipconfig));
5788 if (is_idle_state(service, service->state_ipv4) == FALSE)
5789 __connman_ipconfig_disable(service->ipconfig_ipv4);
5791 if (is_idle_state(service, service->state_ipv6) == FALSE)
5792 __connman_ipconfig_disable(service->ipconfig_ipv6);
5794 stats_stop(service);
5795 service_save(service);
5798 static void service_ip_bound(struct connman_ipconfig *ipconfig)
5800 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5801 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5802 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5804 DBG("%s ip bound", __connman_ipconfig_get_ifname(ipconfig));
5806 type = __connman_ipconfig_get_config_type(ipconfig);
5807 method = __connman_ipconfig_get_method(ipconfig);
5809 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5812 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5813 method == CONNMAN_IPCONFIG_METHOD_AUTO)
5814 __connman_service_ipconfig_indicate_state(service,
5815 CONNMAN_SERVICE_STATE_READY,
5816 CONNMAN_IPCONFIG_TYPE_IPV6);
5818 settings_changed(service, ipconfig);
5821 static void service_ip_release(struct connman_ipconfig *ipconfig)
5823 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5824 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5825 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5827 DBG("%s ip release", __connman_ipconfig_get_ifname(ipconfig));
5829 type = __connman_ipconfig_get_config_type(ipconfig);
5830 method = __connman_ipconfig_get_method(ipconfig);
5832 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5835 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5836 method == CONNMAN_IPCONFIG_METHOD_OFF)
5837 __connman_service_ipconfig_indicate_state(service,
5838 CONNMAN_SERVICE_STATE_DISCONNECT,
5839 CONNMAN_IPCONFIG_TYPE_IPV6);
5841 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
5842 method == CONNMAN_IPCONFIG_METHOD_OFF)
5843 __connman_service_ipconfig_indicate_state(service,
5844 CONNMAN_SERVICE_STATE_DISCONNECT,
5845 CONNMAN_IPCONFIG_TYPE_IPV4);
5847 settings_changed(service, ipconfig);
5850 static void service_route_changed(struct connman_ipconfig *ipconfig)
5852 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5854 DBG("%s route changed", __connman_ipconfig_get_ifname(ipconfig));
5856 settings_changed(service, ipconfig);
5859 static const struct connman_ipconfig_ops service_ops = {
5861 .down = service_down,
5862 .lower_up = service_lower_up,
5863 .lower_down = service_lower_down,
5864 .ip_bound = service_ip_bound,
5865 .ip_release = service_ip_release,
5866 .route_set = service_route_changed,
5867 .route_unset = service_route_changed,
5870 static void setup_ip4config(struct connman_service *service, int index,
5871 enum connman_ipconfig_method method)
5873 service->ipconfig_ipv4 = __connman_ipconfig_create(index,
5874 CONNMAN_IPCONFIG_TYPE_IPV4);
5875 if (service->ipconfig_ipv4 == NULL)
5878 __connman_ipconfig_set_method(service->ipconfig_ipv4, method);
5880 __connman_ipconfig_set_data(service->ipconfig_ipv4, service);
5882 __connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
5885 static void setup_ip6config(struct connman_service *service, int index)
5887 service->ipconfig_ipv6 = __connman_ipconfig_create(index,
5888 CONNMAN_IPCONFIG_TYPE_IPV6);
5889 if (service->ipconfig_ipv6 == NULL)
5892 __connman_ipconfig_set_data(service->ipconfig_ipv6, service);
5894 __connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
5897 void __connman_service_read_ip4config(struct connman_service *service)
5901 if (service->ipconfig_ipv4 == NULL)
5904 keyfile = connman_storage_load_service(service->identifier);
5905 if (keyfile == NULL)
5908 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5909 service->identifier, "IPv4.");
5911 g_key_file_free(keyfile);
5914 void connman_service_create_ip4config(struct connman_service *service,
5917 DBG("ipv4 %p", service->ipconfig_ipv4);
5919 if (service->ipconfig_ipv4 != NULL)
5922 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5923 __connman_service_read_ip4config(service);
5926 void __connman_service_read_ip6config(struct connman_service *service)
5930 if (service->ipconfig_ipv6 == NULL)
5933 keyfile = connman_storage_load_service(service->identifier);
5934 if (keyfile == NULL)
5937 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5938 service->identifier, "IPv6.");
5940 g_key_file_free(keyfile);
5943 void connman_service_create_ip6config(struct connman_service *service,
5946 DBG("ipv6 %p", service->ipconfig_ipv6);
5948 if (service->ipconfig_ipv6 != NULL)
5951 setup_ip6config(service, index);
5953 __connman_service_read_ip6config(service);
5957 * connman_service_lookup_from_network:
5958 * @network: network structure
5960 * Look up a service by network (reference count will not be increased)
5962 struct connman_service *connman_service_lookup_from_network(struct connman_network *network)
5964 struct connman_service *service;
5965 const char *ident, *group;
5968 DBG("network %p", network);
5970 if (network == NULL)
5973 ident = __connman_network_get_ident(network);
5977 group = connman_network_get_group(network);
5981 name = g_strdup_printf("%s_%s_%s",
5982 __connman_network_get_type(network), ident, group);
5983 service = lookup_by_identifier(name);
5989 struct connman_service *__connman_service_lookup_from_index(int index)
5991 struct connman_service *service;
5992 GSequenceIter *iter;
5994 iter = g_sequence_get_begin_iter(service_list);
5996 while (g_sequence_iter_is_end(iter) == FALSE) {
5997 service = g_sequence_get(iter);
5999 if (__connman_ipconfig_get_index(service->ipconfig_ipv4)
6003 if (__connman_ipconfig_get_index(service->ipconfig_ipv6)
6007 iter = g_sequence_iter_next(iter);
6013 struct connman_service *__connman_service_lookup_from_ident(const char *identifier)
6015 return lookup_by_identifier(identifier);
6018 const char *__connman_service_get_ident(struct connman_service *service)
6020 return service->identifier;
6023 const char *__connman_service_get_path(struct connman_service *service)
6025 return service->path;
6028 unsigned int __connman_service_get_order(struct connman_service *service)
6030 GSequenceIter *iter;
6032 if (service == NULL)
6035 if (service->favorite == FALSE) {
6040 iter = g_hash_table_lookup(service_hash, service->identifier);
6042 if (g_sequence_iter_get_position(iter) == 0)
6044 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
6045 service->do_split_routing == FALSE)
6046 service->order = 10;
6051 DBG("service %p name %s order %d split %d", service, service->name,
6052 service->order, service->do_split_routing);
6055 return service->order;
6058 void __connman_service_update_ordering(void)
6060 GSequenceIter *iter;
6062 iter = g_sequence_get_begin_iter(service_list);
6063 if (iter != NULL && g_sequence_get_length(service_list) > 1)
6064 g_sequence_sort_changed(iter, service_compare, NULL);
6067 static enum connman_service_type convert_network_type(struct connman_network *network)
6069 enum connman_network_type type = connman_network_get_type(network);
6072 case CONNMAN_NETWORK_TYPE_UNKNOWN:
6073 case CONNMAN_NETWORK_TYPE_VENDOR:
6075 case CONNMAN_NETWORK_TYPE_ETHERNET:
6076 return CONNMAN_SERVICE_TYPE_ETHERNET;
6077 case CONNMAN_NETWORK_TYPE_WIFI:
6078 return CONNMAN_SERVICE_TYPE_WIFI;
6079 case CONNMAN_NETWORK_TYPE_WIMAX:
6080 return CONNMAN_SERVICE_TYPE_WIMAX;
6081 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
6082 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
6083 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
6084 case CONNMAN_NETWORK_TYPE_CELLULAR:
6085 return CONNMAN_SERVICE_TYPE_CELLULAR;
6088 return CONNMAN_SERVICE_TYPE_UNKNOWN;
6091 static enum connman_service_security convert_wifi_security(const char *security)
6093 if (security == NULL)
6094 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
6095 else if (g_str_equal(security, "none") == TRUE)
6096 return CONNMAN_SERVICE_SECURITY_NONE;
6097 else if (g_str_equal(security, "wep") == TRUE)
6098 return CONNMAN_SERVICE_SECURITY_WEP;
6099 else if (g_str_equal(security, "psk") == TRUE)
6100 return CONNMAN_SERVICE_SECURITY_PSK;
6101 else if (g_str_equal(security, "ieee8021x") == TRUE)
6102 return CONNMAN_SERVICE_SECURITY_8021X;
6103 else if (g_str_equal(security, "wpa") == TRUE)
6104 return CONNMAN_SERVICE_SECURITY_WPA;
6105 else if (g_str_equal(security, "rsn") == TRUE)
6106 return CONNMAN_SERVICE_SECURITY_RSN;
6108 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
6111 static void update_from_network(struct connman_service *service,
6112 struct connman_network *network)
6114 connman_uint8_t strength = service->strength;
6115 GSequenceIter *iter;
6118 DBG("service %p network %p", service, network);
6120 if (is_connected(service) == TRUE)
6123 if (is_connecting(service) == TRUE)
6126 str = connman_network_get_string(network, "Name");
6128 g_free(service->name);
6129 service->name = g_strdup(str);
6130 service->hidden = FALSE;
6132 g_free(service->name);
6133 service->name = NULL;
6134 service->hidden = TRUE;
6137 service->strength = connman_network_get_strength(network);
6138 service->roaming = connman_network_get_bool(network, "Roaming");
6140 if (service->strength == 0) {
6142 * Filter out 0-values; it's unclear what they mean
6143 * and they cause anomalous sorting of the priority list.
6145 service->strength = strength;
6148 str = connman_network_get_string(network, "WiFi.Security");
6149 service->security = convert_wifi_security(str);
6151 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
6152 service->wps = connman_network_get_bool(network, "WiFi.WPS");
6154 if (service->strength > strength && service->network != NULL) {
6155 connman_network_unref(service->network);
6156 service->network = connman_network_ref(network);
6158 strength_changed(service);
6161 if (service->network == NULL)
6162 service->network = connman_network_ref(network);
6164 iter = g_hash_table_lookup(service_hash, service->identifier);
6165 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
6166 g_sequence_sort_changed(iter, service_compare, NULL);
6167 service_schedule_changed();
6172 * __connman_service_create_from_network:
6173 * @network: network structure
6175 * Look up service by network and if not found, create one
6177 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
6179 struct connman_service *service;
6180 struct connman_device *device;
6181 const char *ident, *group;
6183 unsigned int *auto_connect_types;
6186 DBG("network %p", network);
6188 if (network == NULL)
6191 ident = __connman_network_get_ident(network);
6195 group = connman_network_get_group(network);
6199 name = g_strdup_printf("%s_%s_%s",
6200 __connman_network_get_type(network), ident, group);
6201 service = service_get(name);
6204 if (service == NULL)
6207 if (__connman_network_get_weakness(network) == TRUE)
6210 if (service->path != NULL) {
6211 update_from_network(service, network);
6212 __connman_connection_update_gateway();
6216 service->type = convert_network_type(network);
6218 auto_connect_types = connman_setting_get_uint_list("DefaultAutoConnectTechnologies");
6219 service->autoconnect = FALSE;
6220 for (i = 0; auto_connect_types != NULL &&
6221 auto_connect_types[i] != 0; i++) {
6222 if (service->type == auto_connect_types[i]) {
6223 service->autoconnect = TRUE;
6228 switch (service->type) {
6229 case CONNMAN_SERVICE_TYPE_UNKNOWN:
6230 case CONNMAN_SERVICE_TYPE_SYSTEM:
6231 case CONNMAN_SERVICE_TYPE_WIMAX:
6232 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
6233 case CONNMAN_SERVICE_TYPE_GPS:
6234 case CONNMAN_SERVICE_TYPE_VPN:
6235 case CONNMAN_SERVICE_TYPE_GADGET:
6236 case CONNMAN_SERVICE_TYPE_WIFI:
6237 case CONNMAN_SERVICE_TYPE_CELLULAR:
6239 case CONNMAN_SERVICE_TYPE_ETHERNET:
6240 service->favorite = TRUE;
6244 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
6245 service->state = combine_state(service->state_ipv4, service->state_ipv6);
6247 update_from_network(service, network);
6249 index = connman_network_get_index(network);
6251 if (service->ipconfig_ipv4 == NULL)
6252 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
6254 if (service->ipconfig_ipv6 == NULL)
6255 setup_ip6config(service, index);
6257 service_register(service);
6259 if (service->favorite == TRUE) {
6260 device = connman_network_get_device(service->network);
6261 if (device && connman_device_get_scanning(device) == FALSE)
6262 __connman_service_auto_connect();
6265 __connman_notifier_service_add(service, service->name);
6266 service_schedule_added(service);
6271 void __connman_service_update_from_network(struct connman_network *network)
6273 connman_bool_t need_sort = FALSE;
6274 struct connman_service *service;
6275 connman_uint8_t strength;
6276 connman_bool_t roaming;
6277 GSequenceIter *iter;
6279 connman_bool_t stats_enable;
6281 DBG("network %p", network);
6283 service = connman_service_lookup_from_network(network);
6284 if (service == NULL)
6287 if (service->network == NULL)
6290 name = connman_network_get_string(service->network, "Name");
6291 if (g_strcmp0(service->name, name) != 0) {
6292 g_free(service->name);
6293 service->name = g_strdup(name);
6294 connman_dbus_property_changed_basic(service->path,
6295 CONNMAN_SERVICE_INTERFACE, "Name",
6296 DBUS_TYPE_STRING, &service->name);
6299 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
6300 service->wps = connman_network_get_bool(network, "WiFi.WPS");
6302 strength = connman_network_get_strength(service->network);
6303 if (strength == service->strength)
6306 service->strength = strength;
6309 strength_changed(service);
6312 roaming = connman_network_get_bool(service->network, "Roaming");
6313 if (roaming == service->roaming)
6316 stats_enable = stats_enabled(service);
6317 if (stats_enable == TRUE)
6318 stats_stop(service);
6320 service->roaming = roaming;
6323 if (stats_enable == TRUE)
6324 stats_start(service);
6326 roaming_changed(service);
6329 if (need_sort == TRUE) {
6330 iter = g_hash_table_lookup(service_hash, service->identifier);
6331 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
6332 g_sequence_sort_changed(iter, service_compare, NULL);
6333 service_schedule_changed();
6338 void __connman_service_remove_from_network(struct connman_network *network)
6340 struct connman_service *service;
6342 DBG("network %p", network);
6344 service = connman_service_lookup_from_network(network);
6345 if (service == NULL)
6348 service->ignore = TRUE;
6350 __connman_connection_gateway_remove(service,
6351 CONNMAN_IPCONFIG_TYPE_ALL);
6353 connman_service_unref(service);
6357 * __connman_service_create_from_provider:
6358 * @provider: provider structure
6360 * Look up service by provider and if not found, create one
6362 struct connman_service *
6363 __connman_service_create_from_provider(struct connman_provider *provider)
6365 struct connman_service *service;
6366 const char *ident, *str;
6368 int index = connman_provider_get_index(provider);
6370 DBG("provider %p", provider);
6372 ident = __connman_provider_get_ident(provider);
6376 name = g_strdup_printf("vpn_%s", ident);
6377 service = service_get(name);
6380 if (service == NULL)
6383 service->type = CONNMAN_SERVICE_TYPE_VPN;
6384 service->provider = connman_provider_ref(provider);
6385 service->autoconnect = FALSE;
6386 service->userconnect = TRUE;
6388 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
6389 service->state = combine_state(service->state_ipv4, service->state_ipv6);
6391 str = connman_provider_get_string(provider, "Name");
6393 g_free(service->name);
6394 service->name = g_strdup(str);
6395 service->hidden = FALSE;
6397 g_free(service->name);
6398 service->name = NULL;
6399 service->hidden = TRUE;
6402 service->strength = 0;
6404 if (service->ipconfig_ipv4 == NULL)
6405 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
6407 if (service->ipconfig_ipv6 == NULL)
6408 setup_ip6config(service, index);
6410 service_register(service);
6412 __connman_notifier_service_add(service, service->name);
6413 service_schedule_added(service);
6418 static void remove_unprovisioned_services()
6421 GKeyFile *keyfile, *configkeyfile;
6422 char *file, *section;
6425 services = connman_storage_get_services();
6426 if (services == NULL)
6429 for (;services[i] != NULL; i++) {
6430 file = section = NULL;
6431 keyfile = configkeyfile = NULL;
6433 keyfile = connman_storage_load_service(services[i]);
6434 if (keyfile == NULL)
6437 file = g_key_file_get_string(keyfile, services[i],
6438 "Config.file", NULL);
6442 section = g_key_file_get_string(keyfile, services[i],
6443 "Config.ident", NULL);
6444 if (section == NULL)
6447 configkeyfile = __connman_storage_load_config(file);
6448 if (configkeyfile == NULL) {
6450 * Config file is missing, remove the provisioned
6453 __connman_storage_remove_service(services[i]);
6457 if (g_key_file_has_group(configkeyfile, section) == FALSE)
6459 * Config section is missing, remove the provisioned
6462 __connman_storage_remove_service(services[i]);
6465 if (keyfile != NULL)
6466 g_key_file_free(keyfile);
6468 if (configkeyfile != NULL)
6469 g_key_file_free(configkeyfile);
6475 g_strfreev(services);
6478 int __connman_service_init(void)
6482 connection = connman_dbus_get_connection();
6484 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
6487 service_list = g_sequence_new(service_free);
6489 services_notify = g_new0(struct _services_notify, 1);
6490 services_notify->remove = g_hash_table_new_full(g_str_hash,
6491 g_str_equal, g_free, NULL);
6492 services_notify->add = g_hash_table_new(g_str_hash, g_str_equal);
6494 remove_unprovisioned_services();
6499 void __connman_service_cleanup(void)
6505 if (autoconnect_timeout != 0) {
6506 g_source_remove(autoconnect_timeout);
6507 autoconnect_timeout = 0;
6510 list = service_list;
6511 service_list = NULL;
6512 g_sequence_free(list);
6514 g_hash_table_destroy(service_hash);
6515 service_hash = NULL;
6517 g_slist_free(counter_list);
6518 counter_list = NULL;
6520 if (services_notify->id != 0) {
6521 g_source_remove(services_notify->id);
6522 service_send_changed(NULL);
6523 g_hash_table_destroy(services_notify->remove);
6524 g_hash_table_destroy(services_notify->add);
6526 g_free(services_notify);
6528 dbus_connection_unref(connection);