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;
48 struct connman_stats {
50 connman_bool_t enabled;
51 struct connman_stats_data data_last;
52 struct connman_stats_data data;
56 struct connman_stats_counter {
57 connman_bool_t append_all;
58 struct connman_stats stats;
59 struct connman_stats stats_roaming;
62 struct connman_service {
64 int session_usage_count;
67 enum connman_service_type type;
68 enum connman_service_security security;
69 enum connman_service_state state;
70 enum connman_service_state state_ipv4;
71 enum connman_service_state state_ipv6;
72 enum connman_service_error error;
73 connman_uint8_t strength;
74 connman_bool_t favorite;
75 connman_bool_t immutable;
76 connman_bool_t hidden;
77 connman_bool_t ignore;
78 connman_bool_t autoconnect;
79 connman_bool_t userconnect;
84 char *agent_passphrase;
85 connman_bool_t roaming;
86 struct connman_ipconfig *ipconfig_ipv4;
87 struct connman_ipconfig *ipconfig_ipv6;
88 struct connman_network *network;
89 struct connman_provider *provider;
91 char **nameservers_config;
92 char **nameservers_auto;
96 char **timeservers_config;
97 /* 802.1x settings from the config files */
100 char *agent_identity;
102 char *client_cert_file;
103 char *private_key_file;
104 char *private_key_passphrase;
106 DBusMessage *pending;
108 struct connman_stats stats;
109 struct connman_stats stats_roaming;
110 GHashTable *counter_table;
111 enum connman_service_proxy_method proxy;
112 enum connman_service_proxy_method proxy_config;
117 int online_check_count;
118 connman_bool_t do_split_routing;
119 connman_bool_t new_service;
120 connman_bool_t hidden_service;
125 struct connman_service *service;
128 static void compare_path(gpointer value, gpointer user_data)
130 struct connman_service *service = value;
131 struct find_data *data = user_data;
133 if (data->service != NULL)
136 if (g_strcmp0(service->path, data->path) == 0)
137 data->service = service;
140 static struct connman_service *find_service(const char *path)
142 struct find_data data = { .path = path, .service = NULL };
144 DBG("path %s", path);
146 g_sequence_foreach(service_list, compare_path, &data);
151 const char *__connman_service_type2string(enum connman_service_type type)
154 case CONNMAN_SERVICE_TYPE_UNKNOWN:
156 case CONNMAN_SERVICE_TYPE_SYSTEM:
158 case CONNMAN_SERVICE_TYPE_ETHERNET:
160 case CONNMAN_SERVICE_TYPE_WIFI:
162 case CONNMAN_SERVICE_TYPE_WIMAX:
164 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
166 case CONNMAN_SERVICE_TYPE_CELLULAR:
168 case CONNMAN_SERVICE_TYPE_GPS:
170 case CONNMAN_SERVICE_TYPE_VPN:
172 case CONNMAN_SERVICE_TYPE_GADGET:
179 enum connman_service_type __connman_service_string2type(const char *str)
182 return CONNMAN_SERVICE_TYPE_UNKNOWN;
184 if (strcmp(str, "ethernet") == 0)
185 return CONNMAN_SERVICE_TYPE_ETHERNET;
186 if (strcmp(str, "gadget") == 0)
187 return CONNMAN_SERVICE_TYPE_GADGET;
188 if (strcmp(str, "wifi") == 0)
189 return CONNMAN_SERVICE_TYPE_WIFI;
190 if (strcmp(str, "cellular") == 0)
191 return CONNMAN_SERVICE_TYPE_CELLULAR;
192 if (strcmp(str, "bluetooth") == 0)
193 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
194 if (strcmp(str, "wimax") == 0)
195 return CONNMAN_SERVICE_TYPE_WIMAX;
196 if (strcmp(str, "vpn") == 0)
197 return CONNMAN_SERVICE_TYPE_VPN;
198 if (strcmp(str, "gps") == 0)
199 return CONNMAN_SERVICE_TYPE_GPS;
200 if (strcmp(str, "system") == 0)
201 return CONNMAN_SERVICE_TYPE_SYSTEM;
203 return CONNMAN_SERVICE_TYPE_UNKNOWN;
206 static const char *security2string(enum connman_service_security security)
209 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
211 case CONNMAN_SERVICE_SECURITY_NONE:
213 case CONNMAN_SERVICE_SECURITY_WEP:
215 case CONNMAN_SERVICE_SECURITY_PSK:
216 case CONNMAN_SERVICE_SECURITY_WPA:
217 case CONNMAN_SERVICE_SECURITY_RSN:
219 case CONNMAN_SERVICE_SECURITY_8021X:
226 static const char *state2string(enum connman_service_state state)
229 case CONNMAN_SERVICE_STATE_UNKNOWN:
231 case CONNMAN_SERVICE_STATE_IDLE:
233 case CONNMAN_SERVICE_STATE_ASSOCIATION:
234 return "association";
235 case CONNMAN_SERVICE_STATE_CONFIGURATION:
236 return "configuration";
237 case CONNMAN_SERVICE_STATE_READY:
239 case CONNMAN_SERVICE_STATE_ONLINE:
241 case CONNMAN_SERVICE_STATE_DISCONNECT:
243 case CONNMAN_SERVICE_STATE_FAILURE:
250 static const char *error2string(enum connman_service_error error)
253 case CONNMAN_SERVICE_ERROR_UNKNOWN:
255 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
256 return "out-of-range";
257 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
258 return "pin-missing";
259 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
260 return "dhcp-failed";
261 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
262 return "connect-failed";
263 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
264 return "login-failed";
265 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
266 return "auth-failed";
267 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
268 return "invalid-key";
274 static enum connman_service_error string2error(const char *error)
276 if (g_strcmp0(error, "dhcp-failed") == 0)
277 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
278 else if (g_strcmp0(error, "pin-missing") == 0)
279 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
280 else if (g_strcmp0(error, "invalid-key") == 0)
281 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
283 return CONNMAN_SERVICE_ERROR_UNKNOWN;
286 static const char *proxymethod2string(enum connman_service_proxy_method method)
289 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
291 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
293 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
295 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
302 static enum connman_service_proxy_method string2proxymethod(const char *method)
304 if (g_strcmp0(method, "direct") == 0)
305 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
306 else if (g_strcmp0(method, "auto") == 0)
307 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
308 else if (g_strcmp0(method, "manual") == 0)
309 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
311 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
314 static int service_load(struct connman_service *service)
317 GError *error = NULL;
320 connman_bool_t autoconnect;
321 unsigned int ssid_len;
324 DBG("service %p", service);
326 keyfile = connman_storage_load_service(service->identifier);
327 if (keyfile == NULL) {
328 service->new_service = TRUE;
331 service->new_service = FALSE;
333 switch (service->type) {
334 case CONNMAN_SERVICE_TYPE_UNKNOWN:
335 case CONNMAN_SERVICE_TYPE_SYSTEM:
336 case CONNMAN_SERVICE_TYPE_GPS:
337 case CONNMAN_SERVICE_TYPE_GADGET:
339 case CONNMAN_SERVICE_TYPE_VPN:
340 service->do_split_routing = g_key_file_get_boolean(keyfile,
341 service->identifier, "SplitRouting", NULL);
343 case CONNMAN_SERVICE_TYPE_WIFI:
344 if (service->name == NULL) {
347 name = g_key_file_get_string(keyfile,
348 service->identifier, "Name", NULL);
350 g_free(service->name);
351 service->name = name;
354 if (service->network != NULL)
355 connman_network_set_name(service->network,
359 if (service->network &&
360 connman_network_get_blob(service->network,
361 "WiFi.SSID", &ssid_len) == NULL) {
364 hex_ssid = g_key_file_get_string(keyfile,
368 if (hex_ssid != NULL) {
370 unsigned int i, j = 0, hex;
371 size_t hex_ssid_len = strlen(hex_ssid);
373 ssid = g_try_malloc0(hex_ssid_len / 2);
380 for (i = 0; i < hex_ssid_len; i += 2) {
381 sscanf(hex_ssid + i, "%02x", &hex);
385 connman_network_set_blob(service->network,
386 "WiFi.SSID", ssid, hex_ssid_len / 2);
393 case CONNMAN_SERVICE_TYPE_WIMAX:
394 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
395 case CONNMAN_SERVICE_TYPE_CELLULAR:
396 service->favorite = g_key_file_get_boolean(keyfile,
397 service->identifier, "Favorite", NULL);
399 str = g_key_file_get_string(keyfile,
400 service->identifier, "Failure", NULL);
402 if (service->favorite == FALSE)
403 service->state_ipv4 = service->state_ipv6 =
404 CONNMAN_SERVICE_STATE_FAILURE;
405 service->error = string2error(str);
410 case CONNMAN_SERVICE_TYPE_ETHERNET:
411 autoconnect = g_key_file_get_boolean(keyfile,
412 service->identifier, "AutoConnect", &error);
414 service->autoconnect = autoconnect;
415 g_clear_error(&error);
419 str = g_key_file_get_string(keyfile,
420 service->identifier, "Modified", NULL);
422 g_time_val_from_iso8601(str, &service->modified);
426 str = g_key_file_get_string(keyfile,
427 service->identifier, "Passphrase", NULL);
429 g_free(service->passphrase);
430 service->passphrase = str;
433 if (service->ipconfig_ipv4 != NULL)
434 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
435 service->identifier, "IPv4.");
437 if (service->ipconfig_ipv6 != NULL)
438 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
439 service->identifier, "IPv6.");
441 service->nameservers_config = g_key_file_get_string_list(keyfile,
442 service->identifier, "Nameservers", &length, NULL);
443 if (service->nameservers_config != NULL && length == 0) {
444 g_strfreev(service->nameservers_config);
445 service->nameservers_config = NULL;
448 service->timeservers_config = g_key_file_get_string_list(keyfile,
449 service->identifier, "Timeservers", &length, NULL);
450 if (service->timeservers_config != NULL && length == 0) {
451 g_strfreev(service->timeservers_config);
452 service->timeservers_config = NULL;
455 service->domains = g_key_file_get_string_list(keyfile,
456 service->identifier, "Domains", &length, NULL);
457 if (service->domains != NULL && length == 0) {
458 g_strfreev(service->domains);
459 service->domains = NULL;
462 str = g_key_file_get_string(keyfile,
463 service->identifier, "Proxy.Method", NULL);
465 service->proxy_config = string2proxymethod(str);
469 service->proxies = g_key_file_get_string_list(keyfile,
470 service->identifier, "Proxy.Servers", &length, NULL);
471 if (service->proxies != NULL && length == 0) {
472 g_strfreev(service->proxies);
473 service->proxies = NULL;
476 service->excludes = g_key_file_get_string_list(keyfile,
477 service->identifier, "Proxy.Excludes", &length, NULL);
478 if (service->excludes != NULL && length == 0) {
479 g_strfreev(service->excludes);
480 service->excludes = NULL;
483 str = g_key_file_get_string(keyfile,
484 service->identifier, "Proxy.URL", NULL);
486 g_free(service->pac);
490 service->hidden_service = g_key_file_get_boolean(keyfile,
491 service->identifier, "Hidden", NULL);
494 g_key_file_free(keyfile);
499 static int service_save(struct connman_service *service)
504 const char *cst_str = NULL;
507 DBG("service %p new %d", service, service->new_service);
509 if (service->new_service == TRUE)
512 keyfile = __connman_storage_open_service(service->identifier);
516 if (service->name != NULL)
517 g_key_file_set_string(keyfile, service->identifier,
518 "Name", service->name);
520 switch (service->type) {
521 case CONNMAN_SERVICE_TYPE_UNKNOWN:
522 case CONNMAN_SERVICE_TYPE_SYSTEM:
523 case CONNMAN_SERVICE_TYPE_GPS:
524 case CONNMAN_SERVICE_TYPE_GADGET:
526 case CONNMAN_SERVICE_TYPE_VPN:
527 g_key_file_set_boolean(keyfile, service->identifier,
528 "SplitRouting", service->do_split_routing);
530 case CONNMAN_SERVICE_TYPE_WIFI:
531 if (service->network) {
532 const unsigned char *ssid;
533 unsigned int ssid_len = 0;
535 ssid = connman_network_get_blob(service->network,
536 "WiFi.SSID", &ssid_len);
538 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
539 char *identifier = service->identifier;
543 str = g_string_sized_new(ssid_len * 2);
549 for (i = 0; i < ssid_len; i++)
550 g_string_append_printf(str,
553 g_key_file_set_string(keyfile, identifier,
556 g_string_free(str, TRUE);
559 freq = connman_network_get_frequency(service->network);
560 g_key_file_set_integer(keyfile, service->identifier,
565 case CONNMAN_SERVICE_TYPE_WIMAX:
566 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
567 case CONNMAN_SERVICE_TYPE_CELLULAR:
568 g_key_file_set_boolean(keyfile, service->identifier,
569 "Favorite", service->favorite);
571 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
572 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
573 const char *failure = error2string(service->error);
575 g_key_file_set_string(keyfile,
579 g_key_file_remove_key(keyfile, service->identifier,
584 case CONNMAN_SERVICE_TYPE_ETHERNET:
585 if (service->favorite == TRUE)
586 g_key_file_set_boolean(keyfile, service->identifier,
587 "AutoConnect", service->autoconnect);
591 str = g_time_val_to_iso8601(&service->modified);
593 g_key_file_set_string(keyfile, service->identifier,
598 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
599 g_key_file_set_string(keyfile, service->identifier,
600 "Passphrase", service->passphrase);
602 g_key_file_remove_key(keyfile, service->identifier,
605 if (service->ipconfig_ipv4 != NULL)
606 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
607 service->identifier, "IPv4.");
609 if (service->ipconfig_ipv6 != NULL)
610 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
611 service->identifier, "IPv6.");
613 if (service->nameservers_config != NULL) {
614 guint len = g_strv_length(service->nameservers_config);
616 g_key_file_set_string_list(keyfile, service->identifier,
618 (const gchar **) service->nameservers_config, len);
620 g_key_file_remove_key(keyfile, service->identifier,
621 "Nameservers", NULL);
623 if (service->timeservers_config != NULL) {
624 guint len = g_strv_length(service->timeservers_config);
626 g_key_file_set_string_list(keyfile, service->identifier,
628 (const gchar **) service->timeservers_config, len);
630 g_key_file_remove_key(keyfile, service->identifier,
631 "Timeservers", NULL);
633 if (service->domains != NULL) {
634 guint len = g_strv_length(service->domains);
636 g_key_file_set_string_list(keyfile, service->identifier,
638 (const gchar **) service->domains, len);
640 g_key_file_remove_key(keyfile, service->identifier,
643 cst_str = proxymethod2string(service->proxy_config);
645 g_key_file_set_string(keyfile, service->identifier,
646 "Proxy.Method", cst_str);
648 if (service->proxies != NULL) {
649 guint len = g_strv_length(service->proxies);
651 g_key_file_set_string_list(keyfile, service->identifier,
653 (const gchar **) service->proxies, len);
655 g_key_file_remove_key(keyfile, service->identifier,
656 "Proxy.Servers", NULL);
658 if (service->excludes != NULL) {
659 guint len = g_strv_length(service->excludes);
661 g_key_file_set_string_list(keyfile, service->identifier,
663 (const gchar **) service->excludes, len);
665 g_key_file_remove_key(keyfile, service->identifier,
666 "Proxy.Excludes", NULL);
668 if (service->pac != NULL && strlen(service->pac) > 0)
669 g_key_file_set_string(keyfile, service->identifier,
670 "Proxy.URL", service->pac);
672 g_key_file_remove_key(keyfile, service->identifier,
675 if (service->hidden_service == TRUE)
676 g_key_file_set_boolean(keyfile, service->identifier, "Hidden",
680 __connman_storage_save_service(keyfile, service->identifier);
682 g_key_file_free(keyfile);
687 static enum connman_service_state combine_state(
688 enum connman_service_state state_a,
689 enum connman_service_state state_b)
691 enum connman_service_state result;
693 if (state_a == state_b) {
698 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
703 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
708 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
713 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
718 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
723 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
728 if (state_a == CONNMAN_SERVICE_STATE_READY) {
733 if (state_b == CONNMAN_SERVICE_STATE_READY) {
738 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
743 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
748 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
753 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
758 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
763 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
768 result = CONNMAN_SERVICE_STATE_FAILURE;
774 static connman_bool_t is_connecting_state(struct connman_service *service,
775 enum connman_service_state state)
778 case CONNMAN_SERVICE_STATE_UNKNOWN:
779 case CONNMAN_SERVICE_STATE_IDLE:
780 case CONNMAN_SERVICE_STATE_FAILURE:
781 if (service->network != NULL)
782 return connman_network_get_connecting(service->network);
783 case CONNMAN_SERVICE_STATE_DISCONNECT:
784 case CONNMAN_SERVICE_STATE_READY:
785 case CONNMAN_SERVICE_STATE_ONLINE:
787 case CONNMAN_SERVICE_STATE_ASSOCIATION:
788 case CONNMAN_SERVICE_STATE_CONFIGURATION:
795 static connman_bool_t is_connected_state(const struct connman_service *service,
796 enum connman_service_state state)
799 case CONNMAN_SERVICE_STATE_UNKNOWN:
800 case CONNMAN_SERVICE_STATE_IDLE:
801 case CONNMAN_SERVICE_STATE_ASSOCIATION:
802 case CONNMAN_SERVICE_STATE_CONFIGURATION:
803 case CONNMAN_SERVICE_STATE_DISCONNECT:
804 case CONNMAN_SERVICE_STATE_FAILURE:
806 case CONNMAN_SERVICE_STATE_READY:
807 case CONNMAN_SERVICE_STATE_ONLINE:
814 static connman_bool_t is_idle_state(const struct connman_service *service,
815 enum connman_service_state state)
818 case CONNMAN_SERVICE_STATE_UNKNOWN:
819 case CONNMAN_SERVICE_STATE_ASSOCIATION:
820 case CONNMAN_SERVICE_STATE_CONFIGURATION:
821 case CONNMAN_SERVICE_STATE_READY:
822 case CONNMAN_SERVICE_STATE_ONLINE:
823 case CONNMAN_SERVICE_STATE_DISCONNECT:
824 case CONNMAN_SERVICE_STATE_FAILURE:
826 case CONNMAN_SERVICE_STATE_IDLE:
833 static connman_bool_t is_connecting(struct connman_service *service)
835 return is_connecting_state(service, service->state);
838 static connman_bool_t is_connected(struct connman_service *service)
840 return is_connected_state(service, service->state);
843 static const char *nameserver_get_ifname(struct connman_service *service)
847 if (service->ipconfig_ipv4)
848 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv4);
849 else if (service->ipconfig_ipv6)
850 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv6);
857 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
858 case CONNMAN_SERVICE_STATE_UNKNOWN:
859 case CONNMAN_SERVICE_STATE_IDLE:
860 case CONNMAN_SERVICE_STATE_ASSOCIATION:
861 case CONNMAN_SERVICE_STATE_CONFIGURATION:
862 case CONNMAN_SERVICE_STATE_FAILURE:
863 case CONNMAN_SERVICE_STATE_DISCONNECT:
865 case CONNMAN_SERVICE_STATE_READY:
866 case CONNMAN_SERVICE_STATE_ONLINE:
873 static void remove_nameservers(struct connman_service *service,
874 const char* interface, char **ns)
876 const char *ifname = interface;
882 if (interface == NULL)
883 ifname = nameserver_get_ifname(service);
888 for (i = 0; ns[i] != NULL; i++)
889 connman_resolver_remove(ifname, NULL, ns[i]);
892 static void remove_searchdomains(struct connman_service *service,
893 const char *interface, char **sd)
895 const char *ifname = interface;
901 if (interface == NULL)
902 ifname = nameserver_get_ifname(service);
907 for (i = 0; sd[i] != NULL; i++)
908 connman_resolver_remove(ifname, sd[i], NULL);
911 static void update_nameservers(struct connman_service *service)
915 if (service->ipconfig_ipv4)
916 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv4);
917 else if (service->ipconfig_ipv6)
918 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv6);
925 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
926 case CONNMAN_SERVICE_STATE_UNKNOWN:
927 case CONNMAN_SERVICE_STATE_IDLE:
928 case CONNMAN_SERVICE_STATE_ASSOCIATION:
929 case CONNMAN_SERVICE_STATE_CONFIGURATION:
931 case CONNMAN_SERVICE_STATE_FAILURE:
932 case CONNMAN_SERVICE_STATE_DISCONNECT:
933 connman_resolver_remove_all(ifname);
935 case CONNMAN_SERVICE_STATE_READY:
936 case CONNMAN_SERVICE_STATE_ONLINE:
940 if (service->nameservers_config != NULL) {
943 for (i = 0; service->nameservers_config[i] != NULL; i++) {
944 connman_resolver_append(ifname, NULL,
945 service->nameservers_config[i]);
947 } else if (service->nameservers != NULL) {
950 for (i = 0; service->nameservers[i] != NULL; i++) {
951 connman_resolver_append(ifname, NULL,
952 service->nameservers[i]);
956 if (service->domains != NULL) {
959 for (i = 0; service->domains[i]; i++)
960 connman_resolver_append(ifname, service->domains[i],
962 } else if (service->domainname != NULL)
963 connman_resolver_append(ifname, service->domainname, NULL);
965 connman_resolver_flush();
969 * The is_auto variable is set to true when IPv6 autoconf nameservers are
970 * inserted to resolver via netlink message (see rtnl.c:rtnl_newnduseropt()
971 * for details) and not through service.c
973 int __connman_service_nameserver_append(struct connman_service *service,
974 const char *nameserver, gboolean is_auto)
979 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
981 if (nameserver == NULL)
985 nameservers = service->nameservers_auto;
987 nameservers = service->nameservers;
989 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
990 if (g_strcmp0(nameservers[i], nameserver) == 0)
993 if (nameservers != NULL) {
994 len = g_strv_length(nameservers);
995 nameservers = g_try_renew(char *, nameservers, len + 2);
998 nameservers = g_try_new0(char *, len + 2);
1001 if (nameservers == NULL)
1004 nameservers[len] = g_strdup(nameserver);
1005 if (nameservers[len] == NULL)
1008 nameservers[len + 1] = NULL;
1010 if (is_auto == TRUE) {
1011 service->nameservers_auto = nameservers;
1013 service->nameservers = nameservers;
1014 update_nameservers(service);
1020 int __connman_service_nameserver_remove(struct connman_service *service,
1021 const char *nameserver, gboolean is_auto)
1023 char **servers, **nameservers;
1024 gboolean found = FALSE;
1027 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
1029 if (nameserver == NULL)
1032 if (is_auto == TRUE)
1033 nameservers = service->nameservers_auto;
1035 nameservers = service->nameservers;
1037 if (nameservers == NULL)
1040 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
1041 if (g_strcmp0(nameservers[i], nameserver) == 0) {
1049 len = g_strv_length(nameservers);
1052 g_strfreev(nameservers);
1053 if (is_auto == TRUE)
1054 service->nameservers_auto = NULL;
1056 service->nameservers = NULL;
1061 servers = g_try_new0(char *, len);
1062 if (servers == NULL)
1065 for (i = 0, j = 0; i < len; i++) {
1066 if (g_strcmp0(nameservers[i], nameserver) != 0) {
1067 servers[j] = g_strdup(nameservers[i]);
1068 if (servers[j] == NULL)
1073 servers[len - 1] = NULL;
1075 g_strfreev(nameservers);
1076 nameservers = servers;
1078 if (is_auto == TRUE) {
1079 service->nameservers_auto = nameservers;
1081 service->nameservers = nameservers;
1082 update_nameservers(service);
1088 void __connman_service_nameserver_clear(struct connman_service *service)
1090 g_strfreev(service->nameservers);
1091 service->nameservers = NULL;
1093 update_nameservers(service);
1096 static void add_nameserver_route(int family, int index, char *nameserver,
1101 if (connman_inet_compare_subnet(index, nameserver) == TRUE)
1104 if (connman_inet_add_host_route(index, nameserver, gw) < 0)
1105 /* For P-t-P link the above route add will fail */
1106 connman_inet_add_host_route(index, nameserver, NULL);
1110 if (connman_inet_add_ipv6_host_route(index, nameserver,
1112 connman_inet_add_ipv6_host_route(index, nameserver,
1118 static void nameserver_add_routes(int index, char **nameservers,
1122 struct addrinfo hints;
1123 struct addrinfo *addr;
1125 for (i = 0; nameservers[i] != NULL; i++) {
1126 memset(&hints, 0, sizeof(struct addrinfo));
1127 hints.ai_flags = AI_NUMERICHOST;
1130 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1131 if (ret == EAI_NONAME)
1132 family = AF_INET; /* use the IPv4 as a default */
1136 family = addr->ai_family;
1138 add_nameserver_route(family, index, nameservers[i], gw);
1144 static void nameserver_del_routes(int index, char **nameservers,
1145 enum connman_ipconfig_type type)
1148 struct addrinfo hints;
1149 struct addrinfo *addr;
1151 for (i = 0; nameservers[i] != NULL; i++) {
1152 memset(&hints, 0, sizeof(struct addrinfo));
1153 hints.ai_flags = AI_NUMERICHOST;
1156 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1157 if (ret == EAI_NONAME)
1158 family = AF_INET; /* use the IPv4 as a default */
1162 family = addr->ai_family;
1166 if (type != CONNMAN_IPCONFIG_TYPE_IPV6)
1167 connman_inet_del_host_route(index,
1171 if (type != CONNMAN_IPCONFIG_TYPE_IPV4)
1172 connman_inet_del_ipv6_host_route(index,
1181 void __connman_service_nameserver_add_routes(struct connman_service *service,
1186 if (service == NULL)
1189 if (service->network != NULL)
1190 index = connman_network_get_index(service->network);
1191 else if (service->provider != NULL)
1192 index = connman_provider_get_index(service->provider);
1194 if (service->nameservers_config != NULL) {
1196 * Configured nameserver takes preference over the
1197 * discoverd nameserver gathered from DHCP, VPN, etc.
1199 nameserver_add_routes(index, service->nameservers_config, gw);
1200 } else if (service->nameservers != NULL) {
1202 * We add nameservers host routes for nameservers that
1203 * are not on our subnet. For those who are, the subnet
1204 * route will be installed by the time the dns proxy code
1205 * tries to reach them. The subnet route is installed
1206 * when setting the interface IP address.
1208 nameserver_add_routes(index, service->nameservers, gw);
1212 void __connman_service_nameserver_del_routes(struct connman_service *service,
1213 enum connman_ipconfig_type type)
1217 if (service == NULL)
1220 if (service->network != NULL)
1221 index = connman_network_get_index(service->network);
1222 else if (service->provider != NULL)
1223 index = connman_provider_get_index(service->provider);
1225 if (service->nameservers_config != NULL)
1226 nameserver_del_routes(index, service->nameservers_config,
1228 else if (service->nameservers != NULL)
1229 nameserver_del_routes(index, service->nameservers, type);
1232 static struct connman_stats *stats_get(struct connman_service *service)
1234 if (service->roaming == TRUE)
1235 return &service->stats_roaming;
1237 return &service->stats;
1240 static connman_bool_t stats_enabled(struct connman_service *service)
1242 struct connman_stats *stats = stats_get(service);
1244 return stats->enabled;
1247 static void stats_start(struct connman_service *service)
1249 struct connman_stats *stats = stats_get(service);
1251 DBG("service %p", service);
1253 if (stats->timer == NULL)
1256 stats->enabled = TRUE;
1257 stats->data_last.time = stats->data.time;
1259 g_timer_start(stats->timer);
1262 static void stats_stop(struct connman_service *service)
1264 struct connman_stats *stats = stats_get(service);
1265 unsigned int seconds;
1267 DBG("service %p", service);
1269 if (stats->timer == NULL)
1272 if (stats->enabled == FALSE)
1275 g_timer_stop(stats->timer);
1277 seconds = g_timer_elapsed(stats->timer, NULL);
1278 stats->data.time = stats->data_last.time + seconds;
1280 stats->enabled = FALSE;
1283 static void reset_stats(struct connman_service *service)
1285 DBG("service %p", service);
1288 service->stats.valid = FALSE;
1290 service->stats.data.rx_packets = 0;
1291 service->stats.data.tx_packets = 0;
1292 service->stats.data.rx_bytes = 0;
1293 service->stats.data.tx_bytes = 0;
1294 service->stats.data.rx_errors = 0;
1295 service->stats.data.tx_errors = 0;
1296 service->stats.data.rx_dropped = 0;
1297 service->stats.data.tx_dropped = 0;
1298 service->stats.data.time = 0;
1299 service->stats.data_last.time = 0;
1301 g_timer_reset(service->stats.timer);
1304 service->stats_roaming.valid = FALSE;
1306 service->stats_roaming.data.rx_packets = 0;
1307 service->stats_roaming.data.tx_packets = 0;
1308 service->stats_roaming.data.rx_bytes = 0;
1309 service->stats_roaming.data.tx_bytes = 0;
1310 service->stats_roaming.data.rx_errors = 0;
1311 service->stats_roaming.data.tx_errors = 0;
1312 service->stats_roaming.data.rx_dropped = 0;
1313 service->stats_roaming.data.tx_dropped = 0;
1314 service->stats_roaming.data.time = 0;
1315 service->stats_roaming.data_last.time = 0;
1317 g_timer_reset(service->stats_roaming.timer);
1320 struct connman_service *__connman_service_get_default(void)
1322 struct connman_service *service;
1323 GSequenceIter *iter;
1325 iter = g_sequence_get_begin_iter(service_list);
1327 if (g_sequence_iter_is_end(iter) == TRUE)
1330 service = g_sequence_get(iter);
1332 if (is_connected(service) == FALSE)
1338 static void default_changed(void)
1340 struct connman_service *service = __connman_service_get_default();
1342 if (service == current_default)
1345 __connman_service_timeserver_changed(current_default, NULL);
1347 current_default = service;
1349 __connman_notifier_default_changed(service);
1352 static void state_changed(struct connman_service *service)
1356 __connman_notifier_service_state_changed(service, service->state);
1358 str = state2string(service->state);
1362 connman_dbus_property_changed_basic(service->path,
1363 CONNMAN_SERVICE_INTERFACE, "State",
1364 DBUS_TYPE_STRING, &str);
1367 static void strength_changed(struct connman_service *service)
1369 if (service->strength == 0)
1372 connman_dbus_property_changed_basic(service->path,
1373 CONNMAN_SERVICE_INTERFACE, "Strength",
1374 DBUS_TYPE_BYTE, &service->strength);
1377 static void favorite_changed(struct connman_service *service)
1379 if (service->path == NULL)
1382 connman_dbus_property_changed_basic(service->path,
1383 CONNMAN_SERVICE_INTERFACE, "Favorite",
1384 DBUS_TYPE_BOOLEAN, &service->favorite);
1387 static void immutable_changed(struct connman_service *service)
1389 if (service->path == NULL)
1392 connman_dbus_property_changed_basic(service->path,
1393 CONNMAN_SERVICE_INTERFACE, "Immutable",
1394 DBUS_TYPE_BOOLEAN, &service->immutable);
1397 static void roaming_changed(struct connman_service *service)
1399 if (service->path == NULL)
1402 connman_dbus_property_changed_basic(service->path,
1403 CONNMAN_SERVICE_INTERFACE, "Roaming",
1404 DBUS_TYPE_BOOLEAN, &service->roaming);
1407 static void autoconnect_changed(struct connman_service *service)
1409 if (service->path == NULL)
1412 connman_dbus_property_changed_basic(service->path,
1413 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
1414 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1417 static void append_security(DBusMessageIter *iter, void *user_data)
1419 struct connman_service *service = user_data;
1422 str = security2string(service->security);
1424 dbus_message_iter_append_basic(iter,
1425 DBUS_TYPE_STRING, &str);
1428 * Some access points incorrectly advertise WPS even when they
1429 * are configured as open or no security, so filter
1432 if (service->wps == TRUE) {
1433 switch (service->security) {
1434 case CONNMAN_SERVICE_SECURITY_PSK:
1435 case CONNMAN_SERVICE_SECURITY_WPA:
1436 case CONNMAN_SERVICE_SECURITY_RSN:
1438 dbus_message_iter_append_basic(iter,
1439 DBUS_TYPE_STRING, &str);
1441 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1442 case CONNMAN_SERVICE_SECURITY_NONE:
1443 case CONNMAN_SERVICE_SECURITY_WEP:
1444 case CONNMAN_SERVICE_SECURITY_8021X:
1450 static void append_ethernet(DBusMessageIter *iter, void *user_data)
1452 struct connman_service *service = user_data;
1454 if (service->ipconfig_ipv4 != NULL)
1455 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
1457 else if (service->ipconfig_ipv6 != NULL)
1458 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
1462 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1464 struct connman_service *service = user_data;
1466 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1467 state2string(service->state_ipv4));
1469 if (is_connected_state(service, service->state_ipv4) == FALSE)
1472 if (service->ipconfig_ipv4 != NULL)
1473 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1476 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1478 struct connman_service *service = user_data;
1480 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1481 state2string(service->state_ipv6));
1483 if (is_connected_state(service, service->state_ipv6) == FALSE)
1486 if (service->ipconfig_ipv6 != NULL)
1487 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1488 service->ipconfig_ipv4);
1491 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1493 struct connman_service *service = user_data;
1495 if (service->ipconfig_ipv4 != NULL)
1496 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1500 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1502 struct connman_service *service = user_data;
1504 if (service->ipconfig_ipv6 != NULL)
1505 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1509 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1514 servers = *nameservers;
1516 for (i = 0; servers[i] != NULL; i++) {
1517 dbus_message_iter_append_basic(iter,
1518 DBUS_TYPE_STRING, &servers[i]);
1522 static void append_dns(DBusMessageIter *iter, void *user_data)
1524 struct connman_service *service = user_data;
1526 if (is_connected(service) == FALSE)
1529 if (service->nameservers_config != NULL) {
1530 append_nameserver(iter, &service->nameservers_config);
1533 if (service->nameservers != NULL)
1534 append_nameserver(iter, &service->nameservers);
1536 if (service->nameservers_auto != NULL)
1537 append_nameserver(iter, &service->nameservers_auto);
1541 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1543 struct connman_service *service = user_data;
1546 if (service->nameservers_config == NULL)
1549 for (i = 0; service->nameservers_config[i]; i++) {
1550 dbus_message_iter_append_basic(iter,
1552 &service->nameservers_config[i]);
1556 static void append_ts(DBusMessageIter *iter, void *user_data)
1558 GSList *list = user_data;
1560 while (list != NULL) {
1561 char *timeserver = list->data;
1563 if (timeserver != NULL)
1564 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
1567 list = g_slist_next(list);
1571 static void append_tsconfig(DBusMessageIter *iter, void *user_data)
1573 struct connman_service *service = user_data;
1576 if (service->timeservers_config == NULL)
1579 for (i = 0; service->timeservers_config[i]; i++) {
1580 dbus_message_iter_append_basic(iter,
1582 &service->timeservers_config[i]);
1586 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1588 struct connman_service *service = user_data;
1591 if (service->domains == NULL)
1594 for (i = 0; service->domains[i]; i++)
1595 dbus_message_iter_append_basic(iter,
1596 DBUS_TYPE_STRING, &service->domains[i]);
1599 static void append_domain(DBusMessageIter *iter, void *user_data)
1601 struct connman_service *service = user_data;
1603 if (is_connected(service) == FALSE &&
1604 is_connecting(service) == FALSE)
1607 if (service->domains != NULL)
1608 append_domainconfig(iter, user_data);
1609 else if (service->domainname != NULL)
1610 dbus_message_iter_append_basic(iter,
1611 DBUS_TYPE_STRING, &service->domainname);
1614 static void append_proxies(DBusMessageIter *iter, void *user_data)
1616 struct connman_service *service = user_data;
1619 if (service->proxies == NULL)
1622 for (i = 0; service->proxies[i]; i++)
1623 dbus_message_iter_append_basic(iter,
1624 DBUS_TYPE_STRING, &service->proxies[i]);
1627 static void append_excludes(DBusMessageIter *iter, void *user_data)
1629 struct connman_service *service = user_data;
1632 if (service->excludes == NULL)
1635 for (i = 0; service->excludes[i]; i++)
1636 dbus_message_iter_append_basic(iter,
1637 DBUS_TYPE_STRING, &service->excludes[i]);
1640 static void append_proxy(DBusMessageIter *iter, void *user_data)
1642 struct connman_service *service = user_data;
1643 enum connman_service_proxy_method proxy;
1644 const char *pac = NULL;
1645 const char *method = proxymethod2string(
1646 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1650 if (is_connected(service) == FALSE)
1653 proxy = connman_service_get_proxy_method(service);
1656 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1658 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1660 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1661 connman_dbus_dict_append_array(iter, "Servers",
1662 DBUS_TYPE_STRING, append_proxies,
1665 connman_dbus_dict_append_array(iter, "Excludes",
1666 DBUS_TYPE_STRING, append_excludes,
1669 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1670 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1671 if (service->ipconfig_ipv4 != NULL)
1672 pac = __connman_ipconfig_get_proxy_autoconfig(
1673 service->ipconfig_ipv4);
1674 else if (service->ipconfig_ipv6 != NULL)
1675 pac = __connman_ipconfig_get_proxy_autoconfig(
1676 service->ipconfig_ipv6);
1678 if (service->pac == NULL && pac == NULL)
1681 if (service->pac != NULL)
1684 connman_dbus_dict_append_basic(iter, "URL",
1685 DBUS_TYPE_STRING, &pac);
1689 method = proxymethod2string(proxy);
1692 connman_dbus_dict_append_basic(iter, "Method",
1693 DBUS_TYPE_STRING, &method);
1696 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1698 struct connman_service *service = user_data;
1701 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1704 switch (service->proxy_config) {
1705 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1707 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1709 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1710 if (service->proxies != NULL)
1711 connman_dbus_dict_append_array(iter, "Servers",
1713 append_proxies, service);
1715 if (service->excludes != NULL)
1716 connman_dbus_dict_append_array(iter, "Excludes",
1718 append_excludes, service);
1720 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1721 if (service->pac != NULL)
1722 connman_dbus_dict_append_basic(iter, "URL",
1723 DBUS_TYPE_STRING, &service->pac);
1727 method = proxymethod2string(service->proxy_config);
1729 connman_dbus_dict_append_basic(iter, "Method",
1730 DBUS_TYPE_STRING, &method);
1733 static void append_provider(DBusMessageIter *iter, void *user_data)
1735 struct connman_service *service = user_data;
1737 DBG("%p %p", service, service->provider);
1739 if (is_connected(service) == FALSE)
1742 if (service->provider != NULL)
1743 __connman_provider_append_properties(service->provider, iter);
1747 static void settings_changed(struct connman_service *service,
1748 struct connman_ipconfig *ipconfig)
1750 connman_dbus_property_changed_dict(service->path,
1751 CONNMAN_SERVICE_INTERFACE, "IPv4",
1752 append_ipv4, service);
1754 connman_dbus_property_changed_dict(service->path,
1755 CONNMAN_SERVICE_INTERFACE, "IPv6",
1756 append_ipv6, service);
1758 __connman_notifier_ipconfig_changed(service, ipconfig);
1761 static void ipv4_configuration_changed(struct connman_service *service)
1763 connman_dbus_property_changed_dict(service->path,
1764 CONNMAN_SERVICE_INTERFACE,
1765 "IPv4.Configuration",
1770 static void ipv6_configuration_changed(struct connman_service *service)
1772 connman_dbus_property_changed_dict(service->path,
1773 CONNMAN_SERVICE_INTERFACE,
1774 "IPv6.Configuration",
1779 static void dns_changed(struct connman_service *service)
1781 connman_dbus_property_changed_array(service->path,
1782 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1783 DBUS_TYPE_STRING, append_dns, service);
1786 static void dns_configuration_changed(struct connman_service *service)
1788 connman_dbus_property_changed_array(service->path,
1789 CONNMAN_SERVICE_INTERFACE,
1790 "Nameservers.Configuration",
1791 DBUS_TYPE_STRING, append_dnsconfig, service);
1793 dns_changed(service);
1796 static void domain_changed(struct connman_service *service)
1798 connman_dbus_property_changed_array(service->path,
1799 CONNMAN_SERVICE_INTERFACE, "Domains",
1800 DBUS_TYPE_STRING, append_domain, service);
1803 static void domain_configuration_changed(struct connman_service *service)
1805 connman_dbus_property_changed_array(service->path,
1806 CONNMAN_SERVICE_INTERFACE,
1807 "Domains.Configuration",
1808 DBUS_TYPE_STRING, append_domainconfig, service);
1811 static void proxy_changed(struct connman_service *service)
1813 connman_dbus_property_changed_dict(service->path,
1814 CONNMAN_SERVICE_INTERFACE, "Proxy",
1815 append_proxy, service);
1818 static void proxy_configuration_changed(struct connman_service *service)
1820 connman_dbus_property_changed_dict(service->path,
1821 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1822 append_proxyconfig, service);
1824 proxy_changed(service);
1827 static void timeservers_configuration_changed(struct connman_service *service)
1829 connman_dbus_property_changed_array(service->path,
1830 CONNMAN_SERVICE_INTERFACE,
1831 "Timeservers.Configuration",
1833 append_tsconfig, service);
1836 static void link_changed(struct connman_service *service)
1838 connman_dbus_property_changed_dict(service->path,
1839 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1840 append_ethernet, service);
1843 static void stats_append_counters(DBusMessageIter *dict,
1844 struct connman_stats_data *stats,
1845 struct connman_stats_data *counters,
1846 connman_bool_t append_all)
1848 if (counters->rx_packets != stats->rx_packets || append_all) {
1849 counters->rx_packets = stats->rx_packets;
1850 connman_dbus_dict_append_basic(dict, "RX.Packets",
1851 DBUS_TYPE_UINT32, &stats->rx_packets);
1854 if (counters->tx_packets != stats->tx_packets || append_all) {
1855 counters->tx_packets = stats->tx_packets;
1856 connman_dbus_dict_append_basic(dict, "TX.Packets",
1857 DBUS_TYPE_UINT32, &stats->tx_packets);
1860 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1861 counters->rx_bytes = stats->rx_bytes;
1862 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1863 DBUS_TYPE_UINT32, &stats->rx_bytes);
1866 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1867 counters->tx_bytes = stats->tx_bytes;
1868 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1869 DBUS_TYPE_UINT32, &stats->tx_bytes);
1872 if (counters->rx_errors != stats->rx_errors || append_all) {
1873 counters->rx_errors = stats->rx_errors;
1874 connman_dbus_dict_append_basic(dict, "RX.Errors",
1875 DBUS_TYPE_UINT32, &stats->rx_errors);
1878 if (counters->tx_errors != stats->tx_errors || append_all) {
1879 counters->tx_errors = stats->tx_errors;
1880 connman_dbus_dict_append_basic(dict, "TX.Errors",
1881 DBUS_TYPE_UINT32, &stats->tx_errors);
1884 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1885 counters->rx_dropped = stats->rx_dropped;
1886 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1887 DBUS_TYPE_UINT32, &stats->rx_dropped);
1890 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1891 counters->tx_dropped = stats->tx_dropped;
1892 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1893 DBUS_TYPE_UINT32, &stats->tx_dropped);
1896 if (counters->time != stats->time || append_all) {
1897 counters->time = stats->time;
1898 connman_dbus_dict_append_basic(dict, "Time",
1899 DBUS_TYPE_UINT32, &stats->time);
1903 static void stats_append(struct connman_service *service,
1904 const char *counter,
1905 struct connman_stats_counter *counters,
1906 connman_bool_t append_all)
1908 DBusMessageIter array, dict;
1911 DBG("service %p counter %s", service, counter);
1913 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1917 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1918 &service->path, DBUS_TYPE_INVALID);
1920 dbus_message_iter_init_append(msg, &array);
1923 connman_dbus_dict_open(&array, &dict);
1925 stats_append_counters(&dict, &service->stats.data,
1926 &counters->stats.data, append_all);
1928 connman_dbus_dict_close(&array, &dict);
1930 /* roaming counter */
1931 connman_dbus_dict_open(&array, &dict);
1933 stats_append_counters(&dict, &service->stats_roaming.data,
1934 &counters->stats_roaming.data, append_all);
1936 connman_dbus_dict_close(&array, &dict);
1938 __connman_counter_send_usage(counter, msg);
1941 static void stats_update(struct connman_service *service,
1942 unsigned int rx_packets, unsigned int tx_packets,
1943 unsigned int rx_bytes, unsigned int tx_bytes,
1944 unsigned int rx_errors, unsigned int tx_errors,
1945 unsigned int rx_dropped, unsigned int tx_dropped)
1947 struct connman_stats *stats = stats_get(service);
1948 struct connman_stats_data *data_last = &stats->data_last;
1949 struct connman_stats_data *data = &stats->data;
1950 unsigned int seconds;
1952 DBG("service %p", service);
1954 if (stats->valid == TRUE) {
1956 rx_packets - data_last->rx_packets;
1958 tx_packets - data_last->tx_packets;
1960 rx_bytes - data_last->rx_bytes;
1962 tx_bytes - data_last->tx_bytes;
1964 rx_errors - data_last->rx_errors;
1966 tx_errors - data_last->tx_errors;
1968 rx_dropped - data_last->rx_dropped;
1970 tx_dropped - data_last->tx_dropped;
1972 stats->valid = TRUE;
1975 data_last->rx_packets = rx_packets;
1976 data_last->tx_packets = tx_packets;
1977 data_last->rx_bytes = rx_bytes;
1978 data_last->tx_bytes = tx_bytes;
1979 data_last->rx_errors = rx_errors;
1980 data_last->tx_errors = tx_errors;
1981 data_last->rx_dropped = rx_dropped;
1982 data_last->tx_dropped = tx_dropped;
1984 seconds = g_timer_elapsed(stats->timer, NULL);
1985 stats->data.time = stats->data_last.time + seconds;
1988 void __connman_service_notify(struct connman_service *service,
1989 unsigned int rx_packets, unsigned int tx_packets,
1990 unsigned int rx_bytes, unsigned int tx_bytes,
1991 unsigned int rx_errors, unsigned int tx_errors,
1992 unsigned int rx_dropped, unsigned int tx_dropped)
1994 GHashTableIter iter;
1995 gpointer key, value;
1996 const char *counter;
1997 struct connman_stats_counter *counters;
1998 struct connman_stats_data *data;
2001 if (service == NULL)
2004 if (is_connected(service) == FALSE)
2007 stats_update(service,
2008 rx_packets, tx_packets,
2010 rx_errors, tx_errors,
2011 rx_dropped, tx_dropped);
2013 data = &stats_get(service)->data;
2014 err = __connman_stats_update(service, service->roaming, data);
2016 connman_error("Failed to store statistics for %s",
2017 service->identifier);
2019 g_hash_table_iter_init(&iter, service->counter_table);
2020 while (g_hash_table_iter_next(&iter, &key, &value)) {
2024 stats_append(service, counter, counters, counters->append_all);
2025 counters->append_all = FALSE;
2029 int __connman_service_counter_register(const char *counter)
2031 struct connman_service *service;
2032 GSequenceIter *iter;
2033 struct connman_stats_counter *counters;
2035 DBG("counter %s", counter);
2037 counter_list = g_slist_append(counter_list, (gpointer)counter);
2039 iter = g_sequence_get_begin_iter(service_list);
2041 while (g_sequence_iter_is_end(iter) == FALSE) {
2042 service = g_sequence_get(iter);
2044 counters = g_try_new0(struct connman_stats_counter, 1);
2045 if (counters == NULL)
2048 counters->append_all = TRUE;
2050 g_hash_table_replace(service->counter_table, (gpointer)counter,
2053 iter = g_sequence_iter_next(iter);
2059 void __connman_service_counter_unregister(const char *counter)
2061 struct connman_service *service;
2062 GSequenceIter *iter;
2064 DBG("counter %s", counter);
2066 iter = g_sequence_get_begin_iter(service_list);
2068 while (g_sequence_iter_is_end(iter) == FALSE) {
2069 service = g_sequence_get(iter);
2071 g_hash_table_remove(service->counter_table, counter);
2073 iter = g_sequence_iter_next(iter);
2076 counter_list = g_slist_remove(counter_list, counter);
2079 GSequence *__connman_service_get_list(struct connman_session *session,
2080 service_match_cb service_match,
2081 create_service_entry_cb create_service_entry,
2082 GDestroyNotify destroy_service_entry)
2085 GSequenceIter *iter;
2086 struct connman_service *service;
2087 struct service_entry *entry;
2089 list = g_sequence_new(destroy_service_entry);
2093 iter = g_sequence_get_begin_iter(service_list);
2095 while (g_sequence_iter_is_end(iter) == FALSE) {
2096 service = g_sequence_get(iter);
2098 if (service_match(session, service) == TRUE) {
2099 entry = create_service_entry(service, service->name,
2104 g_sequence_append(list, entry);
2107 iter = g_sequence_iter_next(iter);
2113 void __connman_service_session_inc(struct connman_service *service)
2115 DBG("service %p ref count %d", service,
2116 service->session_usage_count + 1);
2118 __sync_fetch_and_add(&service->session_usage_count, 1);
2121 connman_bool_t __connman_service_session_dec(struct connman_service *service)
2123 DBG("service %p ref count %d", service,
2124 service->session_usage_count - 1);
2126 if (__sync_fetch_and_sub(&service->session_usage_count, 1) != 1)
2132 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
2133 struct connman_service *service)
2138 str = __connman_service_type2string(service->type);
2140 connman_dbus_dict_append_basic(dict, "Type",
2141 DBUS_TYPE_STRING, &str);
2143 connman_dbus_dict_append_array(dict, "Security",
2144 DBUS_TYPE_STRING, append_security, service);
2146 str = state2string(service->state);
2148 connman_dbus_dict_append_basic(dict, "State",
2149 DBUS_TYPE_STRING, &str);
2151 str = error2string(service->error);
2153 connman_dbus_dict_append_basic(dict, "Error",
2154 DBUS_TYPE_STRING, &str);
2156 if (service->strength > 0)
2157 connman_dbus_dict_append_basic(dict, "Strength",
2158 DBUS_TYPE_BYTE, &service->strength);
2160 connman_dbus_dict_append_basic(dict, "Favorite",
2161 DBUS_TYPE_BOOLEAN, &service->favorite);
2163 connman_dbus_dict_append_basic(dict, "Immutable",
2164 DBUS_TYPE_BOOLEAN, &service->immutable);
2166 if (service->favorite == TRUE)
2167 connman_dbus_dict_append_basic(dict, "AutoConnect",
2168 DBUS_TYPE_BOOLEAN, &service->autoconnect);
2170 connman_dbus_dict_append_basic(dict, "AutoConnect",
2171 DBUS_TYPE_BOOLEAN, &service->favorite);
2173 if (service->name != NULL)
2174 connman_dbus_dict_append_basic(dict, "Name",
2175 DBUS_TYPE_STRING, &service->name);
2177 switch (service->type) {
2178 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2179 case CONNMAN_SERVICE_TYPE_SYSTEM:
2180 case CONNMAN_SERVICE_TYPE_GPS:
2181 case CONNMAN_SERVICE_TYPE_VPN:
2182 case CONNMAN_SERVICE_TYPE_GADGET:
2184 case CONNMAN_SERVICE_TYPE_CELLULAR:
2185 connman_dbus_dict_append_basic(dict, "Roaming",
2186 DBUS_TYPE_BOOLEAN, &service->roaming);
2188 connman_dbus_dict_append_dict(dict, "Ethernet",
2189 append_ethernet, service);
2191 case CONNMAN_SERVICE_TYPE_WIFI:
2192 case CONNMAN_SERVICE_TYPE_ETHERNET:
2193 case CONNMAN_SERVICE_TYPE_WIMAX:
2194 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2195 connman_dbus_dict_append_dict(dict, "Ethernet",
2196 append_ethernet, service);
2200 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
2202 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
2203 append_ipv4config, service);
2205 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
2207 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
2208 append_ipv6config, service);
2210 connman_dbus_dict_append_array(dict, "Nameservers",
2211 DBUS_TYPE_STRING, append_dns, service);
2213 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
2214 DBUS_TYPE_STRING, append_dnsconfig, service);
2216 if (service->state == CONNMAN_SERVICE_STATE_READY ||
2217 service->state == CONNMAN_SERVICE_STATE_ONLINE)
2218 list = __connman_timeserver_get_all(service);
2222 connman_dbus_dict_append_array(dict, "Timeservers",
2223 DBUS_TYPE_STRING, append_ts, list);
2225 g_slist_free_full(list, g_free);
2227 connman_dbus_dict_append_array(dict, "Timeservers.Configuration",
2228 DBUS_TYPE_STRING, append_tsconfig, service);
2230 connman_dbus_dict_append_array(dict, "Domains",
2231 DBUS_TYPE_STRING, append_domain, service);
2233 connman_dbus_dict_append_array(dict, "Domains.Configuration",
2234 DBUS_TYPE_STRING, append_domainconfig, service);
2236 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
2238 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
2239 append_proxyconfig, service);
2241 connman_dbus_dict_append_dict(dict, "Provider",
2242 append_provider, service);
2245 static void append_struct_service(DBusMessageIter *iter,
2246 connman_dbus_append_cb_t function,
2247 struct connman_service *service)
2249 DBusMessageIter entry, dict;
2251 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
2253 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2256 connman_dbus_dict_open(&entry, &dict);
2257 if (function != NULL)
2258 function(&dict, service);
2259 connman_dbus_dict_close(&entry, &dict);
2261 dbus_message_iter_close_container(iter, &entry);
2264 static void append_dict_properties(DBusMessageIter *dict, void *user_data)
2266 struct connman_service *service = user_data;
2268 append_properties(dict, TRUE, service);
2271 static void append_struct(gpointer value, gpointer user_data)
2273 struct connman_service *service = value;
2274 DBusMessageIter *iter = user_data;
2276 if (service->path == NULL)
2279 append_struct_service(iter, append_dict_properties, service);
2282 void __connman_service_list_struct(DBusMessageIter *iter)
2284 g_sequence_foreach(service_list, append_struct, iter);
2287 connman_bool_t __connman_service_is_hidden(struct connman_service *service)
2289 return service->hidden;
2293 __connman_service_is_split_routing(struct connman_service *service)
2295 return service->do_split_routing;
2298 int __connman_service_get_index(struct connman_service *service)
2300 if (service == NULL)
2303 if (service->network != NULL)
2304 return connman_network_get_index(service->network);
2305 else if (service->provider != NULL)
2306 return connman_provider_get_index(service->provider);
2311 void __connman_service_set_hidden(struct connman_service *service)
2313 if (service == NULL || service->hidden == TRUE)
2316 service->hidden_service = TRUE;
2319 void __connman_service_set_domainname(struct connman_service *service,
2320 const char *domainname)
2322 if (service == NULL || service->hidden == TRUE)
2325 g_free(service->domainname);
2326 service->domainname = g_strdup(domainname);
2328 domain_changed(service);
2331 const char *connman_service_get_domainname(struct connman_service *service)
2333 if (service == NULL)
2336 if (service->domains != NULL)
2337 return service->domains[0];
2339 return service->domainname;
2342 char **connman_service_get_nameservers(struct connman_service *service)
2344 if (service == NULL)
2347 if (service->nameservers_config != NULL)
2348 return g_strdupv(service->nameservers_config);
2349 else if (service->nameservers != NULL ||
2350 service->nameservers_auto != NULL) {
2351 int len = 0, len_auto = 0, i;
2354 if (service->nameservers != NULL)
2355 len = g_strv_length(service->nameservers);
2356 if (service->nameservers_auto != NULL)
2357 len_auto = g_strv_length(service->nameservers_auto);
2359 nameservers = g_try_new0(char *, len + len_auto + 1);
2360 if (nameservers == NULL)
2363 for (i = 0; i < len; i++)
2364 nameservers[i] = g_strdup(service->nameservers[i]);
2366 for (i = 0; i < len_auto; i++)
2367 nameservers[i + len] =
2368 g_strdup(service->nameservers_auto[i]);
2376 char **connman_service_get_timeservers_config(struct connman_service *service)
2378 if (service == NULL)
2381 return service->timeservers_config;
2384 char **connman_service_get_timeservers(struct connman_service *service)
2386 if (service == NULL)
2389 if (service->timeservers != NULL)
2390 return service->timeservers;
2395 void connman_service_set_proxy_method(struct connman_service *service,
2396 enum connman_service_proxy_method method)
2398 if (service == NULL || service->hidden == TRUE)
2401 service->proxy = method;
2403 proxy_changed(service);
2405 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
2406 __connman_notifier_proxy_changed(service);
2409 enum connman_service_proxy_method connman_service_get_proxy_method(
2410 struct connman_service *service)
2412 if (service == NULL)
2413 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2415 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
2416 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
2417 service->pac == NULL)
2418 return service->proxy;
2420 return service->proxy_config;
2423 return service->proxy;
2426 char **connman_service_get_proxy_servers(struct connman_service *service)
2428 return g_strdupv(service->proxies);
2431 char **connman_service_get_proxy_excludes(struct connman_service *service)
2433 return g_strdupv(service->excludes);
2436 const char *connman_service_get_proxy_url(struct connman_service *service)
2438 if (service == NULL)
2441 return service->pac;
2444 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
2447 if (service == NULL || service->hidden == TRUE)
2450 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
2452 if (service->ipconfig_ipv4) {
2453 if (__connman_ipconfig_set_proxy_autoconfig(
2454 service->ipconfig_ipv4, url) < 0)
2456 } else if (service->ipconfig_ipv6) {
2457 if (__connman_ipconfig_set_proxy_autoconfig(
2458 service->ipconfig_ipv6, url) < 0)
2463 proxy_changed(service);
2465 __connman_notifier_proxy_changed(service);
2468 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
2470 if (service == NULL)
2473 if (service->ipconfig_ipv4)
2474 return __connman_ipconfig_get_proxy_autoconfig(
2475 service->ipconfig_ipv4);
2476 else if (service->ipconfig_ipv6)
2477 return __connman_ipconfig_get_proxy_autoconfig(
2478 service->ipconfig_ipv6);
2482 int __connman_service_timeserver_append(struct connman_service *service,
2483 const char *timeserver)
2487 DBG("service %p timeserver %s", service, timeserver);
2489 if (timeserver == NULL)
2492 if (service->timeservers != NULL) {
2495 for (i = 0; service->timeservers[i] != NULL; i++)
2496 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2499 len = g_strv_length(service->timeservers);
2500 service->timeservers = g_try_renew(char *, service->timeservers,
2504 service->timeservers = g_try_new0(char *, len + 2);
2507 if (service->timeservers == NULL)
2510 service->timeservers[len] = g_strdup(timeserver);
2511 service->timeservers[len + 1] = NULL;
2516 int __connman_service_timeserver_remove(struct connman_service *service,
2517 const char *timeserver)
2520 int len, i, j, found = 0;
2522 DBG("service %p timeserver %s", service, timeserver);
2524 if (timeserver == NULL)
2527 if (service->timeservers == NULL)
2530 for (i = 0; service->timeservers != NULL &&
2531 service->timeservers[i] != NULL; i++)
2532 if (g_strcmp0(service->timeservers[i], timeserver) == 0) {
2540 len = g_strv_length(service->timeservers);
2543 g_strfreev(service->timeservers);
2544 service->timeservers = NULL;
2549 servers = g_try_new0(char *, len);
2550 if (servers == NULL)
2553 for (i = 0, j = 0; i < len; i++) {
2554 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2555 servers[j] = g_strdup(service->timeservers[i]);
2556 if (servers[j] == NULL)
2561 servers[len - 1] = NULL;
2563 g_strfreev(service->timeservers);
2564 service->timeservers = servers;
2569 void __connman_service_timeserver_changed(struct connman_service *service,
2572 if (service == NULL)
2575 connman_dbus_property_changed_array(service->path,
2576 CONNMAN_SERVICE_INTERFACE, "Timeservers",
2577 DBUS_TYPE_STRING, append_ts, ts_list);
2580 void __connman_service_set_pac(struct connman_service *service,
2583 if (service->hidden == TRUE)
2585 g_free(service->pac);
2586 service->pac = g_strdup(pac);
2588 proxy_changed(service);
2591 void __connman_service_set_identity(struct connman_service *service,
2592 const char *identity)
2594 if (service->immutable || service->hidden == TRUE)
2597 g_free(service->identity);
2598 service->identity = g_strdup(identity);
2600 if (service->network != NULL)
2601 connman_network_set_string(service->network,
2606 void __connman_service_set_agent_identity(struct connman_service *service,
2607 const char *agent_identity)
2609 if (service->hidden == TRUE)
2611 g_free(service->agent_identity);
2612 service->agent_identity = g_strdup(agent_identity);
2614 if (service->network != NULL)
2615 connman_network_set_string(service->network,
2616 "WiFi.AgentIdentity",
2617 service->agent_identity);
2620 static int check_passphrase(enum connman_service_security security,
2621 const char *passphrase)
2626 if (passphrase == NULL)
2629 length = strlen(passphrase);
2632 case CONNMAN_SERVICE_SECURITY_PSK:
2633 case CONNMAN_SERVICE_SECURITY_WPA:
2634 case CONNMAN_SERVICE_SECURITY_RSN:
2635 /* A raw key is always 64 bytes length,
2636 * its content is in hex representation.
2637 * A PSK key must be between [8..63].
2640 for (i = 0; i < 64; i++)
2641 if (!isxdigit((unsigned char)
2644 } else if (length < 8 || length > 63)
2647 case CONNMAN_SERVICE_SECURITY_WEP:
2648 /* length of WEP key is 10 or 26
2649 * length of WEP passphrase is 5 or 13
2651 if (length == 10 || length == 26) {
2652 for (i = 0; i < length; i++)
2653 if (!isxdigit((unsigned char)
2656 } else if (length != 5 && length != 13)
2659 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2660 case CONNMAN_SERVICE_SECURITY_NONE:
2661 case CONNMAN_SERVICE_SECURITY_8021X:
2668 int __connman_service_set_passphrase(struct connman_service *service,
2669 const char *passphrase)
2673 if (service->immutable == TRUE || service->hidden == TRUE)
2676 err = check_passphrase(service->security, passphrase);
2679 g_free(service->passphrase);
2680 service->passphrase = g_strdup(passphrase);
2682 if (service->network != NULL)
2683 connman_network_set_string(service->network,
2685 service->passphrase);
2686 service_save(service);
2692 void __connman_service_set_agent_passphrase(struct connman_service *service,
2693 const char *agent_passphrase)
2695 if (service->hidden == TRUE)
2697 g_free(service->agent_passphrase);
2698 service->agent_passphrase = g_strdup(agent_passphrase);
2700 if (service->network != NULL)
2701 connman_network_set_string(service->network,
2702 "WiFi.AgentPassphrase",
2703 service->agent_passphrase);
2706 static DBusMessage *get_properties(DBusConnection *conn,
2707 DBusMessage *msg, void *user_data)
2709 struct connman_service *service = user_data;
2711 DBusMessageIter array, dict;
2713 DBG("service %p", service);
2715 reply = dbus_message_new_method_return(msg);
2719 dbus_message_iter_init_append(reply, &array);
2721 connman_dbus_dict_open(&array, &dict);
2722 append_properties(&dict, FALSE, service);
2723 connman_dbus_dict_close(&array, &dict);
2728 static int update_proxy_configuration(struct connman_service *service,
2729 DBusMessageIter *array)
2731 DBusMessageIter dict;
2732 enum connman_service_proxy_method method;
2733 GString *servers_str = NULL;
2734 GString *excludes_str = NULL;
2735 const char *url = NULL;
2737 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2739 dbus_message_iter_recurse(array, &dict);
2741 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2742 DBusMessageIter entry, variant;
2746 dbus_message_iter_recurse(&dict, &entry);
2748 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2751 dbus_message_iter_get_basic(&entry, &key);
2752 dbus_message_iter_next(&entry);
2754 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2757 dbus_message_iter_recurse(&entry, &variant);
2759 type = dbus_message_iter_get_arg_type(&variant);
2761 if (g_str_equal(key, "Method") == TRUE) {
2764 if (type != DBUS_TYPE_STRING)
2767 dbus_message_iter_get_basic(&variant, &val);
2768 method = string2proxymethod(val);
2769 } else if (g_str_equal(key, "URL") == TRUE) {
2770 if (type != DBUS_TYPE_STRING)
2773 dbus_message_iter_get_basic(&variant, &url);
2774 } else if (g_str_equal(key, "Servers") == TRUE) {
2775 DBusMessageIter str_array;
2777 if (type != DBUS_TYPE_ARRAY)
2780 servers_str = g_string_new(NULL);
2781 if (servers_str == NULL)
2784 dbus_message_iter_recurse(&variant, &str_array);
2786 while (dbus_message_iter_get_arg_type(&str_array) ==
2790 dbus_message_iter_get_basic(&str_array, &val);
2792 if (servers_str->len > 0)
2793 g_string_append_printf(servers_str,
2796 g_string_append(servers_str, val);
2798 dbus_message_iter_next(&str_array);
2800 } else if (g_str_equal(key, "Excludes") == TRUE) {
2801 DBusMessageIter str_array;
2803 if (type != DBUS_TYPE_ARRAY)
2806 excludes_str = g_string_new(NULL);
2807 if (excludes_str == NULL)
2810 dbus_message_iter_recurse(&variant, &str_array);
2812 while (dbus_message_iter_get_arg_type(&str_array) ==
2816 dbus_message_iter_get_basic(&str_array, &val);
2818 if (excludes_str->len > 0)
2819 g_string_append_printf(excludes_str,
2822 g_string_append(excludes_str, val);
2824 dbus_message_iter_next(&str_array);
2828 dbus_message_iter_next(&dict);
2832 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2834 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2835 if (servers_str == NULL && service->proxies == NULL)
2838 if (servers_str != NULL) {
2839 g_strfreev(service->proxies);
2841 if (servers_str->len > 0)
2842 service->proxies = g_strsplit_set(
2843 servers_str->str, " ", 0);
2845 service->proxies = NULL;
2848 if (excludes_str != NULL) {
2849 g_strfreev(service->excludes);
2851 if (excludes_str->len > 0)
2852 service->excludes = g_strsplit_set(
2853 excludes_str->str, " ", 0);
2855 service->excludes = NULL;
2858 if (service->proxies == NULL)
2859 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2862 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2863 g_free(service->pac);
2865 if (url != NULL && strlen(url) > 0)
2866 service->pac = g_strdup(url);
2868 service->pac = NULL;
2870 /* if we are connected:
2871 - if service->pac == NULL
2872 - if __connman_ipconfig_get_proxy_autoconfig(
2873 service->ipconfig) == NULL
2874 --> We should start WPAD */
2877 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2881 if (servers_str != NULL)
2882 g_string_free(servers_str, TRUE);
2884 if (excludes_str != NULL)
2885 g_string_free(excludes_str, TRUE);
2887 service->proxy_config = method;
2892 if (servers_str != NULL)
2893 g_string_free(servers_str, TRUE);
2895 if (excludes_str != NULL)
2896 g_string_free(excludes_str, TRUE);
2901 static int set_ipconfig(struct connman_service *service,
2902 struct connman_ipconfig *ipconfig,
2903 DBusMessageIter *array,
2904 enum connman_service_state state,
2905 enum connman_service_state *new_state)
2907 enum connman_ipconfig_method old_method;
2908 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2909 enum connman_ipconfig_type type;
2912 if (ipconfig == NULL)
2915 old_method = __connman_ipconfig_get_method(ipconfig);
2917 if (is_connecting_state(service, state) ||
2918 is_connected_state(service, state))
2919 __connman_network_clear_ipconfig(service->network, ipconfig);
2921 err = __connman_ipconfig_set_config(ipconfig, array);
2922 method = __connman_ipconfig_get_method(ipconfig);
2923 type = __connman_ipconfig_get_config_type(ipconfig);
2925 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2926 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2927 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2928 *new_state = service->state_ipv4 =
2929 CONNMAN_SERVICE_STATE_CONFIGURATION;
2930 __connman_ipconfig_enable(ipconfig);
2933 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2934 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2935 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2936 *new_state = service->state_ipv6;
2937 __connman_ipconfig_enable(ipconfig);
2941 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2942 type, method, state2string(*new_state));
2947 static DBusMessage *set_property(DBusConnection *conn,
2948 DBusMessage *msg, void *user_data)
2950 struct connman_service *service = user_data;
2951 DBusMessageIter iter, value;
2955 DBG("service %p", service);
2957 if (dbus_message_iter_init(msg, &iter) == FALSE)
2958 return __connman_error_invalid_arguments(msg);
2960 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
2961 return __connman_error_invalid_arguments(msg);
2963 dbus_message_iter_get_basic(&iter, &name);
2964 dbus_message_iter_next(&iter);
2966 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
2967 return __connman_error_invalid_arguments(msg);
2969 dbus_message_iter_recurse(&iter, &value);
2971 type = dbus_message_iter_get_arg_type(&value);
2973 if (g_str_equal(name, "AutoConnect") == TRUE) {
2974 connman_bool_t autoconnect;
2976 if (type != DBUS_TYPE_BOOLEAN)
2977 return __connman_error_invalid_arguments(msg);
2979 if (service->favorite == FALSE)
2980 return __connman_error_invalid_service(msg);
2982 dbus_message_iter_get_basic(&value, &autoconnect);
2984 if (service->autoconnect == autoconnect)
2985 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2987 service->autoconnect = autoconnect;
2989 autoconnect_changed(service);
2991 service_save(service);
2992 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2993 DBusMessageIter entry;
2998 if (type != DBUS_TYPE_ARRAY)
2999 return __connman_error_invalid_arguments(msg);
3001 str = g_string_new(NULL);
3003 return __connman_error_invalid_arguments(msg);
3005 index = connman_network_get_index(service->network);
3006 gw = __connman_ipconfig_get_gateway_from_index(index,
3007 CONNMAN_IPCONFIG_TYPE_ALL);
3009 if (gw && strlen(gw))
3010 __connman_service_nameserver_del_routes(service,
3011 CONNMAN_IPCONFIG_TYPE_ALL);
3013 dbus_message_iter_recurse(&value, &entry);
3015 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3017 dbus_message_iter_get_basic(&entry, &val);
3018 dbus_message_iter_next(&entry);
3020 g_string_append_printf(str, " %s", val);
3022 g_string_append(str, val);
3025 remove_nameservers(service, NULL, service->nameservers_config);
3026 g_strfreev(service->nameservers_config);
3029 service->nameservers_config =
3030 g_strsplit_set(str->str, " ", 0);
3032 service->nameservers_config = NULL;
3035 g_string_free(str, TRUE);
3037 if (gw && strlen(gw))
3038 __connman_service_nameserver_add_routes(service, gw);
3040 update_nameservers(service);
3041 dns_configuration_changed(service);
3043 service_save(service);
3044 } else if (g_str_equal(name, "Timeservers.Configuration") == TRUE) {
3045 DBusMessageIter entry;
3046 GSList *list = NULL;
3049 if (type != DBUS_TYPE_ARRAY)
3050 return __connman_error_invalid_arguments(msg);
3052 dbus_message_iter_recurse(&value, &entry);
3054 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3058 dbus_message_iter_get_basic(&entry, &val);
3060 new_head = __connman_timeserver_add_list(list, val);
3061 if (list != new_head) {
3066 dbus_message_iter_next(&entry);
3069 g_strfreev(service->timeservers_config);
3070 service->timeservers_config = NULL;
3073 service->timeservers_config = g_new0(char *, count+1);
3075 while (list != NULL) {
3077 service->timeservers_config[count] = list->data;
3078 list = g_slist_delete_link(list, list);
3082 service_save(service);
3083 timeservers_configuration_changed(service);
3085 __connman_timeserver_sync(service);
3086 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
3087 DBusMessageIter entry;
3090 if (type != DBUS_TYPE_ARRAY)
3091 return __connman_error_invalid_arguments(msg);
3093 str = g_string_new(NULL);
3095 return __connman_error_invalid_arguments(msg);
3097 dbus_message_iter_recurse(&value, &entry);
3099 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
3101 dbus_message_iter_get_basic(&entry, &val);
3102 dbus_message_iter_next(&entry);
3104 g_string_append_printf(str, " %s", val);
3106 g_string_append(str, val);
3109 remove_searchdomains(service, NULL, service->domains);
3110 g_strfreev(service->domains);
3113 service->domains = g_strsplit_set(str->str, " ", 0);
3115 service->domains = NULL;
3117 g_string_free(str, TRUE);
3119 update_nameservers(service);
3120 domain_configuration_changed(service);
3122 service_save(service);
3123 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
3126 if (type != DBUS_TYPE_ARRAY)
3127 return __connman_error_invalid_arguments(msg);
3129 err = update_proxy_configuration(service, &value);
3132 return __connman_error_failed(msg, -err);
3134 proxy_configuration_changed(service);
3136 __connman_notifier_proxy_changed(service);
3138 service_save(service);
3139 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
3140 g_str_equal(name, "IPv6.Configuration")) {
3142 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
3143 enum connman_service_state state =
3144 CONNMAN_SERVICE_STATE_UNKNOWN;
3149 if (service->ipconfig_ipv4 == NULL &&
3150 service->ipconfig_ipv6 == NULL)
3151 return __connman_error_invalid_property(msg);
3153 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
3154 ipv4 = service->ipconfig_ipv4;
3155 err = set_ipconfig(service, ipv4, &value,
3156 service->state_ipv4, &state);
3158 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
3159 ipv6 = service->ipconfig_ipv6;
3160 err = set_ipconfig(service, ipv6, &value,
3161 service->state_ipv6, &state);
3165 if (is_connected_state(service, state) ||
3166 is_connecting_state(service, state))
3167 __connman_network_set_ipconfig(service->network,
3169 return __connman_error_failed(msg, -err);
3173 ipv4_configuration_changed(service);
3175 ipv6_configuration_changed(service);
3177 if (is_connecting(service) || is_connected(service))
3178 __connman_network_set_ipconfig(service->network,
3181 service_save(service);
3183 return __connman_error_invalid_property(msg);
3185 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3188 static void set_idle(struct connman_service *service)
3190 service->state = service->state_ipv4 = service->state_ipv6 =
3191 CONNMAN_SERVICE_STATE_IDLE;
3192 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
3193 state_changed(service);
3196 static DBusMessage *clear_property(DBusConnection *conn,
3197 DBusMessage *msg, void *user_data)
3199 struct connman_service *service = user_data;
3202 DBG("service %p", service);
3204 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
3207 if (g_str_equal(name, "Error") == TRUE) {
3210 g_get_current_time(&service->modified);
3211 service_save(service);
3213 return __connman_error_invalid_property(msg);
3215 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3218 static connman_bool_t is_ignore(struct connman_service *service)
3220 if (service->autoconnect == FALSE)
3223 if (service->roaming == TRUE)
3226 if (service->ignore == TRUE)
3229 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
3235 struct preferred_tech_data {
3236 GSequence *preferred_list;
3237 enum connman_service_type type;
3240 static void preferred_tech_add_by_type(gpointer data, gpointer user_data)
3242 struct connman_service *service = data;
3243 struct preferred_tech_data *tech_data = user_data;
3245 if (service->type == tech_data->type) {
3246 g_sequence_append(tech_data->preferred_list, service);
3248 DBG("type %d service %p %s", tech_data->type, service,
3253 static GSequence* preferred_tech_list_get(GSequence *list)
3255 unsigned int *tech_array;
3256 struct preferred_tech_data tech_data;
3259 tech_array = connman_setting_get_uint_list("PreferredTechnologies");
3260 if (tech_array == NULL)
3263 tech_data.preferred_list = g_sequence_new(NULL);
3265 for (i = 0; tech_array[i] != 0; i += 1) {
3266 tech_data.type = tech_array[i];
3267 g_sequence_foreach(service_list, preferred_tech_add_by_type,
3271 return tech_data.preferred_list;
3274 static connman_bool_t auto_connect_service(GSequenceIter* iter,
3275 connman_bool_t preferred)
3277 struct connman_service *service = NULL;
3279 while (g_sequence_iter_is_end(iter) == FALSE) {
3280 service = g_sequence_get(iter);
3282 if (service->pending != NULL)
3285 if (is_connecting(service) == TRUE)
3288 if (service->favorite == FALSE) {
3289 if (preferred == TRUE)
3294 if (is_connected(service) == TRUE) {
3295 if (preferred == TRUE && service->state !=
3296 CONNMAN_SERVICE_STATE_ONLINE)
3301 if (is_ignore(service) == FALSE && service->state ==
3302 CONNMAN_SERVICE_STATE_IDLE)
3308 iter = g_sequence_iter_next(iter);
3311 if (service != NULL) {
3313 DBG("service %p %s %s", service, service->name,
3314 (preferred == TRUE)? "preferred": "auto");
3316 service->userconnect = FALSE;
3317 __connman_service_connect(service);
3323 static gboolean run_auto_connect(gpointer data)
3325 GSequenceIter *iter = NULL;
3326 GSequence *preferred_tech;
3328 autoconnect_timeout = 0;
3332 preferred_tech = preferred_tech_list_get(service_list);
3333 if (preferred_tech != NULL)
3334 iter = g_sequence_get_begin_iter(preferred_tech);
3336 if (iter == NULL || auto_connect_service(iter, TRUE) == FALSE)
3337 iter = g_sequence_get_begin_iter(service_list);
3340 auto_connect_service(iter, FALSE);
3342 if (preferred_tech != NULL)
3343 g_sequence_free(preferred_tech);
3348 void __connman_service_auto_connect(void)
3352 if (__connman_session_mode() == TRUE) {
3353 DBG("Session mode enabled: auto connect disabled");
3357 if (autoconnect_timeout != 0)
3360 autoconnect_timeout = g_timeout_add_seconds(0, run_auto_connect, NULL);
3363 static void remove_timeout(struct connman_service *service)
3365 if (service->timeout > 0) {
3366 g_source_remove(service->timeout);
3367 service->timeout = 0;
3371 static void reply_pending(struct connman_service *service, int error)
3373 remove_timeout(service);
3375 if (service->pending != NULL) {
3379 reply = __connman_error_failed(service->pending,
3382 g_dbus_send_message(connection, reply);
3386 sender = dbus_message_get_interface(service->pending);
3388 DBG("sender %s", sender);
3390 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
3391 g_dbus_send_reply(connection, service->pending,
3392 DBUS_TYPE_OBJECT_PATH, &service->path,
3395 g_dbus_send_reply(connection, service->pending,
3399 dbus_message_unref(service->pending);
3400 service->pending = NULL;
3404 static gboolean connect_timeout(gpointer user_data)
3406 struct connman_service *service = user_data;
3407 connman_bool_t autoconnect = FALSE;
3409 DBG("service %p", service);
3411 service->timeout = 0;
3413 if (service->network != NULL)
3414 __connman_network_disconnect(service->network);
3416 __connman_ipconfig_disable(service->ipconfig_ipv4);
3417 __connman_ipconfig_disable(service->ipconfig_ipv6);
3419 __connman_stats_service_unregister(service);
3421 if (service->pending != NULL) {
3424 reply = __connman_error_operation_timeout(service->pending);
3426 g_dbus_send_message(connection, reply);
3428 dbus_message_unref(service->pending);
3429 service->pending = NULL;
3433 __connman_service_ipconfig_indicate_state(service,
3434 CONNMAN_SERVICE_STATE_FAILURE,
3435 CONNMAN_IPCONFIG_TYPE_IPV4);
3436 __connman_service_ipconfig_indicate_state(service,
3437 CONNMAN_SERVICE_STATE_FAILURE,
3438 CONNMAN_IPCONFIG_TYPE_IPV6);
3440 if (autoconnect == TRUE && service->userconnect == FALSE)
3441 __connman_service_auto_connect();
3446 static void set_reconnect_state(struct connman_service *service,
3447 connman_bool_t reconnect)
3449 struct connman_device *device;
3451 if (service->network == NULL)
3454 device = connman_network_get_device(service->network);
3458 __connman_device_set_reconnect(device, reconnect);
3461 static connman_bool_t get_reconnect_state(struct connman_service *service)
3463 struct connman_device *device;
3465 if (service->network == NULL)
3468 device = connman_network_get_device(service->network);
3472 return __connman_device_get_reconnect(device);
3475 static DBusMessage *connect_service(DBusConnection *conn,
3476 DBusMessage *msg, void *user_data)
3478 struct connman_service *service = user_data;
3479 GSequenceIter *iter;
3482 DBG("service %p", service);
3484 if (service->pending != NULL)
3485 return __connman_error_in_progress(msg);
3487 iter = g_sequence_get_begin_iter(service_list);
3489 while (g_sequence_iter_is_end(iter) == FALSE) {
3490 struct connman_service *temp = g_sequence_get(iter);
3492 if (service->type == temp->type && is_connecting(temp) == TRUE)
3493 return __connman_error_in_progress(msg);
3495 iter = g_sequence_iter_next(iter);
3498 service->ignore = FALSE;
3500 service->userconnect = TRUE;
3502 service->pending = dbus_message_ref(msg);
3504 set_reconnect_state(service, FALSE);
3506 err = __connman_service_connect(service);
3508 if (service->pending == NULL)
3511 if (err != -EINPROGRESS) {
3512 dbus_message_unref(service->pending);
3513 service->pending = NULL;
3515 return __connman_error_failed(msg, -err);
3521 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3524 static DBusMessage *disconnect_service(DBusConnection *conn,
3525 DBusMessage *msg, void *user_data)
3527 struct connman_service *service = user_data;
3530 DBG("service %p", service);
3532 reply_pending(service, ECONNABORTED);
3534 service->ignore = TRUE;
3536 set_reconnect_state(service, FALSE);
3538 err = __connman_service_disconnect(service);
3540 if (err != -EINPROGRESS)
3541 return __connman_error_failed(msg, -err);
3546 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3549 static DBusMessage *remove_service(DBusConnection *conn,
3550 DBusMessage *msg, void *user_data)
3552 struct connman_service *service = user_data;
3554 DBG("service %p", service);
3556 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
3557 return __connman_error_not_supported(msg);
3559 if (service->immutable == TRUE || service->hidden == TRUE)
3560 return __connman_error_not_supported(msg);
3562 if (service->favorite == FALSE && service->state !=
3563 CONNMAN_SERVICE_STATE_FAILURE)
3564 return __connman_error_not_supported(msg);
3566 set_reconnect_state(service, FALSE);
3568 __connman_service_disconnect(service);
3570 g_free(service->passphrase);
3571 service->passphrase = NULL;
3575 __connman_service_set_favorite(service, FALSE);
3576 service_save(service);
3578 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3581 static gboolean check_suitable_state(enum connman_service_state a,
3582 enum connman_service_state b)
3585 * Special check so that "ready" service can be moved before
3588 if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
3589 b == CONNMAN_SERVICE_STATE_READY) ||
3590 (b == CONNMAN_SERVICE_STATE_ONLINE &&
3591 a == CONNMAN_SERVICE_STATE_READY))
3597 static void downgrade_state(struct connman_service *service)
3599 if (service == NULL)
3602 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
3603 service->state_ipv6);
3605 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
3606 __connman_service_ipconfig_indicate_state(service,
3607 CONNMAN_SERVICE_STATE_READY,
3608 CONNMAN_IPCONFIG_TYPE_IPV4);
3610 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
3611 __connman_service_ipconfig_indicate_state(service,
3612 CONNMAN_SERVICE_STATE_READY,
3613 CONNMAN_IPCONFIG_TYPE_IPV6);
3616 static void apply_relevant_default_downgrade(struct connman_service *service)
3618 struct connman_service *def_service;
3620 def_service = __connman_service_get_default();
3621 if (def_service == NULL)
3624 if (def_service == service &&
3625 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3626 def_service->state = CONNMAN_SERVICE_STATE_READY;
3629 static void switch_default_service(struct connman_service *default_service,
3630 struct connman_service *downgrade_service)
3632 GSequenceIter *src, *dst;
3634 apply_relevant_default_downgrade(default_service);
3635 src = g_hash_table_lookup(service_hash, downgrade_service->identifier);
3636 dst = g_hash_table_lookup(service_hash, default_service->identifier);
3637 g_sequence_move(src, dst);
3638 downgrade_state(downgrade_service);
3641 static DBusMessage *move_service(DBusConnection *conn,
3642 DBusMessage *msg, void *user_data,
3645 struct connman_service *service = user_data;
3646 struct connman_service *target;
3648 enum connman_ipconfig_method target4, target6;
3649 enum connman_ipconfig_method service4, service6;
3651 DBG("service %p", service);
3653 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3656 if (service->favorite == FALSE)
3657 return __connman_error_not_supported(msg);
3659 target = find_service(path);
3660 if (target == NULL || target->favorite == FALSE || target == service)
3661 return __connman_error_invalid_service(msg);
3663 if (target->type == CONNMAN_SERVICE_TYPE_VPN) {
3665 * We only allow VPN route splitting if there are
3666 * routes defined for a given VPN.
3668 if (__connman_provider_check_routes(target->provider)
3670 connman_info("Cannot move service. "
3671 "No routes defined for provider %s",
3672 __connman_provider_get_ident(target->provider));
3673 return __connman_error_invalid_service(msg);
3676 target->do_split_routing = TRUE;
3678 target->do_split_routing = FALSE;
3680 service->do_split_routing = FALSE;
3682 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3683 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3684 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3685 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3687 DBG("target %s method %d/%d state %d/%d split %d", target->identifier,
3688 target4, target6, target->state_ipv4, target->state_ipv6,
3689 target->do_split_routing);
3691 DBG("service %s method %d/%d state %d/%d", service->identifier,
3693 service->state_ipv4, service->state_ipv6);
3696 * If method is OFF, then we do not need to check the corresponding
3699 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3700 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3701 if (check_suitable_state(target->state_ipv6,
3702 service->state_ipv6) == FALSE)
3703 return __connman_error_invalid_service(msg);
3707 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3708 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3709 if (check_suitable_state(target->state_ipv4,
3710 service->state_ipv4) == FALSE)
3711 return __connman_error_invalid_service(msg);
3715 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3716 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3717 if (check_suitable_state(target->state_ipv6,
3718 service->state_ipv6) == FALSE)
3719 return __connman_error_invalid_service(msg);
3723 if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3724 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3725 if (check_suitable_state(target->state_ipv4,
3726 service->state_ipv4) == FALSE)
3727 return __connman_error_invalid_service(msg);
3731 g_get_current_time(&service->modified);
3732 service_save(service);
3733 service_save(target);
3736 * If the service which goes down is the default service and is
3737 * online, we downgrade directly its state to ready so:
3738 * the service which goes up, needs to recompute its state which
3739 * is triggered via downgrading it - if relevant - to state ready.
3742 switch_default_service(target, service);
3744 switch_default_service(service, target);
3746 __connman_connection_update_gateway();
3748 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3751 static DBusMessage *move_before(DBusConnection *conn,
3752 DBusMessage *msg, void *user_data)
3754 return move_service(conn, msg, user_data, TRUE);
3757 static DBusMessage *move_after(DBusConnection *conn,
3758 DBusMessage *msg, void *user_data)
3760 return move_service(conn, msg, user_data, FALSE);
3763 static DBusMessage *reset_counters(DBusConnection *conn,
3764 DBusMessage *msg, void *user_data)
3766 struct connman_service *service = user_data;
3768 reset_stats(service);
3770 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3773 static struct _services_notify {
3779 static void service_append_added_foreach(gpointer data, gpointer user_data)
3781 struct connman_service *service = data;
3782 DBusMessageIter *iter = user_data;
3784 if (service == NULL || service->path == NULL) {
3785 DBG("service %p or path is NULL", service);
3789 if (g_hash_table_lookup(services_notify->add, service->path) != NULL) {
3790 DBG("new %s", service->path);
3792 append_struct(service, iter);
3793 g_hash_table_remove(services_notify->add, service->path);
3795 DBG("changed %s", service->path);
3797 append_struct_service(iter, NULL, service);
3801 static void service_append_ordered(DBusMessageIter *iter, void *user_data)
3803 if (service_list != NULL)
3804 g_sequence_foreach(service_list,
3805 service_append_added_foreach, iter);
3808 static void append_removed(gpointer key, gpointer value, gpointer user_data)
3810 char *objpath = key;
3811 DBusMessageIter *iter = user_data;
3813 DBG("removed %s", objpath);
3814 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &objpath);
3817 static gboolean service_send_changed(gpointer data)
3819 DBusMessage *signal;
3820 DBusMessageIter iter, array;
3824 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
3825 CONNMAN_MANAGER_INTERFACE, "ServicesChanged");
3829 __connman_dbus_append_objpath_dict_array(signal,
3830 service_append_ordered, NULL);
3832 dbus_message_iter_init_append(signal, &iter);
3833 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
3834 DBUS_TYPE_OBJECT_PATH_AS_STRING, &array);
3836 g_hash_table_foreach(services_notify->remove, append_removed, &array);
3838 dbus_message_iter_close_container(&iter, &array);
3840 dbus_connection_send(connection, signal, NULL);
3841 dbus_message_unref(signal);
3843 g_hash_table_remove_all(services_notify->remove);
3844 g_hash_table_remove_all(services_notify->add);
3846 services_notify->id = 0;
3850 static void service_schedule_changed(void)
3852 if (services_notify->id != 0)
3855 services_notify->id = g_timeout_add(100, service_send_changed, NULL);
3858 static void service_schedule_added(struct connman_service *service)
3860 DBG("service %p", service);
3862 g_hash_table_remove(services_notify->remove, service->path);
3863 g_hash_table_replace(services_notify->add, service->path, service);
3865 service_schedule_changed();
3868 static void service_schedule_removed(struct connman_service *service)
3870 DBG("service %p %s", service, service->path);
3872 if (service == NULL || service->path == NULL) {
3873 DBG("service %p or path is NULL", service);
3877 g_hash_table_remove(services_notify->add, service->path);
3878 g_hash_table_replace(services_notify->remove, g_strdup(service->path),
3881 service_schedule_changed();
3884 static const GDBusMethodTable service_methods[] = {
3885 { GDBUS_DEPRECATED_METHOD("GetProperties",
3886 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
3888 { GDBUS_METHOD("SetProperty",
3889 GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
3890 NULL, set_property) },
3891 { GDBUS_METHOD("ClearProperty",
3892 GDBUS_ARGS({ "name", "s" }), NULL,
3894 { GDBUS_ASYNC_METHOD("Connect", NULL, NULL,
3896 { GDBUS_METHOD("Disconnect", NULL, NULL,
3897 disconnect_service) },
3898 { GDBUS_METHOD("Remove", NULL, NULL, remove_service) },
3899 { GDBUS_METHOD("MoveBefore",
3900 GDBUS_ARGS({ "service", "o" }), NULL,
3902 { GDBUS_METHOD("MoveAfter",
3903 GDBUS_ARGS({ "service", "o" }), NULL,
3905 { GDBUS_METHOD("ResetCounters", NULL, NULL, reset_counters) },
3909 static const GDBusSignalTable service_signals[] = {
3910 { GDBUS_SIGNAL("PropertyChanged",
3911 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
3915 static void service_free(gpointer user_data)
3917 struct connman_service *service = user_data;
3918 char *path = service->path;
3920 DBG("service %p", service);
3922 reply_pending(service, ENOENT);
3924 g_hash_table_remove(service_hash, service->identifier);
3926 __connman_notifier_service_remove(service);
3927 service_schedule_removed(service);
3929 stats_stop(service);
3931 service->path = NULL;
3934 __connman_connection_update_gateway();
3936 g_dbus_unregister_interface(connection, path,
3937 CONNMAN_SERVICE_INTERFACE);
3941 g_hash_table_destroy(service->counter_table);
3943 if (service->network != NULL) {
3944 __connman_network_disconnect(service->network);
3945 connman_network_unref(service->network);
3946 service->network = NULL;
3949 if (service->provider != NULL)
3950 connman_provider_unref(service->provider);
3952 if (service->ipconfig_ipv4 != NULL) {
3953 __connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3954 __connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3955 __connman_ipconfig_unref(service->ipconfig_ipv4);
3956 service->ipconfig_ipv4 = NULL;
3959 if (service->ipconfig_ipv6 != NULL) {
3960 __connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3961 __connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3962 __connman_ipconfig_unref(service->ipconfig_ipv6);
3963 service->ipconfig_ipv6 = NULL;
3966 g_strfreev(service->timeservers);
3967 g_strfreev(service->timeservers_config);
3968 g_strfreev(service->nameservers);
3969 g_strfreev(service->nameservers_config);
3970 g_strfreev(service->nameservers_auto);
3971 g_strfreev(service->domains);
3972 g_strfreev(service->proxies);
3973 g_strfreev(service->excludes);
3975 g_free(service->domainname);
3976 g_free(service->pac);
3977 g_free(service->name);
3978 g_free(service->passphrase);
3979 g_free(service->agent_passphrase);
3980 g_free(service->identifier);
3981 g_free(service->eap);
3982 g_free(service->identity);
3983 g_free(service->agent_identity);
3984 g_free(service->ca_cert_file);
3985 g_free(service->client_cert_file);
3986 g_free(service->private_key_file);
3987 g_free(service->private_key_passphrase);
3988 g_free(service->phase2);
3990 if (service->stats.timer != NULL)
3991 g_timer_destroy(service->stats.timer);
3992 if (service->stats_roaming.timer != NULL)
3993 g_timer_destroy(service->stats_roaming.timer);
3998 static void stats_init(struct connman_service *service)
4001 service->stats.valid = FALSE;
4002 service->stats.enabled = FALSE;
4003 service->stats.timer = g_timer_new();
4006 service->stats_roaming.valid = FALSE;
4007 service->stats_roaming.enabled = FALSE;
4008 service->stats_roaming.timer = g_timer_new();
4011 static void service_initialize(struct connman_service *service)
4013 DBG("service %p", service);
4015 service->refcount = 1;
4016 service->session_usage_count = 0;
4018 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
4019 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
4021 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
4022 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
4023 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
4025 service->favorite = FALSE;
4026 service->immutable = FALSE;
4027 service->hidden = FALSE;
4029 service->ignore = FALSE;
4031 service->userconnect = FALSE;
4035 stats_init(service);
4037 service->provider = NULL;
4039 service->wps = FALSE;
4043 * connman_service_create:
4045 * Allocate a new service.
4047 * Returns: a newly-allocated #connman_service structure
4049 struct connman_service *connman_service_create(void)
4052 struct connman_stats_counter *counters;
4053 const char *counter;
4055 struct connman_service *service;
4057 service = g_try_new0(struct connman_service, 1);
4058 if (service == NULL)
4061 DBG("service %p", service);
4063 service->counter_table = g_hash_table_new_full(g_str_hash,
4064 g_str_equal, NULL, g_free);
4066 for (list = counter_list; list; list = list->next) {
4067 counter = list->data;
4069 counters = g_try_new0(struct connman_stats_counter, 1);
4070 if (counters == NULL) {
4071 g_hash_table_destroy(service->counter_table);
4076 counters->append_all = TRUE;
4078 g_hash_table_replace(service->counter_table, (gpointer)counter,
4082 service_initialize(service);
4088 * connman_service_ref:
4089 * @service: service structure
4091 * Increase reference counter of service
4093 struct connman_service *
4094 connman_service_ref_debug(struct connman_service *service,
4095 const char *file, int line, const char *caller)
4097 DBG("%p ref %d by %s:%d:%s()", service, service->refcount + 1,
4098 file, line, caller);
4100 __sync_fetch_and_add(&service->refcount, 1);
4106 * connman_service_unref:
4107 * @service: service structure
4109 * Decrease reference counter of service and release service if no
4112 void connman_service_unref_debug(struct connman_service *service,
4113 const char *file, int line, const char *caller)
4115 GSequenceIter *iter;
4117 DBG("%p ref %d by %s:%d:%s()", service, service->refcount - 1,
4118 file, line, caller);
4120 if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
4123 iter = g_hash_table_lookup(service_hash, service->identifier);
4125 reply_pending(service, ECONNABORTED);
4127 __connman_service_disconnect(service);
4129 g_sequence_remove(iter);
4131 service_free(service);
4135 static gint service_compare(gconstpointer a, gconstpointer b,
4138 struct connman_service *service_a = (void *) a;
4139 struct connman_service *service_b = (void *) b;
4140 enum connman_service_state state_a, state_b;
4142 state_a = service_a->state;
4143 state_b = service_b->state;
4145 if (state_a != state_b) {
4146 gboolean a_connected = is_connected(service_a);
4147 gboolean b_connected = is_connected(service_b);
4149 if (a_connected == TRUE && b_connected == TRUE) {
4150 /* We prefer online over ready state */
4151 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
4154 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
4158 if (a_connected == TRUE)
4160 if (b_connected == TRUE)
4163 if (is_connecting(service_a) == TRUE)
4165 if (is_connecting(service_b) == TRUE)
4169 if (service_a->order > service_b->order)
4172 if (service_a->order < service_b->order)
4175 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
4178 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
4181 if (service_a->type != service_b->type) {
4182 switch (service_a->type) {
4183 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4184 case CONNMAN_SERVICE_TYPE_SYSTEM:
4185 case CONNMAN_SERVICE_TYPE_ETHERNET:
4186 case CONNMAN_SERVICE_TYPE_GPS:
4187 case CONNMAN_SERVICE_TYPE_VPN:
4188 case CONNMAN_SERVICE_TYPE_GADGET:
4190 case CONNMAN_SERVICE_TYPE_WIFI:
4192 case CONNMAN_SERVICE_TYPE_WIMAX:
4193 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4194 case CONNMAN_SERVICE_TYPE_CELLULAR:
4199 return (gint) service_b->strength - (gint) service_a->strength;
4203 * connman_service_get_type:
4204 * @service: service structure
4206 * Get the type of service
4208 enum connman_service_type connman_service_get_type(struct connman_service *service)
4210 if (service == NULL)
4211 return CONNMAN_SERVICE_TYPE_UNKNOWN;
4213 return service->type;
4217 * connman_service_get_interface:
4218 * @service: service structure
4220 * Get network interface of service
4222 char *connman_service_get_interface(struct connman_service *service)
4226 if (service == NULL)
4229 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
4230 if (service->ipconfig_ipv4)
4231 index = __connman_ipconfig_get_index(
4232 service->ipconfig_ipv4);
4233 else if (service->ipconfig_ipv6)
4234 index = __connman_ipconfig_get_index(
4235 service->ipconfig_ipv6);
4239 return connman_inet_ifname(index);
4242 if (service->network == NULL)
4245 index = connman_network_get_index(service->network);
4247 return connman_inet_ifname(index);
4251 * connman_service_get_network:
4252 * @service: service structure
4254 * Get the service network
4256 struct connman_network *
4257 __connman_service_get_network(struct connman_service *service)
4259 if (service == NULL)
4262 return service->network;
4265 struct connman_ipconfig *
4266 __connman_service_get_ip4config(struct connman_service *service)
4268 if (service == NULL)
4271 return service->ipconfig_ipv4;
4274 struct connman_ipconfig *
4275 __connman_service_get_ip6config(struct connman_service *service)
4277 if (service == NULL)
4280 return service->ipconfig_ipv6;
4283 struct connman_ipconfig *
4284 __connman_service_get_ipconfig(struct connman_service *service, int family)
4286 if (family == AF_INET)
4287 return __connman_service_get_ip4config(service);
4288 else if (family == AF_INET6)
4289 return __connman_service_get_ip6config(service);
4295 connman_bool_t __connman_service_is_connected_state(struct connman_service *service,
4296 enum connman_ipconfig_type type)
4298 if (service == NULL)
4302 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
4304 case CONNMAN_IPCONFIG_TYPE_IPV4:
4305 return is_connected_state(service, service->state_ipv4);
4306 case CONNMAN_IPCONFIG_TYPE_IPV6:
4307 return is_connected_state(service, service->state_ipv6);
4312 enum connman_service_security __connman_service_get_security(struct connman_service *service)
4314 if (service == NULL)
4315 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
4317 return service->security;
4320 const char *__connman_service_get_phase2(struct connman_service *service)
4322 if (service == NULL)
4325 return service->phase2;
4328 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
4330 if (service == NULL)
4333 return service->wps;
4337 * __connman_service_set_favorite:
4338 * @service: service structure
4339 * @favorite: favorite value
4341 * Change the favorite setting of service
4343 int __connman_service_set_favorite(struct connman_service *service,
4344 connman_bool_t favorite)
4346 GSequenceIter *iter;
4348 if (service->hidden == TRUE)
4350 iter = g_hash_table_lookup(service_hash, service->identifier);
4354 if (service->favorite == favorite)
4357 service->favorite = favorite;
4358 service->order = __connman_service_get_order(service);
4360 favorite_changed(service);
4362 if (g_sequence_get_length(service_list) > 1) {
4363 g_sequence_sort_changed(iter, service_compare, NULL);
4364 service_schedule_changed();
4367 __connman_connection_update_gateway();
4372 int __connman_service_set_immutable(struct connman_service *service,
4373 connman_bool_t immutable)
4375 if (service->hidden == TRUE)
4377 service->immutable = immutable;
4379 immutable_changed(service);
4384 void __connman_service_set_string(struct connman_service *service,
4385 const char *key, const char *value)
4387 if (service->hidden == TRUE)
4389 if (g_str_equal(key, "EAP") == TRUE) {
4390 g_free(service->eap);
4391 service->eap = g_strdup(value);
4392 } else if (g_str_equal(key, "Identity") == TRUE) {
4393 g_free(service->identity);
4394 service->identity = g_strdup(value);
4395 } else if (g_str_equal(key, "CACertFile") == TRUE) {
4396 g_free(service->ca_cert_file);
4397 service->ca_cert_file = g_strdup(value);
4398 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
4399 g_free(service->client_cert_file);
4400 service->client_cert_file = g_strdup(value);
4401 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
4402 g_free(service->private_key_file);
4403 service->private_key_file = g_strdup(value);
4404 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
4405 g_free(service->private_key_passphrase);
4406 service->private_key_passphrase = g_strdup(value);
4407 } else if (g_str_equal(key, "Phase2") == TRUE) {
4408 g_free(service->phase2);
4409 service->phase2 = g_strdup(value);
4410 } else if (g_str_equal(key, "Passphrase") == TRUE) {
4411 g_free(service->passphrase);
4412 service->passphrase = g_strdup(value);
4416 void __connman_service_set_userconnect(struct connman_service *service,
4417 connman_bool_t userconnect)
4419 if (service != NULL)
4420 service->userconnect = userconnect;
4423 static void service_complete(struct connman_service *service)
4425 reply_pending(service, EIO);
4427 if (service->userconnect == FALSE)
4428 __connman_service_auto_connect();
4430 g_get_current_time(&service->modified);
4431 service_save(service);
4434 static void report_error_cb(struct connman_service *service,
4435 gboolean retry, void *user_data)
4438 __connman_service_connect(service);
4440 /* It is not relevant to stay on Failure state
4441 * when failing is due to wrong user input */
4442 service->state = CONNMAN_SERVICE_STATE_IDLE;
4444 service_complete(service);
4445 __connman_connection_update_gateway();
4449 int __connman_service_add_passphrase(struct connman_service *service,
4450 const gchar *passphrase)
4454 switch (service->security) {
4455 case CONNMAN_SERVICE_SECURITY_WEP:
4456 case CONNMAN_SERVICE_SECURITY_PSK:
4457 err = __connman_service_set_passphrase(service, passphrase);
4459 case CONNMAN_SERVICE_SECURITY_8021X:
4460 __connman_service_set_agent_passphrase(service,
4463 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4464 case CONNMAN_SERVICE_SECURITY_NONE:
4465 case CONNMAN_SERVICE_SECURITY_WPA:
4466 case CONNMAN_SERVICE_SECURITY_RSN:
4467 DBG("service security '%s' (%d) not handled",
4468 security2string(service->security),
4476 static int check_wpspin(struct connman_service *service, const char *wpspin)
4484 length = strlen(wpspin);
4486 /* If 0, it will mean user wants to use PBC method */
4488 connman_network_set_string(service->network,
4489 "WiFi.PinWPS", NULL);
4493 /* A WPS PIN is always 8 chars length,
4494 * its content is in digit representation.
4499 for (i = 0; i < 8; i++)
4500 if (!isdigit((unsigned char) wpspin[i]))
4503 connman_network_set_string(service->network, "WiFi.PinWPS", wpspin);
4508 static void request_input_cb (struct connman_service *service,
4509 connman_bool_t values_received,
4510 const char *name, int name_len,
4511 const char *identity, const char *passphrase,
4512 gboolean wps, const char *wpspin,
4513 const char *error, void *user_data)
4515 struct connman_device *device;
4518 DBG ("RequestInput return, %p", service);
4520 if (error != NULL) {
4521 DBG("error: %s", error);
4523 if (g_strcmp0(error,
4524 "net.connman.Agent.Error.Canceled") == 0) {
4530 if (service->hidden == TRUE && name_len > 0 && name_len <= 32) {
4531 device = connman_network_get_device(service->network);
4532 __connman_device_request_hidden_scan(device,
4534 identity, passphrase);
4537 if (values_received == FALSE || service->hidden == TRUE) {
4542 if (wps == TRUE && service->network != NULL) {
4543 err = check_wpspin(service, wpspin);
4547 connman_network_set_bool(service->network, "WiFi.UseWPS", wps);
4550 if (identity != NULL)
4551 __connman_service_set_agent_identity(service, identity);
4553 if (passphrase != NULL)
4554 err = __connman_service_add_passphrase(service, passphrase);
4558 __connman_service_connect(service);
4560 /* Never cache agent provided credentials */
4561 __connman_service_set_agent_identity(service, NULL);
4562 __connman_service_set_agent_passphrase(service, NULL);
4563 } else if (err == -ENOKEY) {
4564 __connman_service_indicate_error(service,
4565 CONNMAN_SERVICE_ERROR_INVALID_KEY);
4566 __connman_agent_report_error(service,
4567 error2string(service->error),
4568 report_error_cb, NULL);
4570 /* It is not relevant to stay on Failure state
4571 * when failing is due to wrong user input */
4572 service->state = CONNMAN_SERVICE_STATE_IDLE;
4574 service_complete(service);
4575 __connman_connection_update_gateway();
4579 static void downgrade_connected_services(void)
4581 struct connman_service *up_service;
4582 GSequenceIter *iter;
4584 iter = g_sequence_get_begin_iter(service_list);
4585 while (g_sequence_iter_is_end(iter) == FALSE) {
4586 up_service = g_sequence_get(iter);
4588 if (is_connected(up_service) == FALSE) {
4589 iter = g_sequence_iter_next(iter);
4593 if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE)
4596 downgrade_state(up_service);
4598 iter = g_sequence_iter_next(iter);
4602 static int service_update_preferred_order(struct connman_service *default_service,
4603 struct connman_service *new_service,
4604 enum connman_service_state new_state)
4606 unsigned int *tech_array;
4609 if (default_service == NULL || default_service == new_service ||
4610 default_service->state != new_state )
4613 tech_array = connman_setting_get_uint_list("PreferredTechnologies");
4614 if (tech_array != NULL) {
4616 for (i = 0; tech_array[i] != 0; i += 1) {
4617 if (default_service->type == tech_array[i])
4620 if (new_service->type == tech_array[i]) {
4621 switch_default_service(new_service,
4632 static int service_indicate_state(struct connman_service *service)
4634 enum connman_service_state old_state, new_state;
4635 struct connman_service *def_service;
4637 GSequenceIter *iter;
4639 if (service == NULL)
4642 old_state = service->state;
4643 new_state = combine_state(service->state_ipv4, service->state_ipv6);
4645 DBG("service %p old %s - new %s/%s => %s",
4647 state2string(old_state),
4648 state2string(service->state_ipv4),
4649 state2string(service->state_ipv6),
4650 state2string(new_state));
4652 if (old_state == new_state)
4655 def_service = __connman_service_get_default();
4657 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4658 result = service_update_preferred_order(def_service,
4659 service, new_state);
4660 if (result == -EALREADY)
4662 if (result == -EAGAIN)
4663 __connman_service_auto_connect();
4666 if (old_state == CONNMAN_SERVICE_STATE_ONLINE)
4667 __connman_notifier_leave_online(service->type);
4669 service->state = new_state;
4670 state_changed(service);
4672 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
4673 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
4674 reply_pending(service, ECONNABORTED);
4676 __connman_service_disconnect(service);
4679 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
4680 if (service->new_service == FALSE &&
4681 __connman_stats_service_register(service) == 0) {
4683 * For new services the statistics are updated after
4684 * we have successfully connected.
4686 __connman_stats_get(service, FALSE,
4687 &service->stats.data);
4688 __connman_stats_get(service, TRUE,
4689 &service->stats_roaming.data);
4693 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
4694 connman_bool_t reconnect;
4696 reconnect = get_reconnect_state(service);
4697 if (reconnect == TRUE)
4698 __connman_service_auto_connect();
4701 if (new_state == CONNMAN_SERVICE_STATE_READY) {
4702 enum connman_ipconfig_method method;
4704 if (service->new_service == TRUE &&
4705 __connman_stats_service_register(service) == 0) {
4707 * This is normally done after configuring state
4708 * but for new service do this after we have connected
4711 __connman_stats_get(service, FALSE,
4712 &service->stats.data);
4713 __connman_stats_get(service, TRUE,
4714 &service->stats_roaming.data);
4717 service->new_service = FALSE;
4719 service_update_preferred_order(def_service, service, new_state);
4721 set_reconnect_state(service, TRUE);
4723 __connman_service_set_favorite(service, TRUE);
4725 reply_pending(service, 0);
4727 service->userconnect = FALSE;
4729 g_get_current_time(&service->modified);
4730 service_save(service);
4732 update_nameservers(service);
4733 dns_changed(service);
4734 domain_changed(service);
4736 __connman_notifier_connect(service->type);
4738 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4739 connman_network_get_bool(service->network,
4740 "WiFi.UseWPS") == TRUE) {
4743 pass = connman_network_get_string(service->network,
4746 __connman_service_set_passphrase(service, pass);
4748 connman_network_set_bool(service->network,
4749 "WiFi.UseWPS", FALSE);
4754 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4755 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4756 __connman_ipconfig_disable_ipv6(
4757 service->ipconfig_ipv6);
4759 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4760 def_service = __connman_service_get_default();
4762 if (__connman_notifier_is_connected() == FALSE &&
4763 def_service != NULL &&
4764 def_service->provider != NULL)
4765 __connman_provider_disconnect(def_service->provider);
4769 __connman_wispr_stop(service);
4771 __connman_wpad_stop(service);
4773 update_nameservers(service);
4774 dns_changed(service);
4775 domain_changed(service);
4777 __connman_notifier_disconnect(service->type);
4780 * Previous services which are connected and which states
4781 * are set to online should reset relevantly ipconfig_state
4782 * to ready so wispr/portal will be rerun on those
4784 downgrade_connected_services();
4787 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4788 if (service->userconnect == TRUE &&
4789 __connman_agent_report_error(service,
4790 error2string(service->error),
4791 report_error_cb, NULL) == -EINPROGRESS)
4793 service_complete(service);
4795 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4797 iter = g_hash_table_lookup(service_hash, service->identifier);
4798 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
4799 g_sequence_sort_changed(iter, service_compare, NULL);
4800 service_schedule_changed();
4803 __connman_connection_update_gateway();
4805 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4806 __connman_notifier_enter_online(service->type);
4813 int __connman_service_indicate_error(struct connman_service *service,
4814 enum connman_service_error error)
4816 DBG("service %p error %d", service, error);
4818 if (service == NULL)
4821 service->error = error;
4823 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4824 __connman_service_set_passphrase(service, NULL);
4826 __connman_service_ipconfig_indicate_state(service,
4827 CONNMAN_SERVICE_STATE_FAILURE,
4828 CONNMAN_IPCONFIG_TYPE_IPV4);
4829 __connman_service_ipconfig_indicate_state(service,
4830 CONNMAN_SERVICE_STATE_FAILURE,
4831 CONNMAN_IPCONFIG_TYPE_IPV6);
4835 int __connman_service_clear_error(struct connman_service *service)
4837 DBG("service %p", service);
4839 if (service == NULL)
4842 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4845 service->state_ipv4 = service->state_ipv6 =
4846 CONNMAN_SERVICE_STATE_UNKNOWN;
4847 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
4849 if (service->favorite == TRUE)
4850 set_reconnect_state(service, TRUE);
4852 __connman_service_ipconfig_indicate_state(service,
4853 CONNMAN_SERVICE_STATE_IDLE,
4854 CONNMAN_IPCONFIG_TYPE_IPV6);
4857 * Toggling the IPv6 state to IDLE could trigger the auto connect
4858 * machinery and consequently the IPv4 state.
4860 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4861 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4864 return __connman_service_ipconfig_indicate_state(service,
4865 CONNMAN_SERVICE_STATE_IDLE,
4866 CONNMAN_IPCONFIG_TYPE_IPV4);
4869 int __connman_service_indicate_default(struct connman_service *service)
4871 DBG("service %p", service);
4878 enum connman_service_state __connman_service_ipconfig_get_state(
4879 struct connman_service *service,
4880 enum connman_ipconfig_type type)
4882 if (service == NULL)
4883 return CONNMAN_SERVICE_STATE_UNKNOWN;
4885 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4886 return service->state_ipv4;
4888 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4889 return service->state_ipv6;
4891 return CONNMAN_SERVICE_STATE_UNKNOWN;
4894 static void check_proxy_setup(struct connman_service *service)
4897 * We start WPAD if we haven't got a PAC URL from DHCP and
4898 * if our proxy manual configuration is either empty or set
4899 * to AUTO with an empty URL.
4902 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4905 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4906 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4907 service->pac != NULL))
4910 if (__connman_wpad_start(service) < 0) {
4911 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4912 __connman_notifier_proxy_changed(service);
4919 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
4923 * How many networks are connected at the same time. If more than 1,
4924 * then set the rp_filter setting properly (loose mode routing) so that network
4925 * connectivity works ok. This is only done for IPv4 networks as IPv6
4926 * does not have rp_filter knob.
4928 static int connected_networks_count;
4929 static int original_rp_filter;
4931 static void service_rp_filter(struct connman_service *service,
4934 enum connman_ipconfig_method method;
4936 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4939 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
4940 case CONNMAN_IPCONFIG_METHOD_OFF:
4941 case CONNMAN_IPCONFIG_METHOD_AUTO:
4943 case CONNMAN_IPCONFIG_METHOD_FIXED:
4944 case CONNMAN_IPCONFIG_METHOD_MANUAL:
4945 case CONNMAN_IPCONFIG_METHOD_DHCP:
4949 if (connected == TRUE) {
4950 if (connected_networks_count == 1) {
4952 filter_value = __connman_ipconfig_set_rp_filter();
4953 if (filter_value < 0)
4956 original_rp_filter = filter_value;
4958 connected_networks_count++;
4961 if (connected_networks_count == 2)
4962 __connman_ipconfig_unset_rp_filter(original_rp_filter);
4964 connected_networks_count--;
4965 if (connected_networks_count < 0)
4966 connected_networks_count = 0;
4969 DBG("%s %s ipconfig %p method %d count %d filter %d",
4970 connected ? "connected" : "disconnected", service->identifier,
4971 service->ipconfig_ipv4, method,
4972 connected_networks_count, original_rp_filter);
4975 static gboolean redo_wispr(gpointer user_data)
4977 struct connman_service *service = user_data;
4981 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV6);
4986 int __connman_service_online_check_failed(struct connman_service *service,
4987 enum connman_ipconfig_type type)
4989 DBG("service %p type %d count %d", service, type,
4990 service->online_check_count);
4992 /* currently we only retry IPv6 stuff */
4993 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 ||
4994 service->online_check_count != 1) {
4995 connman_warn("Online check failed for %p %s", service,
5000 service->online_check_count = 0;
5003 * We set the timeout to 1 sec so that we have a chance to get
5004 * necessary IPv6 router advertisement messages that might have
5007 g_timeout_add_seconds(1, redo_wispr, service);
5012 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
5013 enum connman_service_state new_state,
5014 enum connman_ipconfig_type type)
5016 struct connman_ipconfig *ipconfig = NULL;
5017 enum connman_service_state old_state;
5020 if (service == NULL)
5023 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
5024 old_state = service->state_ipv4;
5025 ipconfig = service->ipconfig_ipv4;
5026 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
5027 old_state = service->state_ipv6;
5028 ipconfig = service->ipconfig_ipv6;
5031 if (ipconfig == NULL)
5035 if (old_state == new_state)
5038 DBG("service %p (%s) state %d (%s) type %d (%s)",
5039 service, service ? service->identifier : NULL,
5040 new_state, state2string(new_state),
5041 type, __connman_ipconfig_type2string(type));
5043 switch (new_state) {
5044 case CONNMAN_SERVICE_STATE_UNKNOWN:
5045 case CONNMAN_SERVICE_STATE_IDLE:
5046 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
5049 case CONNMAN_SERVICE_STATE_ASSOCIATION:
5051 case CONNMAN_SERVICE_STATE_CONFIGURATION:
5052 __connman_ipconfig_enable(ipconfig);
5054 case CONNMAN_SERVICE_STATE_READY:
5055 update_nameservers(service);
5057 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
5058 check_proxy_setup(service);
5059 service_rp_filter(service, TRUE);
5061 service->online_check_count = 1;
5062 __connman_wispr_start(service, type);
5065 case CONNMAN_SERVICE_STATE_ONLINE:
5067 case CONNMAN_SERVICE_STATE_DISCONNECT:
5068 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
5071 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
5072 service_rp_filter(service, FALSE);
5075 case CONNMAN_SERVICE_STATE_FAILURE:
5079 /* We keep that state */
5080 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
5081 service->state_ipv4 = new_state;
5082 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
5083 service->state_ipv6 = new_state;
5085 ret = service_indicate_state(service);
5088 * If the ipconfig method is OFF, then we set the state to IDLE
5089 * so that it will not affect the combined state in the future.
5091 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
5092 enum connman_ipconfig_method method;
5093 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
5094 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
5095 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
5096 service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
5097 ret = service_indicate_state(service);
5100 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
5101 enum connman_ipconfig_method method;
5102 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
5103 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
5104 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
5105 service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5106 ret = service_indicate_state(service);
5113 static connman_bool_t prepare_network(struct connman_service *service)
5115 enum connman_network_type type;
5116 unsigned int ssid_len;
5118 type = connman_network_get_type(service->network);
5121 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5122 case CONNMAN_NETWORK_TYPE_VENDOR:
5124 case CONNMAN_NETWORK_TYPE_WIFI:
5125 if (connman_network_get_blob(service->network, "WiFi.SSID",
5129 if (service->passphrase != NULL)
5130 connman_network_set_string(service->network,
5131 "WiFi.Passphrase", service->passphrase);
5133 case CONNMAN_NETWORK_TYPE_ETHERNET:
5134 case CONNMAN_NETWORK_TYPE_WIMAX:
5135 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5136 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5137 case CONNMAN_NETWORK_TYPE_CELLULAR:
5144 static void prepare_8021x(struct connman_service *service)
5146 if (service->eap != NULL)
5147 connman_network_set_string(service->network, "WiFi.EAP",
5150 if (service->identity != NULL)
5151 connman_network_set_string(service->network, "WiFi.Identity",
5154 if (service->ca_cert_file != NULL)
5155 connman_network_set_string(service->network, "WiFi.CACertFile",
5156 service->ca_cert_file);
5158 if (service->client_cert_file != NULL)
5159 connman_network_set_string(service->network,
5160 "WiFi.ClientCertFile",
5161 service->client_cert_file);
5163 if (service->private_key_file != NULL)
5164 connman_network_set_string(service->network,
5165 "WiFi.PrivateKeyFile",
5166 service->private_key_file);
5168 if (service->private_key_passphrase != NULL)
5169 connman_network_set_string(service->network,
5170 "WiFi.PrivateKeyPassphrase",
5171 service->private_key_passphrase);
5173 if (service->phase2 != NULL)
5174 connman_network_set_string(service->network, "WiFi.Phase2",
5178 static int service_connect(struct connman_service *service)
5182 if (service->hidden == TRUE)
5185 switch (service->type) {
5186 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5187 case CONNMAN_SERVICE_TYPE_SYSTEM:
5188 case CONNMAN_SERVICE_TYPE_GPS:
5189 case CONNMAN_SERVICE_TYPE_GADGET:
5191 case CONNMAN_SERVICE_TYPE_ETHERNET:
5192 case CONNMAN_SERVICE_TYPE_WIMAX:
5193 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5194 case CONNMAN_SERVICE_TYPE_CELLULAR:
5195 case CONNMAN_SERVICE_TYPE_VPN:
5197 case CONNMAN_SERVICE_TYPE_WIFI:
5198 switch (service->security) {
5199 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
5200 case CONNMAN_SERVICE_SECURITY_NONE:
5202 case CONNMAN_SERVICE_SECURITY_WEP:
5203 case CONNMAN_SERVICE_SECURITY_PSK:
5204 case CONNMAN_SERVICE_SECURITY_WPA:
5205 case CONNMAN_SERVICE_SECURITY_RSN:
5206 if (service->passphrase == NULL) {
5207 if (service->network == NULL)
5210 if (service->wps == FALSE ||
5211 connman_network_get_bool(
5213 "WiFi.UseWPS") == FALSE)
5217 case CONNMAN_SERVICE_SECURITY_8021X:
5218 if (service->eap == NULL)
5222 * never request credentials if using EAP-TLS
5223 * (EAP-TLS networks need to be fully provisioned)
5225 if (g_str_equal(service->eap, "tls") == TRUE)
5229 * Return -ENOKEY if either identity or passphrase is
5230 * missing. Agent provided credentials can be used as
5231 * fallback if needed.
5233 if ((service->identity == NULL &&
5234 service->agent_identity == NULL) ||
5235 (service->passphrase == NULL &&
5236 service->agent_passphrase == NULL))
5244 if (service->network != NULL) {
5245 if (prepare_network(service) == FALSE)
5248 switch (service->security) {
5249 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
5250 case CONNMAN_SERVICE_SECURITY_NONE:
5251 case CONNMAN_SERVICE_SECURITY_WEP:
5252 case CONNMAN_SERVICE_SECURITY_PSK:
5253 case CONNMAN_SERVICE_SECURITY_WPA:
5254 case CONNMAN_SERVICE_SECURITY_RSN:
5256 case CONNMAN_SERVICE_SECURITY_8021X:
5257 prepare_8021x(service);
5261 if (__connman_stats_service_register(service) == 0) {
5262 __connman_stats_get(service, FALSE,
5263 &service->stats.data);
5264 __connman_stats_get(service, TRUE,
5265 &service->stats_roaming.data);
5268 if (service->ipconfig_ipv4)
5269 __connman_ipconfig_enable(service->ipconfig_ipv4);
5270 if (service->ipconfig_ipv6)
5271 __connman_ipconfig_enable(service->ipconfig_ipv6);
5273 err = __connman_network_connect(service->network);
5274 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5275 service->provider != NULL)
5276 err = __connman_provider_connect(service->provider);
5281 if (err != -EINPROGRESS) {
5282 __connman_ipconfig_disable(service->ipconfig_ipv4);
5283 __connman_ipconfig_disable(service->ipconfig_ipv6);
5284 __connman_stats_service_unregister(service);
5292 int __connman_service_connect(struct connman_service *service)
5296 DBG("service %p state %s", service, state2string(service->state));
5298 if (is_connected(service) == TRUE)
5301 if (is_connecting(service) == TRUE)
5304 switch (service->type) {
5305 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5306 case CONNMAN_SERVICE_TYPE_SYSTEM:
5307 case CONNMAN_SERVICE_TYPE_GPS:
5308 case CONNMAN_SERVICE_TYPE_GADGET:
5311 err = service_connect(service);
5317 if (err == -EINPROGRESS) {
5318 if (service->timeout == 0)
5319 service->timeout = g_timeout_add_seconds(
5320 CONNECT_TIMEOUT, connect_timeout, service);
5322 return -EINPROGRESS;
5325 if (service->network != NULL)
5326 __connman_network_disconnect(service->network);
5327 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5328 service->provider != NULL)
5329 __connman_provider_disconnect(service->provider);
5331 if (service->userconnect == TRUE) {
5332 if (err == -ENOKEY || err == -EPERM) {
5333 return __connman_agent_request_passphrase_input(service,
5334 request_input_cb, NULL);
5336 reply_pending(service, -err);
5342 int __connman_service_disconnect(struct connman_service *service)
5346 DBG("service %p", service);
5348 if (service->network != NULL) {
5349 err = __connman_network_disconnect(service->network);
5350 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5351 service->provider != NULL)
5352 err = __connman_provider_disconnect(service->provider);
5356 if (err < 0 && err != -EINPROGRESS)
5359 __connman_6to4_remove(service->ipconfig_ipv4);
5361 if (service->ipconfig_ipv4)
5362 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
5365 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
5368 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
5369 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
5371 __connman_ipconfig_disable(service->ipconfig_ipv4);
5372 __connman_ipconfig_disable(service->ipconfig_ipv6);
5374 __connman_stats_service_unregister(service);
5379 int __connman_service_disconnect_all(void)
5381 GSequenceIter *iter;
5382 GSList *services = NULL, *list;
5386 iter = g_sequence_get_begin_iter(service_list);
5388 while (g_sequence_iter_is_end(iter) == FALSE) {
5389 struct connman_service *service = g_sequence_get(iter);
5391 services = g_slist_prepend(services, service);
5393 iter = g_sequence_iter_next(iter);
5396 for (list = services; list != NULL; list = list->next) {
5397 struct connman_service *service = list->data;
5399 service->ignore = TRUE;
5401 set_reconnect_state(service, FALSE);
5403 __connman_service_disconnect(service);
5413 * lookup_by_identifier:
5414 * @identifier: service identifier
5416 * Look up a service by identifier (reference count will not be increased)
5418 static struct connman_service *lookup_by_identifier(const char *identifier)
5420 GSequenceIter *iter;
5422 iter = g_hash_table_lookup(service_hash, identifier);
5424 return g_sequence_get(iter);
5429 static void provision_changed(gpointer value, gpointer user_data)
5431 struct connman_service *service = value;
5432 char *path = user_data;
5434 __connman_config_provision_service_ident(service, path);
5437 void __connman_service_provision_changed(const char *ident)
5439 g_sequence_foreach(service_list, provision_changed, (void *)ident);
5443 * __connman_service_get:
5444 * @identifier: service identifier
5446 * Look up a service by identifier or create a new one if not found
5448 static struct connman_service *service_get(const char *identifier)
5450 struct connman_service *service;
5451 GSequenceIter *iter;
5453 iter = g_hash_table_lookup(service_hash, identifier);
5455 service = g_sequence_get(iter);
5456 if (service != NULL)
5457 connman_service_ref(service);
5461 service = connman_service_create();
5462 if (service == NULL)
5465 DBG("service %p", service);
5467 service->identifier = g_strdup(identifier);
5469 iter = g_sequence_insert_sorted(service_list, service,
5470 service_compare, NULL);
5472 g_hash_table_insert(service_hash, service->identifier, iter);
5477 static int service_register(struct connman_service *service)
5479 GSequenceIter *iter;
5481 DBG("service %p", service);
5483 if (service->path != NULL)
5486 service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
5487 service->identifier);
5489 DBG("path %s", service->path);
5491 __connman_config_provision_service(service);
5493 service_load(service);
5495 g_dbus_register_interface(connection, service->path,
5496 CONNMAN_SERVICE_INTERFACE,
5497 service_methods, service_signals,
5498 NULL, service, NULL);
5500 iter = g_hash_table_lookup(service_hash, service->identifier);
5501 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
5502 g_sequence_sort_changed(iter, service_compare, NULL);
5503 service_schedule_changed();
5506 __connman_connection_update_gateway();
5511 static void service_up(struct connman_ipconfig *ipconfig)
5513 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5515 DBG("%s up", __connman_ipconfig_get_ifname(ipconfig));
5517 link_changed(service);
5519 service->stats.valid = FALSE;
5520 service->stats_roaming.valid = FALSE;
5523 static void service_down(struct connman_ipconfig *ipconfig)
5525 DBG("%s down", __connman_ipconfig_get_ifname(ipconfig));
5528 static void service_lower_up(struct connman_ipconfig *ipconfig)
5530 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5532 DBG("%s lower up", __connman_ipconfig_get_ifname(ipconfig));
5534 stats_start(service);
5537 static void service_lower_down(struct connman_ipconfig *ipconfig)
5539 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5541 DBG("%s lower down", __connman_ipconfig_get_ifname(ipconfig));
5543 if (is_idle_state(service, service->state_ipv4) == FALSE)
5544 __connman_ipconfig_disable(service->ipconfig_ipv4);
5546 if (is_idle_state(service, service->state_ipv6) == FALSE)
5547 __connman_ipconfig_disable(service->ipconfig_ipv6);
5549 stats_stop(service);
5550 service_save(service);
5553 static void service_ip_bound(struct connman_ipconfig *ipconfig)
5555 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5556 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5557 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5559 DBG("%s ip bound", __connman_ipconfig_get_ifname(ipconfig));
5561 type = __connman_ipconfig_get_config_type(ipconfig);
5562 method = __connman_ipconfig_get_method(ipconfig);
5564 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5567 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5568 method == CONNMAN_IPCONFIG_METHOD_AUTO)
5569 __connman_service_ipconfig_indicate_state(service,
5570 CONNMAN_SERVICE_STATE_READY,
5571 CONNMAN_IPCONFIG_TYPE_IPV6);
5573 settings_changed(service, ipconfig);
5576 static void service_ip_release(struct connman_ipconfig *ipconfig)
5578 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
5579 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5580 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5582 DBG("%s ip release", __connman_ipconfig_get_ifname(ipconfig));
5584 type = __connman_ipconfig_get_config_type(ipconfig);
5585 method = __connman_ipconfig_get_method(ipconfig);
5587 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5590 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5591 method == CONNMAN_IPCONFIG_METHOD_OFF)
5592 __connman_service_ipconfig_indicate_state(service,
5593 CONNMAN_SERVICE_STATE_DISCONNECT,
5594 CONNMAN_IPCONFIG_TYPE_IPV6);
5596 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
5597 method == CONNMAN_IPCONFIG_METHOD_OFF)
5598 __connman_service_ipconfig_indicate_state(service,
5599 CONNMAN_SERVICE_STATE_DISCONNECT,
5600 CONNMAN_IPCONFIG_TYPE_IPV4);
5602 settings_changed(service, ipconfig);
5605 static const struct connman_ipconfig_ops service_ops = {
5607 .down = service_down,
5608 .lower_up = service_lower_up,
5609 .lower_down = service_lower_down,
5610 .ip_bound = service_ip_bound,
5611 .ip_release = service_ip_release,
5614 static void setup_ip4config(struct connman_service *service, int index,
5615 enum connman_ipconfig_method method)
5617 service->ipconfig_ipv4 = __connman_ipconfig_create(index,
5618 CONNMAN_IPCONFIG_TYPE_IPV4);
5619 if (service->ipconfig_ipv4 == NULL)
5622 __connman_ipconfig_set_method(service->ipconfig_ipv4, method);
5624 __connman_ipconfig_set_data(service->ipconfig_ipv4, service);
5626 __connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
5629 static void setup_ip6config(struct connman_service *service, int index)
5631 service->ipconfig_ipv6 = __connman_ipconfig_create(index,
5632 CONNMAN_IPCONFIG_TYPE_IPV6);
5633 if (service->ipconfig_ipv6 == NULL)
5636 __connman_ipconfig_set_data(service->ipconfig_ipv6, service);
5638 __connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
5641 void __connman_service_read_ip4config(struct connman_service *service)
5645 if (service->ipconfig_ipv4 == NULL)
5648 keyfile = connman_storage_load_service(service->identifier);
5649 if (keyfile == NULL)
5652 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5653 service->identifier, "IPv4.");
5655 g_key_file_free(keyfile);
5658 void __connman_service_create_ip4config(struct connman_service *service,
5661 DBG("ipv4 %p", service->ipconfig_ipv4);
5663 if (service->ipconfig_ipv4 != NULL)
5666 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5667 __connman_service_read_ip4config(service);
5670 void __connman_service_read_ip6config(struct connman_service *service)
5674 if (service->ipconfig_ipv6 == NULL)
5677 keyfile = connman_storage_load_service(service->identifier);
5678 if (keyfile == NULL)
5681 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5682 service->identifier, "IPv6.");
5684 g_key_file_free(keyfile);
5687 void __connman_service_create_ip6config(struct connman_service *service,
5690 DBG("ipv6 %p", service->ipconfig_ipv6);
5692 if (service->ipconfig_ipv6 != NULL)
5695 setup_ip6config(service, index);
5697 __connman_service_read_ip6config(service);
5701 * __connman_service_lookup_from_network:
5702 * @network: network structure
5704 * Look up a service by network (reference count will not be increased)
5706 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
5708 struct connman_service *service;
5709 const char *ident, *group;
5712 DBG("network %p", network);
5714 if (network == NULL)
5717 ident = __connman_network_get_ident(network);
5721 group = connman_network_get_group(network);
5725 name = g_strdup_printf("%s_%s_%s",
5726 __connman_network_get_type(network), ident, group);
5727 service = lookup_by_identifier(name);
5733 struct connman_service *__connman_service_lookup_from_index(int index)
5735 struct connman_service *service;
5736 GSequenceIter *iter;
5738 iter = g_sequence_get_begin_iter(service_list);
5740 while (g_sequence_iter_is_end(iter) == FALSE) {
5741 service = g_sequence_get(iter);
5743 if (__connman_ipconfig_get_index(service->ipconfig_ipv4)
5747 if (__connman_ipconfig_get_index(service->ipconfig_ipv6)
5751 iter = g_sequence_iter_next(iter);
5757 const char *__connman_service_get_ident(struct connman_service *service)
5759 return service->identifier;
5762 const char *__connman_service_get_path(struct connman_service *service)
5764 return service->path;
5767 unsigned int __connman_service_get_order(struct connman_service *service)
5769 GSequenceIter *iter;
5771 if (service == NULL)
5774 if (service->favorite == FALSE) {
5779 iter = g_hash_table_lookup(service_hash, service->identifier);
5781 if (g_sequence_iter_get_position(iter) == 0)
5783 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
5784 service->do_split_routing == FALSE)
5785 service->order = 10;
5790 DBG("service %p name %s order %d split %d", service, service->name,
5791 service->order, service->do_split_routing);
5794 return service->order;
5797 void __connman_service_update_ordering(void)
5799 GSequenceIter *iter;
5801 iter = g_sequence_get_begin_iter(service_list);
5802 if (iter != NULL && g_sequence_get_length(service_list) > 1)
5803 g_sequence_sort_changed(iter, service_compare, NULL);
5806 static enum connman_service_type convert_network_type(struct connman_network *network)
5808 enum connman_network_type type = connman_network_get_type(network);
5811 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5812 case CONNMAN_NETWORK_TYPE_VENDOR:
5814 case CONNMAN_NETWORK_TYPE_ETHERNET:
5815 return CONNMAN_SERVICE_TYPE_ETHERNET;
5816 case CONNMAN_NETWORK_TYPE_WIFI:
5817 return CONNMAN_SERVICE_TYPE_WIFI;
5818 case CONNMAN_NETWORK_TYPE_WIMAX:
5819 return CONNMAN_SERVICE_TYPE_WIMAX;
5820 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5821 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5822 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5823 case CONNMAN_NETWORK_TYPE_CELLULAR:
5824 return CONNMAN_SERVICE_TYPE_CELLULAR;
5827 return CONNMAN_SERVICE_TYPE_UNKNOWN;
5830 static enum connman_service_security convert_wifi_security(const char *security)
5832 if (security == NULL)
5833 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5834 else if (g_str_equal(security, "none") == TRUE)
5835 return CONNMAN_SERVICE_SECURITY_NONE;
5836 else if (g_str_equal(security, "wep") == TRUE)
5837 return CONNMAN_SERVICE_SECURITY_WEP;
5838 else if (g_str_equal(security, "psk") == TRUE)
5839 return CONNMAN_SERVICE_SECURITY_PSK;
5840 else if (g_str_equal(security, "ieee8021x") == TRUE)
5841 return CONNMAN_SERVICE_SECURITY_8021X;
5842 else if (g_str_equal(security, "wpa") == TRUE)
5843 return CONNMAN_SERVICE_SECURITY_WPA;
5844 else if (g_str_equal(security, "rsn") == TRUE)
5845 return CONNMAN_SERVICE_SECURITY_RSN;
5847 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5850 static void update_from_network(struct connman_service *service,
5851 struct connman_network *network)
5853 connman_uint8_t strength = service->strength;
5854 GSequenceIter *iter;
5857 DBG("service %p network %p", service, network);
5859 if (is_connected(service) == TRUE)
5862 if (is_connecting(service) == TRUE)
5865 str = connman_network_get_string(network, "Name");
5867 g_free(service->name);
5868 service->name = g_strdup(str);
5869 service->hidden = FALSE;
5871 g_free(service->name);
5872 service->name = NULL;
5873 service->hidden = TRUE;
5876 service->strength = connman_network_get_strength(network);
5877 service->roaming = connman_network_get_bool(network, "Roaming");
5879 if (service->strength == 0) {
5881 * Filter out 0-values; it's unclear what they mean
5882 * and they cause anomalous sorting of the priority list.
5884 service->strength = strength;
5887 str = connman_network_get_string(network, "WiFi.Security");
5888 service->security = convert_wifi_security(str);
5890 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5891 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5893 if (service->strength > strength && service->network != NULL) {
5894 connman_network_unref(service->network);
5895 service->network = connman_network_ref(network);
5897 strength_changed(service);
5900 if (service->network == NULL)
5901 service->network = connman_network_ref(network);
5903 iter = g_hash_table_lookup(service_hash, service->identifier);
5904 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
5905 g_sequence_sort_changed(iter, service_compare, NULL);
5906 service_schedule_changed();
5911 * __connman_service_create_from_network:
5912 * @network: network structure
5914 * Look up service by network and if not found, create one
5916 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5918 struct connman_service *service;
5919 struct connman_device *device;
5920 const char *ident, *group;
5922 unsigned int *auto_connect_types;
5925 DBG("network %p", network);
5927 if (network == NULL)
5930 ident = __connman_network_get_ident(network);
5934 group = connman_network_get_group(network);
5938 name = g_strdup_printf("%s_%s_%s",
5939 __connman_network_get_type(network), ident, group);
5940 service = service_get(name);
5943 if (service == NULL)
5946 if (__connman_network_get_weakness(network) == TRUE)
5949 if (service->path != NULL) {
5950 update_from_network(service, network);
5951 __connman_connection_update_gateway();
5955 service->type = convert_network_type(network);
5957 auto_connect_types = connman_setting_get_uint_list("DefaultAutoConnectTechnologies");
5958 service->autoconnect = FALSE;
5959 for (i = 0; auto_connect_types != NULL &&
5960 auto_connect_types[i] != 0; i++) {
5961 if (service->type == auto_connect_types[i]) {
5962 service->autoconnect = TRUE;
5967 switch (service->type) {
5968 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5969 case CONNMAN_SERVICE_TYPE_SYSTEM:
5970 case CONNMAN_SERVICE_TYPE_WIMAX:
5971 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5972 case CONNMAN_SERVICE_TYPE_GPS:
5973 case CONNMAN_SERVICE_TYPE_VPN:
5974 case CONNMAN_SERVICE_TYPE_GADGET:
5975 case CONNMAN_SERVICE_TYPE_WIFI:
5976 case CONNMAN_SERVICE_TYPE_CELLULAR:
5978 case CONNMAN_SERVICE_TYPE_ETHERNET:
5979 service->favorite = TRUE;
5983 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5984 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5986 update_from_network(service, network);
5988 index = connman_network_get_index(network);
5990 if (service->ipconfig_ipv4 == NULL)
5991 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5993 if (service->ipconfig_ipv6 == NULL)
5994 setup_ip6config(service, index);
5996 service_register(service);
5998 if (service->favorite == TRUE) {
5999 device = connman_network_get_device(service->network);
6000 if (device && connman_device_get_scanning(device) == FALSE)
6001 __connman_service_auto_connect();
6004 __connman_notifier_service_add(service, service->name);
6005 service_schedule_added(service);
6010 void __connman_service_update_from_network(struct connman_network *network)
6012 connman_bool_t need_sort = FALSE;
6013 struct connman_service *service;
6014 connman_uint8_t strength;
6015 connman_bool_t roaming;
6016 GSequenceIter *iter;
6018 connman_bool_t stats_enable;
6020 DBG("network %p", network);
6022 service = __connman_service_lookup_from_network(network);
6023 if (service == NULL)
6026 if (service->network == NULL)
6029 name = connman_network_get_string(service->network, "Name");
6030 if (g_strcmp0(service->name, name) != 0) {
6031 g_free(service->name);
6032 service->name = g_strdup(name);
6033 connman_dbus_property_changed_basic(service->path,
6034 CONNMAN_SERVICE_INTERFACE, "Name",
6035 DBUS_TYPE_STRING, &service->name);
6038 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
6039 service->wps = connman_network_get_bool(network, "WiFi.WPS");
6041 strength = connman_network_get_strength(service->network);
6042 if (strength == service->strength)
6045 service->strength = strength;
6048 strength_changed(service);
6051 roaming = connman_network_get_bool(service->network, "Roaming");
6052 if (roaming == service->roaming)
6055 stats_enable = stats_enabled(service);
6056 if (stats_enable == TRUE)
6057 stats_stop(service);
6059 service->roaming = roaming;
6062 if (stats_enable == TRUE)
6063 stats_start(service);
6065 roaming_changed(service);
6068 if (need_sort == TRUE) {
6069 iter = g_hash_table_lookup(service_hash, service->identifier);
6070 if (iter != NULL && g_sequence_get_length(service_list) > 1) {
6071 g_sequence_sort_changed(iter, service_compare, NULL);
6072 service_schedule_changed();
6077 void __connman_service_remove_from_network(struct connman_network *network)
6079 struct connman_service *service;
6081 DBG("network %p", network);
6083 service = __connman_service_lookup_from_network(network);
6084 if (service == NULL)
6087 service->ignore = TRUE;
6089 __connman_connection_gateway_remove(service,
6090 CONNMAN_IPCONFIG_TYPE_ALL);
6092 connman_service_unref(service);
6096 * __connman_service_create_from_provider:
6097 * @provider: provider structure
6099 * Look up service by provider and if not found, create one
6101 struct connman_service *
6102 __connman_service_create_from_provider(struct connman_provider *provider)
6104 struct connman_service *service;
6105 const char *ident, *str;
6107 int index = connman_provider_get_index(provider);
6109 DBG("provider %p", provider);
6111 ident = __connman_provider_get_ident(provider);
6115 name = g_strdup_printf("vpn_%s", ident);
6116 service = service_get(name);
6119 if (service == NULL)
6122 service->type = CONNMAN_SERVICE_TYPE_VPN;
6123 service->provider = connman_provider_ref(provider);
6124 service->autoconnect = FALSE;
6125 service->userconnect = TRUE;
6127 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
6128 service->state = combine_state(service->state_ipv4, service->state_ipv6);
6130 str = connman_provider_get_string(provider, "Name");
6132 g_free(service->name);
6133 service->name = g_strdup(str);
6134 service->hidden = FALSE;
6136 g_free(service->name);
6137 service->name = NULL;
6138 service->hidden = TRUE;
6141 service->strength = 0;
6143 if (service->ipconfig_ipv4 == NULL)
6144 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
6146 if (service->ipconfig_ipv6 == NULL)
6147 setup_ip6config(service, index);
6149 service_register(service);
6151 __connman_notifier_service_add(service, service->name);
6152 service_schedule_added(service);
6157 int __connman_service_init(void)
6161 connection = connman_dbus_get_connection();
6163 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
6166 service_list = g_sequence_new(service_free);
6168 services_notify = g_new0(struct _services_notify, 1);
6169 services_notify->remove = g_hash_table_new_full(g_str_hash,
6170 g_str_equal, g_free, NULL);
6171 services_notify->add = g_hash_table_new(g_str_hash, g_str_equal);
6176 void __connman_service_cleanup(void)
6182 if (autoconnect_timeout != 0) {
6183 g_source_remove(autoconnect_timeout);
6184 autoconnect_timeout = 0;
6187 list = service_list;
6188 service_list = NULL;
6189 g_sequence_free(list);
6191 g_hash_table_destroy(service_hash);
6192 service_hash = NULL;
6194 g_slist_free(counter_list);
6195 counter_list = NULL;
6197 if (services_notify->id != 0) {
6198 g_source_remove(services_notify->id);
6199 service_send_changed(NULL);
6200 g_hash_table_destroy(services_notify->remove);
6201 g_hash_table_destroy(services_notify->add);
6203 g_free(services_notify);
6205 dbus_connection_unref(connection);