5 * Copyright (C) 2007-2010 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
32 #include <connman/storage.h>
36 #define CONNECT_TIMEOUT 120
38 static DBusConnection *connection = NULL;
40 static GSequence *service_list = NULL;
41 static GHashTable *service_hash = NULL;
42 static GSList *counter_list = NULL;
44 struct connman_stats {
46 connman_bool_t enabled;
47 struct connman_stats_data data_last;
48 struct connman_stats_data data;
52 struct connman_stats_counter {
53 connman_bool_t append_all;
54 struct connman_stats stats;
55 struct connman_stats stats_roaming;
58 struct connman_service {
60 int session_usage_count;
63 enum connman_service_type type;
64 enum connman_service_security security;
65 enum connman_service_state state;
66 enum connman_service_state state_ipv4;
67 enum connman_service_state state_ipv6;
68 enum connman_service_error error;
69 connman_uint8_t strength;
70 connman_bool_t favorite;
71 connman_bool_t immutable;
72 connman_bool_t hidden;
73 connman_bool_t ignore;
74 connman_bool_t autoconnect;
75 connman_bool_t userconnect;
80 char *agent_passphrase;
81 connman_bool_t roaming;
82 struct connman_ipconfig *ipconfig_ipv4;
83 struct connman_ipconfig *ipconfig_ipv6;
84 struct connman_network *network;
85 struct connman_provider *provider;
87 char **nameservers_config;
88 char **nameservers_auto;
92 /* 802.1x settings from the config files */
97 char *client_cert_file;
98 char *private_key_file;
99 char *private_key_passphrase;
101 DBusMessage *pending;
103 struct connman_stats stats;
104 struct connman_stats stats_roaming;
105 GHashTable *counter_table;
106 enum connman_service_proxy_method proxy;
107 enum connman_service_proxy_method proxy_config;
112 int online_check_count;
115 static void append_path(gpointer value, gpointer user_data)
117 struct connman_service *service = value;
118 DBusMessageIter *iter = user_data;
120 if (service->path == NULL)
123 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
127 void __connman_service_list(DBusMessageIter *iter, void *user_data)
129 if (service_list == NULL)
132 g_sequence_foreach(service_list, append_path, iter);
137 struct connman_service *service;
140 static void compare_path(gpointer value, gpointer user_data)
142 struct connman_service *service = value;
143 struct find_data *data = user_data;
145 if (data->service != NULL)
148 if (g_strcmp0(service->path, data->path) == 0)
149 data->service = service;
152 static struct connman_service *find_service(const char *path)
154 struct find_data data = { .path = path, .service = NULL };
156 DBG("path %s", path);
158 g_sequence_foreach(service_list, compare_path, &data);
163 const char *__connman_service_type2string(enum connman_service_type type)
166 case CONNMAN_SERVICE_TYPE_UNKNOWN:
168 case CONNMAN_SERVICE_TYPE_SYSTEM:
170 case CONNMAN_SERVICE_TYPE_ETHERNET:
172 case CONNMAN_SERVICE_TYPE_WIFI:
174 case CONNMAN_SERVICE_TYPE_WIMAX:
176 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
178 case CONNMAN_SERVICE_TYPE_CELLULAR:
180 case CONNMAN_SERVICE_TYPE_GPS:
182 case CONNMAN_SERVICE_TYPE_VPN:
184 case CONNMAN_SERVICE_TYPE_GADGET:
191 static const char *security2string(enum connman_service_security security)
194 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
196 case CONNMAN_SERVICE_SECURITY_NONE:
198 case CONNMAN_SERVICE_SECURITY_WEP:
200 case CONNMAN_SERVICE_SECURITY_PSK:
201 case CONNMAN_SERVICE_SECURITY_WPA:
202 case CONNMAN_SERVICE_SECURITY_RSN:
204 case CONNMAN_SERVICE_SECURITY_8021X:
211 static const char *state2string(enum connman_service_state state)
214 case CONNMAN_SERVICE_STATE_UNKNOWN:
216 case CONNMAN_SERVICE_STATE_IDLE:
218 case CONNMAN_SERVICE_STATE_ASSOCIATION:
219 return "association";
220 case CONNMAN_SERVICE_STATE_CONFIGURATION:
221 return "configuration";
222 case CONNMAN_SERVICE_STATE_READY:
224 case CONNMAN_SERVICE_STATE_ONLINE:
226 case CONNMAN_SERVICE_STATE_DISCONNECT:
228 case CONNMAN_SERVICE_STATE_FAILURE:
235 static const char *error2string(enum connman_service_error error)
238 case CONNMAN_SERVICE_ERROR_UNKNOWN:
240 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
241 return "out-of-range";
242 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
243 return "pin-missing";
244 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
245 return "dhcp-failed";
246 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
247 return "connect-failed";
248 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
249 return "login-failed";
250 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
251 return "auth-failed";
252 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
253 return "invalid-key";
259 static enum connman_service_error string2error(const char *error)
261 if (g_strcmp0(error, "dhcp-failed") == 0)
262 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
263 else if (g_strcmp0(error, "pin-missing") == 0)
264 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
265 else if (g_strcmp0(error, "invalid-key") == 0)
266 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
268 return CONNMAN_SERVICE_ERROR_UNKNOWN;
271 static const char *proxymethod2string(enum connman_service_proxy_method method)
274 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
276 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
278 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
280 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
287 static enum connman_service_proxy_method string2proxymethod(const char *method)
289 if (g_strcmp0(method, "direct") == 0)
290 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
291 else if (g_strcmp0(method, "auto") == 0)
292 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
293 else if (g_strcmp0(method, "manual") == 0)
294 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
296 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
299 static int service_load(struct connman_service *service)
302 GError *error = NULL;
305 connman_bool_t autoconnect;
306 unsigned int ssid_len;
309 DBG("service %p", service);
311 keyfile = connman_storage_load_service(service->identifier);
315 switch (service->type) {
316 case CONNMAN_SERVICE_TYPE_UNKNOWN:
317 case CONNMAN_SERVICE_TYPE_SYSTEM:
318 case CONNMAN_SERVICE_TYPE_ETHERNET:
319 case CONNMAN_SERVICE_TYPE_GPS:
320 case CONNMAN_SERVICE_TYPE_VPN:
321 case CONNMAN_SERVICE_TYPE_GADGET:
323 case CONNMAN_SERVICE_TYPE_WIFI:
324 if (service->name == NULL) {
327 name = g_key_file_get_string(keyfile,
328 service->identifier, "Name", NULL);
330 g_free(service->name);
331 service->name = name;
334 if (service->network != NULL)
335 connman_network_set_name(service->network,
339 if (service->network &&
340 connman_network_get_blob(service->network,
341 "WiFi.SSID", &ssid_len) == NULL) {
344 hex_ssid = g_key_file_get_string(keyfile,
348 if (hex_ssid != NULL) {
350 unsigned int i, j = 0, hex;
351 size_t hex_ssid_len = strlen(hex_ssid);
353 ssid = g_try_malloc0(hex_ssid_len / 2);
360 for (i = 0; i < hex_ssid_len; i += 2) {
361 sscanf(hex_ssid + i, "%02x", &hex);
365 connman_network_set_blob(service->network,
366 "WiFi.SSID", ssid, hex_ssid_len / 2);
373 case CONNMAN_SERVICE_TYPE_WIMAX:
374 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
375 case CONNMAN_SERVICE_TYPE_CELLULAR:
376 service->favorite = g_key_file_get_boolean(keyfile,
377 service->identifier, "Favorite", NULL);
379 autoconnect = g_key_file_get_boolean(keyfile,
380 service->identifier, "AutoConnect", &error);
382 service->autoconnect = autoconnect;
383 g_clear_error(&error);
385 str = g_key_file_get_string(keyfile,
386 service->identifier, "Failure", NULL);
388 if (service->favorite == FALSE)
389 service->state_ipv4 = service->state_ipv6 =
390 CONNMAN_SERVICE_STATE_FAILURE;
391 service->error = string2error(str);
397 str = g_key_file_get_string(keyfile,
398 service->identifier, "Modified", NULL);
400 g_time_val_from_iso8601(str, &service->modified);
404 str = g_key_file_get_string(keyfile,
405 service->identifier, "Passphrase", NULL);
407 g_free(service->passphrase);
408 service->passphrase = str;
411 if (service->ipconfig_ipv4 != NULL)
412 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
413 service->identifier, "IPv4.");
415 if (service->ipconfig_ipv6 != NULL)
416 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
417 service->identifier, "IPv6.");
419 service->nameservers_config = g_key_file_get_string_list(keyfile,
420 service->identifier, "Nameservers", &length, NULL);
421 if (service->nameservers_config != NULL && length == 0) {
422 g_strfreev(service->nameservers_config);
423 service->nameservers_config = NULL;
426 service->domains = g_key_file_get_string_list(keyfile,
427 service->identifier, "Domains", &length, NULL);
428 if (service->domains != NULL && length == 0) {
429 g_strfreev(service->domains);
430 service->domains = NULL;
433 str = g_key_file_get_string(keyfile,
434 service->identifier, "Proxy.Method", NULL);
436 service->proxy_config = string2proxymethod(str);
440 service->proxies = g_key_file_get_string_list(keyfile,
441 service->identifier, "Proxy.Servers", &length, NULL);
442 if (service->proxies != NULL && length == 0) {
443 g_strfreev(service->proxies);
444 service->proxies = NULL;
447 service->excludes = g_key_file_get_string_list(keyfile,
448 service->identifier, "Proxy.Excludes", &length, NULL);
449 if (service->excludes != NULL && length == 0) {
450 g_strfreev(service->excludes);
451 service->excludes = NULL;
454 str = g_key_file_get_string(keyfile,
455 service->identifier, "Proxy.URL", NULL);
457 g_free(service->pac);
462 g_key_file_free(keyfile);
467 static int service_save(struct connman_service *service)
472 const char *cst_str = NULL;
475 DBG("service %p", service);
477 keyfile = __connman_storage_open_service(service->identifier);
481 if (service->name != NULL)
482 g_key_file_set_string(keyfile, service->identifier,
483 "Name", service->name);
485 switch (service->type) {
486 case CONNMAN_SERVICE_TYPE_UNKNOWN:
487 case CONNMAN_SERVICE_TYPE_SYSTEM:
488 case CONNMAN_SERVICE_TYPE_ETHERNET:
489 case CONNMAN_SERVICE_TYPE_GPS:
490 case CONNMAN_SERVICE_TYPE_VPN:
491 case CONNMAN_SERVICE_TYPE_GADGET:
493 case CONNMAN_SERVICE_TYPE_WIFI:
494 if (service->network) {
495 const unsigned char *ssid;
496 unsigned int ssid_len = 0;
498 ssid = connman_network_get_blob(service->network,
499 "WiFi.SSID", &ssid_len);
501 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
502 char *identifier = service->identifier;
506 str = g_string_sized_new(ssid_len * 2);
512 for (i = 0; i < ssid_len; i++)
513 g_string_append_printf(str,
516 g_key_file_set_string(keyfile, identifier,
519 g_string_free(str, TRUE);
522 freq = connman_network_get_frequency(service->network);
523 g_key_file_set_integer(keyfile, service->identifier,
528 case CONNMAN_SERVICE_TYPE_WIMAX:
529 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
530 case CONNMAN_SERVICE_TYPE_CELLULAR:
531 g_key_file_set_boolean(keyfile, service->identifier,
532 "Favorite", service->favorite);
534 if (service->favorite == TRUE)
535 g_key_file_set_boolean(keyfile, service->identifier,
536 "AutoConnect", service->autoconnect);
538 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
539 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
540 const char *failure = error2string(service->error);
542 g_key_file_set_string(keyfile,
546 g_key_file_remove_key(keyfile, service->identifier,
552 str = g_time_val_to_iso8601(&service->modified);
554 g_key_file_set_string(keyfile, service->identifier,
559 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
560 g_key_file_set_string(keyfile, service->identifier,
561 "Passphrase", service->passphrase);
563 g_key_file_remove_key(keyfile, service->identifier,
566 if (service->ipconfig_ipv4 != NULL)
567 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
568 service->identifier, "IPv4.");
570 if (service->ipconfig_ipv6 != NULL)
571 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
572 service->identifier, "IPv6.");
574 if (service->nameservers_config != NULL) {
575 guint len = g_strv_length(service->nameservers_config);
577 g_key_file_set_string_list(keyfile, service->identifier,
579 (const gchar **) service->nameservers_config, len);
581 g_key_file_remove_key(keyfile, service->identifier,
582 "Nameservers", NULL);
584 if (service->domains != NULL) {
585 guint len = g_strv_length(service->domains);
587 g_key_file_set_string_list(keyfile, service->identifier,
589 (const gchar **) service->domains, len);
591 g_key_file_remove_key(keyfile, service->identifier,
594 cst_str = proxymethod2string(service->proxy_config);
596 g_key_file_set_string(keyfile, service->identifier,
597 "Proxy.Method", cst_str);
599 if (service->proxies != NULL) {
600 guint len = g_strv_length(service->proxies);
602 g_key_file_set_string_list(keyfile, service->identifier,
604 (const gchar **) service->proxies, len);
606 g_key_file_remove_key(keyfile, service->identifier,
607 "Proxy.Servers", NULL);
609 if (service->excludes != NULL) {
610 guint len = g_strv_length(service->excludes);
612 g_key_file_set_string_list(keyfile, service->identifier,
614 (const gchar **) service->excludes, len);
616 g_key_file_remove_key(keyfile, service->identifier,
617 "Proxy.Excludes", NULL);
619 if (service->pac != NULL && strlen(service->pac) > 0)
620 g_key_file_set_string(keyfile, service->identifier,
621 "Proxy.URL", service->pac);
623 g_key_file_remove_key(keyfile, service->identifier,
627 __connman_storage_save_service(keyfile, service->identifier);
629 g_key_file_free(keyfile);
634 static guint changed_timeout = 0;
636 static gboolean notify_services_changed(gpointer user_data)
640 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
641 CONNMAN_MANAGER_INTERFACE, "Services",
642 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
648 static void services_changed(gboolean delayed)
652 if (changed_timeout > 0) {
653 g_source_remove(changed_timeout);
657 if (__connman_connection_update_gateway() == TRUE) {
658 notify_services_changed(NULL);
662 if (delayed == FALSE) {
663 notify_services_changed(NULL);
667 changed_timeout = g_timeout_add_seconds(1, notify_services_changed,
671 static enum connman_service_state combine_state(
672 enum connman_service_state state_a,
673 enum connman_service_state state_b)
675 enum connman_service_state result;
677 if (state_a == state_b) {
682 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
687 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
692 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
697 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
702 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
707 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
712 if (state_a == CONNMAN_SERVICE_STATE_READY) {
717 if (state_b == CONNMAN_SERVICE_STATE_READY) {
722 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
727 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
732 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
737 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
742 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
747 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
752 result = CONNMAN_SERVICE_STATE_FAILURE;
758 static connman_bool_t is_connecting_state(struct connman_service *service,
759 enum connman_service_state state)
762 case CONNMAN_SERVICE_STATE_UNKNOWN:
763 case CONNMAN_SERVICE_STATE_IDLE:
764 case CONNMAN_SERVICE_STATE_FAILURE:
765 if (service->network != NULL)
766 return connman_network_get_connecting(service->network);
767 case CONNMAN_SERVICE_STATE_DISCONNECT:
768 case CONNMAN_SERVICE_STATE_READY:
769 case CONNMAN_SERVICE_STATE_ONLINE:
771 case CONNMAN_SERVICE_STATE_ASSOCIATION:
772 case CONNMAN_SERVICE_STATE_CONFIGURATION:
779 static connman_bool_t is_connected_state(const struct connman_service *service,
780 enum connman_service_state state)
783 case CONNMAN_SERVICE_STATE_UNKNOWN:
784 case CONNMAN_SERVICE_STATE_IDLE:
785 case CONNMAN_SERVICE_STATE_ASSOCIATION:
786 case CONNMAN_SERVICE_STATE_CONFIGURATION:
787 case CONNMAN_SERVICE_STATE_DISCONNECT:
788 case CONNMAN_SERVICE_STATE_FAILURE:
790 case CONNMAN_SERVICE_STATE_READY:
791 case CONNMAN_SERVICE_STATE_ONLINE:
798 static connman_bool_t is_connecting(struct connman_service *service)
800 return is_connecting_state(service, service->state);
803 static connman_bool_t is_connected(struct connman_service *service)
805 return is_connected_state(service, service->state);
808 static void update_nameservers(struct connman_service *service)
812 if (service->ipconfig_ipv4)
813 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv4);
814 else if (service->ipconfig_ipv6)
815 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv6);
822 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
823 case CONNMAN_SERVICE_STATE_UNKNOWN:
824 case CONNMAN_SERVICE_STATE_IDLE:
825 case CONNMAN_SERVICE_STATE_ASSOCIATION:
826 case CONNMAN_SERVICE_STATE_CONFIGURATION:
828 case CONNMAN_SERVICE_STATE_FAILURE:
829 case CONNMAN_SERVICE_STATE_DISCONNECT:
830 connman_resolver_remove_all(ifname);
832 case CONNMAN_SERVICE_STATE_READY:
833 case CONNMAN_SERVICE_STATE_ONLINE:
837 if (service->nameservers_config != NULL) {
840 for (i = 0; service->nameservers_config[i] != NULL; i++) {
841 connman_resolver_append(ifname, NULL,
842 service->nameservers_config[i]);
844 } else if (service->nameservers != NULL) {
847 for (i = 0; service->nameservers[i] != NULL; i++) {
848 connman_resolver_append(ifname, NULL,
849 service->nameservers[i]);
853 if (service->domains != NULL) {
856 for (i = 0; service->domains[i]; i++)
857 connman_resolver_append(ifname, service->domains[i],
859 } else if (service->domainname != NULL)
860 connman_resolver_append(ifname, service->domainname, NULL);
862 connman_resolver_flush();
866 * The is_auto variable is set to true when IPv6 autoconf nameservers are
867 * inserted to resolver via netlink message (see rtnl.c:rtnl_newnduseropt()
868 * for details) and not through service.c
870 int __connman_service_nameserver_append(struct connman_service *service,
871 const char *nameserver, gboolean is_auto)
876 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
878 if (nameserver == NULL)
882 nameservers = service->nameservers_auto;
884 nameservers = service->nameservers;
886 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
887 if (g_strcmp0(nameservers[i], nameserver) == 0)
890 if (nameservers != NULL) {
891 len = g_strv_length(nameservers);
892 nameservers = g_try_renew(char *, nameservers, len + 2);
895 nameservers = g_try_new0(char *, len + 2);
898 if (nameservers == NULL)
901 nameservers[len] = g_strdup(nameserver);
902 if (nameservers[len] == NULL)
905 nameservers[len + 1] = NULL;
907 if (is_auto == TRUE) {
908 service->nameservers_auto = nameservers;
910 service->nameservers = nameservers;
911 update_nameservers(service);
917 int __connman_service_nameserver_remove(struct connman_service *service,
918 const char *nameserver, gboolean is_auto)
920 char **servers, **nameservers;
921 gboolean found = FALSE;
924 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
926 if (nameserver == NULL)
930 nameservers = service->nameservers_auto;
932 nameservers = service->nameservers;
934 if (nameservers == NULL)
937 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
938 if (g_strcmp0(nameservers[i], nameserver) == 0) {
946 len = g_strv_length(nameservers);
949 g_strfreev(nameservers);
951 service->nameservers_auto = NULL;
953 service->nameservers = NULL;
958 servers = g_try_new0(char *, len);
962 for (i = 0, j = 0; i < len; i++) {
963 if (g_strcmp0(nameservers[i], nameserver) != 0) {
964 servers[j] = g_strdup(nameservers[i]);
965 if (servers[j] == NULL)
970 servers[len - 1] = NULL;
972 g_strfreev(nameservers);
973 nameservers = servers;
975 if (is_auto == TRUE) {
976 service->nameservers_auto = nameservers;
978 service->nameservers = nameservers;
979 update_nameservers(service);
985 void __connman_service_nameserver_clear(struct connman_service *service)
987 g_strfreev(service->nameservers);
988 service->nameservers = NULL;
990 update_nameservers(service);
993 static void nameserver_add_routes(int index, char **nameservers,
997 struct addrinfo hints;
998 struct addrinfo *addr;
1000 for (i = 0; nameservers[i] != NULL; i++) {
1001 memset(&hints, 0, sizeof(struct addrinfo));
1002 hints.ai_flags = AI_NUMERICHOST;
1005 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1006 if (ret == EAI_NONAME)
1007 family = AF_INET; /* use the IPv4 as a default */
1011 family = addr->ai_family;
1013 if (family == AF_INET) {
1014 if (connman_inet_compare_subnet(index,
1015 nameservers[i]) != TRUE)
1016 connman_inet_add_host_route(index,
1017 nameservers[i], gw);
1018 } else if (family == AF_INET6)
1019 connman_inet_add_ipv6_host_route(index,
1020 nameservers[i], gw);
1026 static void nameserver_del_routes(int index, char **nameservers)
1029 struct addrinfo hints;
1030 struct addrinfo *addr;
1032 for (i = 0; nameservers[i] != NULL; i++) {
1033 memset(&hints, 0, sizeof(struct addrinfo));
1034 hints.ai_flags = AI_NUMERICHOST;
1037 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1038 if (ret == EAI_NONAME)
1039 family = AF_INET; /* use the IPv4 as a default */
1043 family = addr->ai_family;
1045 if (family == AF_INET)
1046 connman_inet_del_host_route(index, nameservers[i]);
1047 else if (family == AF_INET6)
1048 connman_inet_del_ipv6_host_route(index,
1055 void __connman_service_nameserver_add_routes(struct connman_service *service,
1060 if (service == NULL)
1063 if (service->network != NULL)
1064 index = connman_network_get_index(service->network);
1065 else if (service->provider != NULL)
1066 index = connman_provider_get_index(service->provider);
1068 if (service->nameservers_config != NULL) {
1070 * Configured nameserver takes preference over the
1071 * discoverd nameserver gathered from DHCP, VPN, etc.
1073 nameserver_add_routes(index, service->nameservers_config, gw);
1074 } else if (service->nameservers != NULL) {
1076 * We add nameservers host routes for nameservers that
1077 * are not on our subnet. For those who are, the subnet
1078 * route will be installed by the time the dns proxy code
1079 * tries to reach them. The subnet route is installed
1080 * when setting the interface IP address.
1082 nameserver_add_routes(index, service->nameservers, gw);
1086 void __connman_service_nameserver_del_routes(struct connman_service *service)
1090 if (service == NULL)
1093 if (service->network != NULL)
1094 index = connman_network_get_index(service->network);
1095 else if (service->provider != NULL)
1096 index = connman_provider_get_index(service->provider);
1098 if (service->nameservers_config != NULL)
1099 nameserver_del_routes(index, service->nameservers_config);
1100 else if (service->nameservers != NULL)
1101 nameserver_del_routes(index, service->nameservers);
1104 static struct connman_stats *stats_get(struct connman_service *service)
1106 if (service->roaming == TRUE)
1107 return &service->stats_roaming;
1109 return &service->stats;
1112 static connman_bool_t stats_enabled(struct connman_service *service)
1114 struct connman_stats *stats = stats_get(service);
1116 return stats->enabled;
1119 static void stats_start(struct connman_service *service)
1121 struct connman_stats *stats = stats_get(service);
1123 DBG("service %p", service);
1125 if (stats->timer == NULL)
1128 stats->enabled = TRUE;
1129 stats->data_last.time = stats->data.time;
1131 g_timer_start(stats->timer);
1134 static void stats_stop(struct connman_service *service)
1136 struct connman_stats *stats = stats_get(service);
1137 unsigned int seconds;
1139 DBG("service %p", service);
1141 if (stats->timer == NULL)
1144 if (stats->enabled == FALSE)
1147 g_timer_stop(stats->timer);
1149 seconds = g_timer_elapsed(stats->timer, NULL);
1150 stats->data.time = stats->data_last.time + seconds;
1152 stats->enabled = FALSE;
1155 static void reset_stats(struct connman_service *service)
1157 DBG("service %p", service);
1160 service->stats.valid = FALSE;
1162 service->stats.data.rx_packets = 0;
1163 service->stats.data.tx_packets = 0;
1164 service->stats.data.rx_bytes = 0;
1165 service->stats.data.tx_bytes = 0;
1166 service->stats.data.rx_errors = 0;
1167 service->stats.data.tx_errors = 0;
1168 service->stats.data.rx_dropped = 0;
1169 service->stats.data.tx_dropped = 0;
1170 service->stats.data.time = 0;
1171 service->stats.data_last.time = 0;
1173 g_timer_reset(service->stats.timer);
1176 service->stats_roaming.valid = FALSE;
1178 service->stats_roaming.data.rx_packets = 0;
1179 service->stats_roaming.data.tx_packets = 0;
1180 service->stats_roaming.data.rx_bytes = 0;
1181 service->stats_roaming.data.tx_bytes = 0;
1182 service->stats_roaming.data.rx_errors = 0;
1183 service->stats_roaming.data.tx_errors = 0;
1184 service->stats_roaming.data.rx_dropped = 0;
1185 service->stats_roaming.data.tx_dropped = 0;
1186 service->stats_roaming.data.time = 0;
1187 service->stats_roaming.data_last.time = 0;
1189 g_timer_reset(service->stats_roaming.timer);
1192 static struct connman_service *get_default(void)
1194 struct connman_service *service;
1195 GSequenceIter *iter;
1197 iter = g_sequence_get_begin_iter(service_list);
1199 if (g_sequence_iter_is_end(iter) == TRUE)
1202 service = g_sequence_get(iter);
1204 if (is_connected(service) == FALSE)
1210 static void default_changed(void)
1212 struct connman_service *service = get_default();
1214 __connman_notifier_default_changed(service);
1217 static void state_changed(struct connman_service *service)
1221 __connman_notifier_service_state_changed(service, service->state);
1223 str = state2string(service->state);
1227 connman_dbus_property_changed_basic(service->path,
1228 CONNMAN_SERVICE_INTERFACE, "State",
1229 DBUS_TYPE_STRING, &str);
1232 static void strength_changed(struct connman_service *service)
1234 if (service->strength == 0)
1237 connman_dbus_property_changed_basic(service->path,
1238 CONNMAN_SERVICE_INTERFACE, "Strength",
1239 DBUS_TYPE_BYTE, &service->strength);
1242 static void favorite_changed(struct connman_service *service)
1244 if (service->path == NULL)
1247 connman_dbus_property_changed_basic(service->path,
1248 CONNMAN_SERVICE_INTERFACE, "Favorite",
1249 DBUS_TYPE_BOOLEAN, &service->favorite);
1252 static void immutable_changed(struct connman_service *service)
1254 if (service->path == NULL)
1257 connman_dbus_property_changed_basic(service->path,
1258 CONNMAN_SERVICE_INTERFACE, "Immutable",
1259 DBUS_TYPE_BOOLEAN, &service->immutable);
1262 static void roaming_changed(struct connman_service *service)
1264 if (service->path == NULL)
1267 connman_dbus_property_changed_basic(service->path,
1268 CONNMAN_SERVICE_INTERFACE, "Roaming",
1269 DBUS_TYPE_BOOLEAN, &service->roaming);
1272 static void autoconnect_changed(struct connman_service *service)
1274 if (service->path == NULL)
1277 connman_dbus_property_changed_basic(service->path,
1278 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
1279 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1282 static void append_security(DBusMessageIter *iter, void *user_data)
1284 struct connman_service *service = user_data;
1287 str = security2string(service->security);
1289 dbus_message_iter_append_basic(iter,
1290 DBUS_TYPE_STRING, &str);
1293 * Some access points incorrectly advertise WPS even when they
1294 * are configured as open or no security, so filter
1297 if (service->wps == TRUE) {
1298 switch (service->security) {
1299 case CONNMAN_SERVICE_SECURITY_PSK:
1300 case CONNMAN_SERVICE_SECURITY_WPA:
1301 case CONNMAN_SERVICE_SECURITY_RSN:
1303 dbus_message_iter_append_basic(iter,
1304 DBUS_TYPE_STRING, &str);
1306 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1307 case CONNMAN_SERVICE_SECURITY_NONE:
1308 case CONNMAN_SERVICE_SECURITY_WEP:
1309 case CONNMAN_SERVICE_SECURITY_8021X:
1315 static void append_ethernet(DBusMessageIter *iter, void *user_data)
1317 struct connman_service *service = user_data;
1319 if (service->ipconfig_ipv4 != NULL)
1320 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
1322 else if (service->ipconfig_ipv6 != NULL)
1323 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
1327 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1329 struct connman_service *service = user_data;
1331 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1332 state2string(service->state_ipv4));
1334 if (is_connected_state(service, service->state_ipv4) == FALSE)
1337 if (service->ipconfig_ipv4 != NULL)
1338 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1341 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1343 struct connman_service *service = user_data;
1345 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1346 state2string(service->state_ipv6));
1348 if (is_connected_state(service, service->state_ipv6) == FALSE)
1351 if (service->ipconfig_ipv6 != NULL)
1352 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1353 service->ipconfig_ipv4);
1356 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1358 struct connman_service *service = user_data;
1360 if (service->ipconfig_ipv4 != NULL)
1361 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1365 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1367 struct connman_service *service = user_data;
1369 if (service->ipconfig_ipv6 != NULL)
1370 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1374 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1379 servers = *nameservers;
1381 for (i = 0; servers[i] != NULL; i++) {
1382 dbus_message_iter_append_basic(iter,
1383 DBUS_TYPE_STRING, &servers[i]);
1387 static void append_dns(DBusMessageIter *iter, void *user_data)
1389 struct connman_service *service = user_data;
1391 if (is_connected(service) == FALSE)
1394 if (service->nameservers_config != NULL) {
1395 append_nameserver(iter, &service->nameservers_config);
1398 if (service->nameservers != NULL)
1399 append_nameserver(iter, &service->nameservers);
1401 if (service->nameservers_auto != NULL)
1402 append_nameserver(iter, &service->nameservers_auto);
1406 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1408 struct connman_service *service = user_data;
1411 if (service->nameservers_config == NULL)
1414 for (i = 0; service->nameservers_config[i]; i++) {
1415 dbus_message_iter_append_basic(iter,
1417 &service->nameservers_config[i]);
1421 static void append_domain(DBusMessageIter *iter, void *user_data)
1423 struct connman_service *service = user_data;
1425 if (is_connected(service) == FALSE &&
1426 is_connecting(service) == FALSE)
1429 if (service->domainname == NULL)
1432 dbus_message_iter_append_basic(iter,
1433 DBUS_TYPE_STRING, &service->domainname);
1436 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1438 struct connman_service *service = user_data;
1441 if (service->domains == NULL)
1444 for (i = 0; service->domains[i]; i++)
1445 dbus_message_iter_append_basic(iter,
1446 DBUS_TYPE_STRING, &service->domains[i]);
1449 static void append_proxies(DBusMessageIter *iter, void *user_data)
1451 struct connman_service *service = user_data;
1454 if (service->proxies == NULL)
1457 for (i = 0; service->proxies[i]; i++)
1458 dbus_message_iter_append_basic(iter,
1459 DBUS_TYPE_STRING, &service->proxies[i]);
1462 static void append_excludes(DBusMessageIter *iter, void *user_data)
1464 struct connman_service *service = user_data;
1467 if (service->excludes == NULL)
1470 for (i = 0; service->excludes[i]; i++)
1471 dbus_message_iter_append_basic(iter,
1472 DBUS_TYPE_STRING, &service->excludes[i]);
1475 static void append_proxy(DBusMessageIter *iter, void *user_data)
1477 struct connman_service *service = user_data;
1478 enum connman_service_proxy_method proxy;
1479 const char *pac = NULL;
1480 const char *method = proxymethod2string(
1481 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1485 if (is_connected(service) == FALSE)
1488 proxy = connman_service_get_proxy_method(service);
1491 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1493 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1495 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1496 connman_dbus_dict_append_array(iter, "Servers",
1497 DBUS_TYPE_STRING, append_proxies,
1500 connman_dbus_dict_append_array(iter, "Excludes",
1501 DBUS_TYPE_STRING, append_excludes,
1504 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1505 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1506 if (service->ipconfig_ipv4 != NULL)
1507 pac = __connman_ipconfig_get_proxy_autoconfig(
1508 service->ipconfig_ipv4);
1509 else if (service->ipconfig_ipv6 != NULL)
1510 pac = __connman_ipconfig_get_proxy_autoconfig(
1511 service->ipconfig_ipv6);
1513 if (service->pac == NULL && pac == NULL)
1516 if (service->pac != NULL)
1519 connman_dbus_dict_append_basic(iter, "URL",
1520 DBUS_TYPE_STRING, &pac);
1524 method = proxymethod2string(proxy);
1527 connman_dbus_dict_append_basic(iter, "Method",
1528 DBUS_TYPE_STRING, &method);
1531 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1533 struct connman_service *service = user_data;
1536 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1539 switch (service->proxy_config) {
1540 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1542 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1544 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1545 if (service->proxies != NULL)
1546 connman_dbus_dict_append_array(iter, "Servers",
1548 append_proxies, service);
1550 if (service->excludes != NULL)
1551 connman_dbus_dict_append_array(iter, "Excludes",
1553 append_excludes, service);
1555 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1556 if (service->pac != NULL)
1557 connman_dbus_dict_append_basic(iter, "URL",
1558 DBUS_TYPE_STRING, &service->pac);
1562 method = proxymethod2string(service->proxy_config);
1564 connman_dbus_dict_append_basic(iter, "Method",
1565 DBUS_TYPE_STRING, &method);
1568 static void append_provider(DBusMessageIter *iter, void *user_data)
1570 struct connman_service *service = user_data;
1572 DBG("%p %p", service, service->provider);
1574 if (is_connected(service) == FALSE)
1577 if (service->provider != NULL)
1578 __connman_provider_append_properties(service->provider, iter);
1582 static void settings_changed(struct connman_service *service,
1583 struct connman_ipconfig *ipconfig)
1585 connman_dbus_property_changed_dict(service->path,
1586 CONNMAN_SERVICE_INTERFACE, "IPv4",
1587 append_ipv4, service);
1589 connman_dbus_property_changed_dict(service->path,
1590 CONNMAN_SERVICE_INTERFACE, "IPv6",
1591 append_ipv6, service);
1593 __connman_notifier_ipconfig_changed(service, ipconfig);
1596 static void ipv4_configuration_changed(struct connman_service *service)
1598 connman_dbus_property_changed_dict(service->path,
1599 CONNMAN_SERVICE_INTERFACE,
1600 "IPv4.Configuration",
1605 static void ipv6_configuration_changed(struct connman_service *service)
1607 connman_dbus_property_changed_dict(service->path,
1608 CONNMAN_SERVICE_INTERFACE,
1609 "IPv6.Configuration",
1614 static void dns_changed(struct connman_service *service)
1616 connman_dbus_property_changed_array(service->path,
1617 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1618 DBUS_TYPE_STRING, append_dns, service);
1621 static void dns_configuration_changed(struct connman_service *service)
1623 connman_dbus_property_changed_array(service->path,
1624 CONNMAN_SERVICE_INTERFACE,
1625 "Nameservers.Configuration",
1626 DBUS_TYPE_STRING, append_dnsconfig, service);
1628 dns_changed(service);
1631 static void domain_changed(struct connman_service *service)
1633 connman_dbus_property_changed_array(service->path,
1634 CONNMAN_SERVICE_INTERFACE, "Domains",
1635 DBUS_TYPE_STRING, append_domain, service);
1638 static void domain_configuration_changed(struct connman_service *service)
1640 connman_dbus_property_changed_array(service->path,
1641 CONNMAN_SERVICE_INTERFACE,
1642 "Domains.Configuration",
1643 DBUS_TYPE_STRING, append_domainconfig, service);
1646 static void proxy_changed(struct connman_service *service)
1648 connman_dbus_property_changed_dict(service->path,
1649 CONNMAN_SERVICE_INTERFACE, "Proxy",
1650 append_proxy, service);
1653 static void proxy_configuration_changed(struct connman_service *service)
1655 connman_dbus_property_changed_dict(service->path,
1656 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1657 append_proxyconfig, service);
1659 proxy_changed(service);
1662 static void link_changed(struct connman_service *service)
1664 connman_dbus_property_changed_dict(service->path,
1665 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1666 append_ethernet, service);
1669 static void stats_append_counters(DBusMessageIter *dict,
1670 struct connman_stats_data *stats,
1671 struct connman_stats_data *counters,
1672 connman_bool_t append_all)
1674 if (counters->rx_packets != stats->rx_packets || append_all) {
1675 counters->rx_packets = stats->rx_packets;
1676 connman_dbus_dict_append_basic(dict, "RX.Packets",
1677 DBUS_TYPE_UINT32, &stats->rx_packets);
1680 if (counters->tx_packets != stats->tx_packets || append_all) {
1681 counters->tx_packets = stats->tx_packets;
1682 connman_dbus_dict_append_basic(dict, "TX.Packets",
1683 DBUS_TYPE_UINT32, &stats->tx_packets);
1686 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1687 counters->rx_bytes = stats->rx_bytes;
1688 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1689 DBUS_TYPE_UINT32, &stats->rx_bytes);
1692 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1693 counters->tx_bytes = stats->tx_bytes;
1694 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1695 DBUS_TYPE_UINT32, &stats->tx_bytes);
1698 if (counters->rx_errors != stats->rx_errors || append_all) {
1699 counters->rx_errors = stats->rx_errors;
1700 connman_dbus_dict_append_basic(dict, "RX.Errors",
1701 DBUS_TYPE_UINT32, &stats->rx_errors);
1704 if (counters->tx_errors != stats->tx_errors || append_all) {
1705 counters->tx_errors = stats->tx_errors;
1706 connman_dbus_dict_append_basic(dict, "TX.Errors",
1707 DBUS_TYPE_UINT32, &stats->tx_errors);
1710 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1711 counters->rx_dropped = stats->rx_dropped;
1712 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1713 DBUS_TYPE_UINT32, &stats->rx_dropped);
1716 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1717 counters->tx_dropped = stats->tx_dropped;
1718 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1719 DBUS_TYPE_UINT32, &stats->tx_dropped);
1722 if (counters->time != stats->time || append_all) {
1723 counters->time = stats->time;
1724 connman_dbus_dict_append_basic(dict, "Time",
1725 DBUS_TYPE_UINT32, &stats->time);
1729 static void stats_append(struct connman_service *service,
1730 const char *counter,
1731 struct connman_stats_counter *counters,
1732 connman_bool_t append_all)
1734 DBusMessageIter array, dict;
1737 DBG("service %p counter %s", service, counter);
1739 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1743 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1744 &service->path, DBUS_TYPE_INVALID);
1746 dbus_message_iter_init_append(msg, &array);
1749 connman_dbus_dict_open(&array, &dict);
1751 stats_append_counters(&dict, &service->stats.data,
1752 &counters->stats.data, append_all);
1754 connman_dbus_dict_close(&array, &dict);
1756 /* roaming counter */
1757 connman_dbus_dict_open(&array, &dict);
1759 stats_append_counters(&dict, &service->stats_roaming.data,
1760 &counters->stats_roaming.data, append_all);
1762 connman_dbus_dict_close(&array, &dict);
1764 __connman_counter_send_usage(counter, msg);
1767 static void stats_update(struct connman_service *service,
1768 unsigned int rx_packets, unsigned int tx_packets,
1769 unsigned int rx_bytes, unsigned int tx_bytes,
1770 unsigned int rx_errors, unsigned int tx_errors,
1771 unsigned int rx_dropped, unsigned int tx_dropped)
1773 struct connman_stats *stats = stats_get(service);
1774 struct connman_stats_data *data_last = &stats->data_last;
1775 struct connman_stats_data *data = &stats->data;
1776 unsigned int seconds;
1778 DBG("service %p", service);
1780 if (stats->valid == TRUE) {
1782 rx_packets - data_last->rx_packets;
1784 tx_packets - data_last->tx_packets;
1786 rx_bytes - data_last->rx_bytes;
1788 tx_bytes - data_last->tx_bytes;
1790 rx_errors - data_last->rx_errors;
1792 tx_errors - data_last->tx_errors;
1794 rx_dropped - data_last->rx_dropped;
1796 tx_dropped - data_last->tx_dropped;
1798 stats->valid = TRUE;
1801 data_last->rx_packets = rx_packets;
1802 data_last->tx_packets = tx_packets;
1803 data_last->rx_bytes = rx_bytes;
1804 data_last->tx_bytes = tx_bytes;
1805 data_last->rx_errors = rx_errors;
1806 data_last->tx_errors = tx_errors;
1807 data_last->rx_dropped = rx_dropped;
1808 data_last->tx_dropped = tx_dropped;
1810 seconds = g_timer_elapsed(stats->timer, NULL);
1811 stats->data.time = stats->data_last.time + seconds;
1814 void __connman_service_notify(struct connman_service *service,
1815 unsigned int rx_packets, unsigned int tx_packets,
1816 unsigned int rx_bytes, unsigned int tx_bytes,
1817 unsigned int rx_errors, unsigned int tx_errors,
1818 unsigned int rx_dropped, unsigned int tx_dropped)
1820 GHashTableIter iter;
1821 gpointer key, value;
1822 const char *counter;
1823 struct connman_stats_counter *counters;
1824 struct connman_stats_data *data;
1827 if (service == NULL)
1830 if (is_connected(service) == FALSE)
1833 stats_update(service,
1834 rx_packets, tx_packets,
1836 rx_errors, tx_errors,
1837 rx_dropped, tx_dropped);
1839 data = &stats_get(service)->data;
1840 err = __connman_stats_update(service, service->roaming, data);
1842 connman_error("Failed to store statistics for %s",
1843 service->identifier);
1845 g_hash_table_iter_init(&iter, service->counter_table);
1846 while (g_hash_table_iter_next(&iter, &key, &value)) {
1850 stats_append(service, counter, counters, counters->append_all);
1851 counters->append_all = FALSE;
1855 int __connman_service_counter_register(const char *counter)
1857 struct connman_service *service;
1858 GSequenceIter *iter;
1859 struct connman_stats_counter *counters;
1861 DBG("counter %s", counter);
1863 counter_list = g_slist_append(counter_list, (gpointer)counter);
1865 iter = g_sequence_get_begin_iter(service_list);
1867 while (g_sequence_iter_is_end(iter) == FALSE) {
1868 service = g_sequence_get(iter);
1870 counters = g_try_new0(struct connman_stats_counter, 1);
1871 if (counters == NULL)
1874 counters->append_all = TRUE;
1876 g_hash_table_replace(service->counter_table, (gpointer)counter,
1879 iter = g_sequence_iter_next(iter);
1885 void __connman_service_counter_unregister(const char *counter)
1887 struct connman_service *service;
1888 GSequenceIter *iter;
1890 DBG("counter %s", counter);
1892 iter = g_sequence_get_begin_iter(service_list);
1894 while (g_sequence_iter_is_end(iter) == FALSE) {
1895 service = g_sequence_get(iter);
1897 g_hash_table_remove(service->counter_table, counter);
1899 iter = g_sequence_iter_next(iter);
1902 counter_list = g_slist_remove(counter_list, counter);
1905 GSequence *__connman_service_get_list(struct connman_session *session,
1906 service_match_cb service_match,
1907 create_service_entry_cb create_service_entry,
1908 GDestroyNotify destroy_service_entry)
1911 GSequenceIter *iter;
1912 struct connman_service *service;
1913 struct service_entry *entry;
1915 list = g_sequence_new(destroy_service_entry);
1919 iter = g_sequence_get_begin_iter(service_list);
1921 while (g_sequence_iter_is_end(iter) == FALSE) {
1922 service = g_sequence_get(iter);
1924 if (service_match(session, service) == TRUE) {
1925 entry = create_service_entry(service, service->name,
1930 g_sequence_append(list, entry);
1933 iter = g_sequence_iter_next(iter);
1939 void __connman_service_session_inc(struct connman_service *service)
1941 DBG("service %p ref count %d", service,
1942 service->session_usage_count + 1);
1944 __sync_fetch_and_add(&service->session_usage_count, 1);
1947 connman_bool_t __connman_service_session_dec(struct connman_service *service)
1949 DBG("service %p ref count %d", service,
1950 service->session_usage_count - 1);
1952 if (__sync_fetch_and_sub(&service->session_usage_count, 1) != 1)
1958 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1959 struct connman_service *service)
1963 str = __connman_service_type2string(service->type);
1965 connman_dbus_dict_append_basic(dict, "Type",
1966 DBUS_TYPE_STRING, &str);
1968 connman_dbus_dict_append_array(dict, "Security",
1969 DBUS_TYPE_STRING, append_security, service);
1971 str = state2string(service->state);
1973 connman_dbus_dict_append_basic(dict, "State",
1974 DBUS_TYPE_STRING, &str);
1976 str = error2string(service->error);
1978 connman_dbus_dict_append_basic(dict, "Error",
1979 DBUS_TYPE_STRING, &str);
1981 if (service->strength > 0)
1982 connman_dbus_dict_append_basic(dict, "Strength",
1983 DBUS_TYPE_BYTE, &service->strength);
1985 connman_dbus_dict_append_basic(dict, "Favorite",
1986 DBUS_TYPE_BOOLEAN, &service->favorite);
1988 connman_dbus_dict_append_basic(dict, "Immutable",
1989 DBUS_TYPE_BOOLEAN, &service->immutable);
1991 if (service->favorite == TRUE)
1992 connman_dbus_dict_append_basic(dict, "AutoConnect",
1993 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1995 connman_dbus_dict_append_basic(dict, "AutoConnect",
1996 DBUS_TYPE_BOOLEAN, &service->favorite);
1998 if (service->name != NULL)
1999 connman_dbus_dict_append_basic(dict, "Name",
2000 DBUS_TYPE_STRING, &service->name);
2002 switch (service->type) {
2003 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2004 case CONNMAN_SERVICE_TYPE_SYSTEM:
2005 case CONNMAN_SERVICE_TYPE_GPS:
2006 case CONNMAN_SERVICE_TYPE_VPN:
2007 case CONNMAN_SERVICE_TYPE_GADGET:
2009 case CONNMAN_SERVICE_TYPE_CELLULAR:
2010 connman_dbus_dict_append_basic(dict, "Roaming",
2011 DBUS_TYPE_BOOLEAN, &service->roaming);
2013 connman_dbus_dict_append_dict(dict, "Ethernet",
2014 append_ethernet, service);
2016 case CONNMAN_SERVICE_TYPE_WIFI:
2017 case CONNMAN_SERVICE_TYPE_ETHERNET:
2018 case CONNMAN_SERVICE_TYPE_WIMAX:
2019 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2020 connman_dbus_dict_append_dict(dict, "Ethernet",
2021 append_ethernet, service);
2025 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
2027 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
2028 append_ipv4config, service);
2030 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
2032 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
2033 append_ipv6config, service);
2035 connman_dbus_dict_append_array(dict, "Nameservers",
2036 DBUS_TYPE_STRING, append_dns, service);
2038 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
2039 DBUS_TYPE_STRING, append_dnsconfig, service);
2041 connman_dbus_dict_append_array(dict, "Domains",
2042 DBUS_TYPE_STRING, append_domain, service);
2044 connman_dbus_dict_append_array(dict, "Domains.Configuration",
2045 DBUS_TYPE_STRING, append_domainconfig, service);
2047 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
2049 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
2050 append_proxyconfig, service);
2052 connman_dbus_dict_append_dict(dict, "Provider",
2053 append_provider, service);
2056 static void append_struct(gpointer value, gpointer user_data)
2058 struct connman_service *service = value;
2059 DBusMessageIter *iter = user_data;
2060 DBusMessageIter entry, dict;
2062 if (service->path == NULL)
2065 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
2067 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2070 connman_dbus_dict_open(&entry, &dict);
2071 append_properties(&dict, TRUE, service);
2072 connman_dbus_dict_close(&entry, &dict);
2074 dbus_message_iter_close_container(iter, &entry);
2077 void __connman_service_list_struct(DBusMessageIter *iter)
2079 g_sequence_foreach(service_list, append_struct, iter);
2082 connman_bool_t __connman_service_is_hidden(struct connman_service *service)
2084 return service->hidden;
2087 int __connman_service_get_index(struct connman_service *service)
2089 if (service == NULL)
2092 if (service->network != NULL)
2093 return connman_network_get_index(service->network);
2094 else if (service->provider != NULL)
2095 return connman_provider_get_index(service->provider);
2100 void __connman_service_set_domainname(struct connman_service *service,
2101 const char *domainname)
2103 if (service == NULL || service->hidden == TRUE)
2106 g_free(service->domainname);
2107 service->domainname = g_strdup(domainname);
2109 domain_changed(service);
2112 const char *connman_service_get_domainname(struct connman_service *service)
2114 if (service == NULL)
2117 if (service->domains != NULL)
2118 return service->domains[0];
2120 return service->domainname;
2123 char **connman_service_get_nameservers(struct connman_service *service)
2125 if (service == NULL)
2128 if (service->nameservers_config != NULL)
2129 return g_strdupv(service->nameservers_config);
2130 else if (service->nameservers != NULL ||
2131 service->nameservers_auto != NULL) {
2132 int len = 0, len_auto = 0, i;
2135 if (service->nameservers != NULL)
2136 len = g_strv_length(service->nameservers);
2137 if (service->nameservers_auto != NULL)
2138 len_auto = g_strv_length(service->nameservers_auto);
2140 nameservers = g_try_new0(char *, len + len_auto + 1);
2141 if (nameservers == NULL)
2144 for (i = 0; i < len; i++)
2145 nameservers[i] = g_strdup(service->nameservers[i]);
2147 for (i = 0; i < len_auto; i++)
2148 nameservers[i + len] =
2149 g_strdup(service->nameservers_auto[i]);
2157 void connman_service_set_proxy_method(struct connman_service *service,
2158 enum connman_service_proxy_method method)
2160 if (service == NULL || service->hidden == TRUE)
2163 service->proxy = method;
2165 proxy_changed(service);
2167 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
2168 __connman_notifier_proxy_changed(service);
2171 enum connman_service_proxy_method connman_service_get_proxy_method(
2172 struct connman_service *service)
2174 if (service == NULL)
2175 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2177 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
2178 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
2179 service->pac == NULL)
2180 return service->proxy;
2182 return service->proxy_config;
2185 return service->proxy;
2188 char **connman_service_get_proxy_servers(struct connman_service *service)
2190 return g_strdupv(service->proxies);
2193 char **connman_service_get_proxy_excludes(struct connman_service *service)
2195 return g_strdupv(service->excludes);
2198 const char *connman_service_get_proxy_url(struct connman_service *service)
2200 if (service == NULL)
2203 return service->pac;
2206 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
2209 if (service == NULL || service->hidden == TRUE)
2212 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
2214 if (service->ipconfig_ipv4) {
2215 if (__connman_ipconfig_set_proxy_autoconfig(
2216 service->ipconfig_ipv4, url) < 0)
2218 } else if (service->ipconfig_ipv6) {
2219 if (__connman_ipconfig_set_proxy_autoconfig(
2220 service->ipconfig_ipv6, url) < 0)
2225 proxy_changed(service);
2227 __connman_notifier_proxy_changed(service);
2230 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
2232 if (service == NULL)
2235 if (service->ipconfig_ipv4)
2236 return __connman_ipconfig_get_proxy_autoconfig(
2237 service->ipconfig_ipv4);
2238 else if (service->ipconfig_ipv6)
2239 return __connman_ipconfig_get_proxy_autoconfig(
2240 service->ipconfig_ipv6);
2244 static void update_timeservers(struct connman_service *service)
2248 if (service->timeservers == NULL)
2251 switch (service->state) {
2252 case CONNMAN_SERVICE_STATE_UNKNOWN:
2253 case CONNMAN_SERVICE_STATE_IDLE:
2254 case CONNMAN_SERVICE_STATE_ASSOCIATION:
2255 case CONNMAN_SERVICE_STATE_CONFIGURATION:
2257 case CONNMAN_SERVICE_STATE_FAILURE:
2258 case CONNMAN_SERVICE_STATE_DISCONNECT:
2259 for (i = 0; service->timeservers[i] != NULL; i++)
2260 connman_timeserver_remove(service->timeservers[i]);
2262 case CONNMAN_SERVICE_STATE_READY:
2263 case CONNMAN_SERVICE_STATE_ONLINE:
2267 for (i = 0; service->timeservers[i] != NULL; i++)
2268 connman_timeserver_append(service->timeservers[i]);
2271 int __connman_service_timeserver_append(struct connman_service *service,
2272 const char *timeserver)
2276 DBG("service %p timeserver %s", service, timeserver);
2278 if (timeserver == NULL)
2281 if (service->timeservers != NULL) {
2284 for (i = 0; service->timeservers[i] != NULL; i++)
2285 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2288 len = g_strv_length(service->timeservers);
2289 service->timeservers = g_try_renew(char *, service->timeservers,
2293 service->timeservers = g_try_new0(char *, len + 2);
2296 if (service->timeservers == NULL)
2299 service->timeservers[len] = g_strdup(timeserver);
2300 service->timeservers[len + 1] = NULL;
2302 update_timeservers(service);
2307 int __connman_service_timeserver_remove(struct connman_service *service,
2308 const char *timeserver)
2311 int len, i, j, found = 0;
2313 DBG("service %p timeserver %s", service, timeserver);
2315 if (timeserver == NULL)
2318 if (service->timeservers == NULL)
2321 for (i = 0; service->timeservers != NULL &&
2322 service->timeservers[i] != NULL; i++)
2323 if (g_strcmp0(service->timeservers[i], timeserver) == 0) {
2331 len = g_strv_length(service->timeservers);
2334 g_strfreev(service->timeservers);
2335 service->timeservers = NULL;
2340 servers = g_try_new0(char *, len);
2341 if (servers == NULL)
2344 for (i = 0, j = 0; i < len; i++) {
2345 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2346 servers[j] = g_strdup(service->timeservers[i]);
2347 if (servers[j] == NULL)
2352 servers[len - 1] = NULL;
2354 g_strfreev(service->timeservers);
2355 service->timeservers = servers;
2357 update_timeservers(service);
2362 void __connman_service_set_pac(struct connman_service *service,
2365 if (service->hidden == TRUE)
2367 g_free(service->pac);
2368 service->pac = g_strdup(pac);
2370 proxy_changed(service);
2373 void __connman_service_set_identity(struct connman_service *service,
2374 const char *identity)
2376 if (service->immutable || service->hidden == TRUE)
2379 g_free(service->identity);
2380 service->identity = g_strdup(identity);
2382 if (service->network != NULL)
2383 connman_network_set_string(service->network,
2388 void __connman_service_set_agent_identity(struct connman_service *service,
2389 const char *agent_identity)
2391 if (service->hidden == TRUE)
2393 g_free(service->agent_identity);
2394 service->agent_identity = g_strdup(agent_identity);
2396 if (service->network != NULL)
2397 connman_network_set_string(service->network,
2398 "WiFi.AgentIdentity",
2399 service->agent_identity);
2402 void __connman_service_set_passphrase(struct connman_service *service,
2403 const char* passphrase)
2405 if (service->immutable == TRUE || service->hidden == TRUE)
2408 g_free(service->passphrase);
2409 service->passphrase = g_strdup(passphrase);
2411 if (service->network != NULL)
2412 connman_network_set_string(service->network,
2414 service->passphrase);
2416 service_save(service);
2419 void __connman_service_set_agent_passphrase(struct connman_service *service,
2420 const char *agent_passphrase)
2422 if (service->hidden == TRUE)
2424 g_free(service->agent_passphrase);
2425 service->agent_passphrase = g_strdup(agent_passphrase);
2427 if (service->network != NULL)
2428 connman_network_set_string(service->network,
2429 "WiFi.AgentPassphrase",
2430 service->agent_passphrase);
2433 static DBusMessage *get_properties(DBusConnection *conn,
2434 DBusMessage *msg, void *user_data)
2436 struct connman_service *service = user_data;
2438 DBusMessageIter array, dict;
2440 DBG("service %p", service);
2442 reply = dbus_message_new_method_return(msg);
2446 dbus_message_iter_init_append(reply, &array);
2448 connman_dbus_dict_open(&array, &dict);
2449 append_properties(&dict, FALSE, service);
2450 connman_dbus_dict_close(&array, &dict);
2455 static int update_proxy_configuration(struct connman_service *service,
2456 DBusMessageIter *array)
2458 DBusMessageIter dict;
2459 enum connman_service_proxy_method method;
2460 GString *servers_str = NULL;
2461 GString *excludes_str = NULL;
2462 const char *url = NULL;
2464 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2466 dbus_message_iter_recurse(array, &dict);
2468 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2469 DBusMessageIter entry, variant;
2473 dbus_message_iter_recurse(&dict, &entry);
2475 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2478 dbus_message_iter_get_basic(&entry, &key);
2479 dbus_message_iter_next(&entry);
2481 if (dbus_message_iter_get_arg_type(&entry) !=
2485 dbus_message_iter_recurse(&entry, &variant);
2486 type = dbus_message_iter_get_arg_type(&variant);
2488 if (g_str_equal(key, "Method") == TRUE) {
2491 if (type != DBUS_TYPE_STRING)
2494 dbus_message_iter_get_basic(&variant, &val);
2495 method = string2proxymethod(val);
2496 } else if (g_str_equal(key, "URL") == TRUE) {
2497 if (type != DBUS_TYPE_STRING)
2500 dbus_message_iter_get_basic(&variant, &url);
2501 } else if (g_str_equal(key, "Servers") == TRUE) {
2502 DBusMessageIter str_array;
2504 if (type != DBUS_TYPE_ARRAY)
2507 servers_str = g_string_new(NULL);
2508 if (servers_str == NULL)
2511 dbus_message_iter_recurse(&variant, &str_array);
2513 while (dbus_message_iter_get_arg_type(&str_array) ==
2517 dbus_message_iter_get_basic(&str_array, &val);
2519 if (servers_str->len > 0)
2520 g_string_append_printf(servers_str,
2523 g_string_append(servers_str, val);
2525 dbus_message_iter_next(&str_array);
2527 } else if (g_str_equal(key, "Excludes") == TRUE) {
2528 DBusMessageIter str_array;
2530 if (type != DBUS_TYPE_ARRAY)
2533 excludes_str = g_string_new(NULL);
2534 if (excludes_str == NULL)
2537 dbus_message_iter_recurse(&variant, &str_array);
2539 while (dbus_message_iter_get_arg_type(&str_array) ==
2543 dbus_message_iter_get_basic(&str_array, &val);
2545 if (excludes_str->len > 0)
2546 g_string_append_printf(excludes_str,
2549 g_string_append(excludes_str, val);
2551 dbus_message_iter_next(&str_array);
2555 dbus_message_iter_next(&dict);
2559 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2561 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2562 if (servers_str == NULL && service->proxies == NULL)
2565 if (servers_str != NULL) {
2566 g_strfreev(service->proxies);
2568 if (servers_str->len > 0)
2569 service->proxies = g_strsplit_set(
2570 servers_str->str, " ", 0);
2572 service->proxies = NULL;
2575 if (excludes_str != NULL) {
2576 g_strfreev(service->excludes);
2578 if (excludes_str->len > 0)
2579 service->excludes = g_strsplit_set(
2580 excludes_str->str, " ", 0);
2582 service->excludes = NULL;
2585 if (service->proxies == NULL)
2586 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2589 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2590 g_free(service->pac);
2592 if (url != NULL && strlen(url) > 0)
2593 service->pac = g_strdup(url);
2595 service->pac = NULL;
2597 /* if we are connected:
2598 - if service->pac == NULL
2599 - if __connman_ipconfig_get_proxy_autoconfig(
2600 service->ipconfig) == NULL
2601 --> We should start WPAD */
2604 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2608 if (servers_str != NULL)
2609 g_string_free(servers_str, TRUE);
2611 if (excludes_str != NULL)
2612 g_string_free(excludes_str, TRUE);
2614 service->proxy_config = method;
2619 if (servers_str != NULL)
2620 g_string_free(servers_str, TRUE);
2622 if (excludes_str != NULL)
2623 g_string_free(excludes_str, TRUE);
2628 static int set_ipconfig(struct connman_service *service,
2629 struct connman_ipconfig *ipconfig,
2630 DBusMessageIter *array,
2631 enum connman_service_state state,
2632 enum connman_service_state *new_state)
2634 enum connman_ipconfig_method old_method;
2635 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2636 enum connman_ipconfig_type type;
2639 if (ipconfig == NULL)
2642 old_method = __connman_ipconfig_get_method(ipconfig);
2644 if (is_connecting_state(service, state) ||
2645 is_connected_state(service, state))
2646 __connman_network_clear_ipconfig(service->network, ipconfig);
2648 err = __connman_ipconfig_set_config(ipconfig, array);
2649 method = __connman_ipconfig_get_method(ipconfig);
2650 type = __connman_ipconfig_get_config_type(ipconfig);
2652 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2653 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2654 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2655 *new_state = service->state_ipv4 =
2656 CONNMAN_SERVICE_STATE_CONFIGURATION;
2657 __connman_ipconfig_enable(ipconfig);
2660 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2661 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2662 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2663 *new_state = service->state_ipv6;
2664 __connman_ipconfig_enable(ipconfig);
2668 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2669 type, method, state2string(*new_state));
2674 static DBusMessage *set_property(DBusConnection *conn,
2675 DBusMessage *msg, void *user_data)
2677 struct connman_service *service = user_data;
2678 DBusMessageIter iter, value;
2682 DBG("service %p", service);
2684 if (dbus_message_iter_init(msg, &iter) == FALSE)
2685 return __connman_error_invalid_arguments(msg);
2687 dbus_message_iter_get_basic(&iter, &name);
2688 dbus_message_iter_next(&iter);
2689 dbus_message_iter_recurse(&iter, &value);
2691 type = dbus_message_iter_get_arg_type(&value);
2693 if (g_str_equal(name, "AutoConnect") == TRUE) {
2694 connman_bool_t autoconnect;
2696 if (type != DBUS_TYPE_BOOLEAN)
2697 return __connman_error_invalid_arguments(msg);
2699 if (service->favorite == FALSE)
2700 return __connman_error_invalid_service(msg);
2702 dbus_message_iter_get_basic(&value, &autoconnect);
2704 if (service->autoconnect == autoconnect)
2705 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2707 service->autoconnect = autoconnect;
2709 autoconnect_changed(service);
2711 service_save(service);
2712 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2713 DBusMessageIter entry;
2718 if (type != DBUS_TYPE_ARRAY)
2719 return __connman_error_invalid_arguments(msg);
2721 str = g_string_new(NULL);
2723 return __connman_error_invalid_arguments(msg);
2725 index = connman_network_get_index(service->network);
2726 gw = __connman_ipconfig_get_gateway_from_index(index);
2728 if (gw && strlen(gw))
2729 __connman_service_nameserver_del_routes(service);
2731 dbus_message_iter_recurse(&value, &entry);
2733 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2735 dbus_message_iter_get_basic(&entry, &val);
2736 dbus_message_iter_next(&entry);
2738 g_string_append_printf(str, " %s", val);
2740 g_string_append(str, val);
2743 g_strfreev(service->nameservers_config);
2746 service->nameservers_config =
2747 g_strsplit_set(str->str, " ", 0);
2749 service->nameservers_config = NULL;
2752 g_string_free(str, TRUE);
2754 if (gw && strlen(gw))
2755 __connman_service_nameserver_add_routes(service, gw);
2757 update_nameservers(service);
2758 dns_configuration_changed(service);
2760 service_save(service);
2761 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2762 DBusMessageIter entry;
2765 if (type != DBUS_TYPE_ARRAY)
2766 return __connman_error_invalid_arguments(msg);
2768 str = g_string_new(NULL);
2770 return __connman_error_invalid_arguments(msg);
2772 dbus_message_iter_recurse(&value, &entry);
2774 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2776 dbus_message_iter_get_basic(&entry, &val);
2777 dbus_message_iter_next(&entry);
2779 g_string_append_printf(str, " %s", val);
2781 g_string_append(str, val);
2784 g_strfreev(service->domains);
2787 service->domains = g_strsplit_set(str->str, " ", 0);
2789 service->domains = NULL;
2791 g_string_free(str, TRUE);
2793 update_nameservers(service);
2794 domain_configuration_changed(service);
2796 service_save(service);
2797 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2800 if (type != DBUS_TYPE_ARRAY)
2801 return __connman_error_invalid_arguments(msg);
2803 err = update_proxy_configuration(service, &value);
2806 return __connman_error_failed(msg, -err);
2808 proxy_configuration_changed(service);
2810 __connman_notifier_proxy_changed(service);
2812 service_save(service);
2813 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2814 g_str_equal(name, "IPv6.Configuration")) {
2816 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2817 enum connman_service_state state =
2818 CONNMAN_SERVICE_STATE_UNKNOWN;
2823 if (service->ipconfig_ipv4 == NULL &&
2824 service->ipconfig_ipv6 == NULL)
2825 return __connman_error_invalid_property(msg);
2827 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2828 ipv4 = service->ipconfig_ipv4;
2829 err = set_ipconfig(service, ipv4, &value,
2830 service->state_ipv4, &state);
2832 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2833 ipv6 = service->ipconfig_ipv6;
2834 err = set_ipconfig(service, ipv6, &value,
2835 service->state_ipv6, &state);
2839 if (is_connected_state(service, state) ||
2840 is_connecting_state(service, state))
2841 __connman_network_set_ipconfig(service->network,
2843 return __connman_error_failed(msg, -err);
2847 ipv4_configuration_changed(service);
2849 ipv6_configuration_changed(service);
2851 if (is_connecting(service) || is_connected(service))
2852 __connman_network_set_ipconfig(service->network,
2855 service_save(service);
2857 return __connman_error_invalid_property(msg);
2859 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2862 static void set_idle(struct connman_service *service)
2864 service->state = service->state_ipv4 = service->state_ipv6 =
2865 CONNMAN_SERVICE_STATE_IDLE;
2866 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2867 state_changed(service);
2870 static DBusMessage *clear_property(DBusConnection *conn,
2871 DBusMessage *msg, void *user_data)
2873 struct connman_service *service = user_data;
2876 DBG("service %p", service);
2878 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2881 if (g_str_equal(name, "Error") == TRUE) {
2884 g_get_current_time(&service->modified);
2885 service_save(service);
2887 return __connman_error_invalid_property(msg);
2889 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2892 static connman_bool_t is_ignore(struct connman_service *service)
2894 if (service->autoconnect == FALSE)
2897 if (service->roaming == TRUE)
2900 if (service->ignore == TRUE)
2903 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
2909 void __connman_service_auto_connect(void)
2911 struct connman_service *service = NULL;
2912 GSequenceIter *iter;
2916 if (__connman_session_mode() == TRUE) {
2917 DBG("Session mode enabled: auto connect disabled");
2921 iter = g_sequence_get_begin_iter(service_list);
2923 while (g_sequence_iter_is_end(iter) == FALSE) {
2924 service = g_sequence_get(iter);
2926 if (service->pending != NULL)
2929 if (is_connecting(service) == TRUE)
2932 if (service->favorite == FALSE)
2935 if (is_connected(service) == TRUE)
2938 if (is_ignore(service) == FALSE && service->state ==
2939 CONNMAN_SERVICE_STATE_IDLE)
2944 iter = g_sequence_iter_next(iter);
2947 if (service != NULL) {
2948 service->userconnect = FALSE;
2949 __connman_service_connect(service);
2953 static void remove_timeout(struct connman_service *service)
2955 if (service->timeout > 0) {
2956 g_source_remove(service->timeout);
2957 service->timeout = 0;
2961 static void reply_pending(struct connman_service *service, int error)
2963 remove_timeout(service);
2965 if (service->pending != NULL) {
2969 reply = __connman_error_failed(service->pending,
2972 g_dbus_send_message(connection, reply);
2976 sender = dbus_message_get_interface(service->pending);
2978 DBG("sender %s", sender);
2980 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2981 g_dbus_send_reply(connection, service->pending,
2982 DBUS_TYPE_OBJECT_PATH, &service->path,
2985 g_dbus_send_reply(connection, service->pending,
2989 dbus_message_unref(service->pending);
2990 service->pending = NULL;
2994 static gboolean connect_timeout(gpointer user_data)
2996 struct connman_service *service = user_data;
2997 connman_bool_t autoconnect = FALSE;
2999 DBG("service %p", service);
3001 service->timeout = 0;
3003 if (service->network != NULL)
3004 __connman_network_disconnect(service->network);
3006 __connman_ipconfig_disable(service->ipconfig_ipv4);
3007 __connman_ipconfig_disable(service->ipconfig_ipv6);
3009 __connman_stats_service_unregister(service);
3011 if (service->pending != NULL) {
3014 reply = __connman_error_operation_timeout(service->pending);
3016 g_dbus_send_message(connection, reply);
3018 dbus_message_unref(service->pending);
3019 service->pending = NULL;
3023 __connman_service_ipconfig_indicate_state(service,
3024 CONNMAN_SERVICE_STATE_FAILURE,
3025 CONNMAN_IPCONFIG_TYPE_IPV4);
3026 __connman_service_ipconfig_indicate_state(service,
3027 CONNMAN_SERVICE_STATE_FAILURE,
3028 CONNMAN_IPCONFIG_TYPE_IPV6);
3030 if (autoconnect == TRUE && service->userconnect == FALSE)
3031 __connman_service_auto_connect();
3036 static void set_reconnect_state(struct connman_service *service,
3037 connman_bool_t reconnect)
3039 struct connman_device *device;
3041 if (service->network == NULL)
3044 device = connman_network_get_device(service->network);
3048 __connman_device_set_reconnect(device, reconnect);
3051 static connman_bool_t get_reconnect_state(struct connman_service *service)
3053 struct connman_device *device;
3055 if (service->network == NULL)
3058 device = connman_network_get_device(service->network);
3062 return __connman_device_get_reconnect(device);
3065 static DBusMessage *connect_service(DBusConnection *conn,
3066 DBusMessage *msg, void *user_data)
3068 struct connman_service *service = user_data;
3069 GSequenceIter *iter;
3072 DBG("service %p", service);
3074 if (service->pending != NULL)
3075 return __connman_error_in_progress(msg);
3077 iter = g_sequence_get_begin_iter(service_list);
3079 while (g_sequence_iter_is_end(iter) == FALSE) {
3080 struct connman_service *temp = g_sequence_get(iter);
3082 if (service->type == temp->type && is_connecting(temp) == TRUE)
3083 return __connman_error_in_progress(msg);
3085 iter = g_sequence_iter_next(iter);
3088 service->ignore = FALSE;
3090 service->userconnect = TRUE;
3092 service->pending = dbus_message_ref(msg);
3094 set_reconnect_state(service, FALSE);
3096 err = __connman_service_connect(service);
3098 if (service->pending == NULL)
3101 if (err != -EINPROGRESS) {
3102 dbus_message_unref(service->pending);
3103 service->pending = NULL;
3105 return __connman_error_failed(msg, -err);
3111 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3114 static DBusMessage *disconnect_service(DBusConnection *conn,
3115 DBusMessage *msg, void *user_data)
3117 struct connman_service *service = user_data;
3120 DBG("service %p", service);
3122 reply_pending(service, ECONNABORTED);
3124 service->ignore = TRUE;
3126 set_reconnect_state(service, FALSE);
3128 err = __connman_service_disconnect(service);
3130 if (err != -EINPROGRESS)
3131 return __connman_error_failed(msg, -err);
3136 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3139 static DBusMessage *remove_service(DBusConnection *conn,
3140 DBusMessage *msg, void *user_data)
3142 struct connman_service *service = user_data;
3144 DBG("service %p", service);
3146 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
3147 return __connman_error_not_supported(msg);
3149 if (service->immutable == TRUE || service->hidden == TRUE)
3150 return __connman_error_not_supported(msg);
3152 if (service->favorite == FALSE && service->state !=
3153 CONNMAN_SERVICE_STATE_FAILURE)
3154 return __connman_error_not_supported(msg);
3156 set_reconnect_state(service, FALSE);
3158 __connman_service_disconnect(service);
3160 g_free(service->passphrase);
3161 service->passphrase = NULL;
3165 __connman_service_set_favorite(service, FALSE);
3166 service_save(service);
3168 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3171 static gboolean check_suitable_state(enum connman_service_state a,
3172 enum connman_service_state b)
3175 * Special check so that "ready" service can be moved before
3178 if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
3179 b == CONNMAN_SERVICE_STATE_READY) ||
3180 (b == CONNMAN_SERVICE_STATE_ONLINE &&
3181 a == CONNMAN_SERVICE_STATE_READY))
3187 static void downgrade_state(struct connman_service *service)
3189 if (service == NULL)
3192 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
3193 service->state_ipv6);
3195 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
3196 __connman_service_ipconfig_indicate_state(service,
3197 CONNMAN_SERVICE_STATE_READY,
3198 CONNMAN_IPCONFIG_TYPE_IPV4);
3200 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
3201 __connman_service_ipconfig_indicate_state(service,
3202 CONNMAN_SERVICE_STATE_READY,
3203 CONNMAN_IPCONFIG_TYPE_IPV6);
3206 static void apply_relevant_default_downgrade(struct connman_service *service)
3208 struct connman_service *def_service;
3210 def_service = get_default();
3211 if (def_service == NULL)
3214 if (def_service == service &&
3215 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3216 def_service->state = CONNMAN_SERVICE_STATE_READY;
3219 static DBusMessage *move_service(DBusConnection *conn,
3220 DBusMessage *msg, void *user_data,
3223 struct connman_service *service = user_data;
3224 struct connman_service *target;
3226 GSequenceIter *src, *dst;
3227 enum connman_ipconfig_method target4, target6;
3228 enum connman_ipconfig_method service4, service6;
3230 DBG("service %p", service);
3232 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3235 if (service->favorite == FALSE)
3236 return __connman_error_not_supported(msg);
3238 target = find_service(path);
3239 if (target == NULL || target->favorite == FALSE || target == service ||
3240 target->type == CONNMAN_SERVICE_TYPE_VPN)
3241 return __connman_error_invalid_service(msg);
3243 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3244 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3245 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3246 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3248 DBG("target %s method %d/%d state %d/%d", target->identifier,
3250 target->state_ipv4, target->state_ipv6);
3252 DBG("service %s method %d/%d state %d/%d", service->identifier,
3254 service->state_ipv4, service->state_ipv6);
3257 * If method is OFF, then we do not need to check the corresponding
3260 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3261 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3262 if (check_suitable_state(target->state_ipv6,
3263 service->state_ipv6) == FALSE)
3264 return __connman_error_invalid_service(msg);
3268 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3269 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3270 if (check_suitable_state(target->state_ipv4,
3271 service->state_ipv4) == FALSE)
3272 return __connman_error_invalid_service(msg);
3276 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3277 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3278 if (check_suitable_state(target->state_ipv6,
3279 service->state_ipv6) == FALSE)
3280 return __connman_error_invalid_service(msg);
3284 if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3285 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3286 if (check_suitable_state(target->state_ipv4,
3287 service->state_ipv4) == FALSE)
3288 return __connman_error_invalid_service(msg);
3292 g_get_current_time(&service->modified);
3293 service_save(service);
3295 src = g_hash_table_lookup(service_hash, service->identifier);
3296 dst = g_hash_table_lookup(service_hash, target->identifier);
3299 * If the service which goes down is the default service and is
3300 * online, we downgrade directly its state to ready so:
3301 * the service which goes up, needs to recompute its state which
3302 * is triggered via downgrading it - if relevant - to state ready.
3304 if (before == TRUE) {
3305 apply_relevant_default_downgrade(target);
3306 g_sequence_move(src, dst);
3307 downgrade_state(service);
3309 apply_relevant_default_downgrade(service);
3310 g_sequence_move(dst, src);
3311 downgrade_state(target);
3314 services_changed(FALSE);
3316 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3319 static DBusMessage *move_before(DBusConnection *conn,
3320 DBusMessage *msg, void *user_data)
3322 return move_service(conn, msg, user_data, TRUE);
3325 static DBusMessage *move_after(DBusConnection *conn,
3326 DBusMessage *msg, void *user_data)
3328 return move_service(conn, msg, user_data, FALSE);
3331 static DBusMessage *reset_counters(DBusConnection *conn,
3332 DBusMessage *msg, void *user_data)
3334 struct connman_service *service = user_data;
3336 reset_stats(service);
3338 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3341 static GDBusMethodTable service_methods[] = {
3342 { "GetProperties", "", "a{sv}", get_properties },
3343 { "SetProperty", "sv", "", set_property },
3344 { "ClearProperty", "s", "", clear_property },
3345 { "Connect", "", "", connect_service,
3346 G_DBUS_METHOD_FLAG_ASYNC },
3347 { "Disconnect", "", "", disconnect_service },
3348 { "Remove", "", "", remove_service },
3349 { "MoveBefore", "o", "", move_before },
3350 { "MoveAfter", "o", "", move_after },
3351 { "ResetCounters", "", "", reset_counters },
3355 static GDBusSignalTable service_signals[] = {
3356 { "PropertyChanged", "sv" },
3360 static void service_free(gpointer user_data)
3362 struct connman_service *service = user_data;
3363 char *path = service->path;
3365 DBG("service %p", service);
3367 reply_pending(service, ENOENT);
3369 g_hash_table_remove(service_hash, service->identifier);
3371 __connman_notifier_service_remove(service);
3373 stats_stop(service);
3375 service->path = NULL;
3378 services_changed(FALSE);
3380 g_dbus_unregister_interface(connection, path,
3381 CONNMAN_SERVICE_INTERFACE);
3385 g_hash_table_destroy(service->counter_table);
3387 if (service->network != NULL) {
3388 __connman_network_disconnect(service->network);
3389 connman_network_unref(service->network);
3392 if (service->provider != NULL)
3393 connman_provider_unref(service->provider);
3395 if (service->ipconfig_ipv4 != NULL) {
3396 __connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3397 __connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3398 __connman_ipconfig_unref(service->ipconfig_ipv4);
3399 service->ipconfig_ipv4 = NULL;
3402 if (service->ipconfig_ipv6 != NULL) {
3403 __connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3404 __connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3405 __connman_ipconfig_unref(service->ipconfig_ipv6);
3406 service->ipconfig_ipv6 = NULL;
3409 g_strfreev(service->nameservers);
3410 g_strfreev(service->nameservers_config);
3411 g_strfreev(service->nameservers_auto);
3412 g_strfreev(service->domains);
3413 g_strfreev(service->proxies);
3414 g_strfreev(service->excludes);
3416 g_free(service->domainname);
3417 g_free(service->pac);
3418 g_free(service->name);
3419 g_free(service->passphrase);
3420 g_free(service->agent_passphrase);
3421 g_free(service->identifier);
3422 g_free(service->eap);
3423 g_free(service->identity);
3424 g_free(service->agent_identity);
3425 g_free(service->ca_cert_file);
3426 g_free(service->client_cert_file);
3427 g_free(service->private_key_file);
3428 g_free(service->private_key_passphrase);
3429 g_free(service->phase2);
3431 if (service->stats.timer != NULL)
3432 g_timer_destroy(service->stats.timer);
3433 if (service->stats_roaming.timer != NULL)
3434 g_timer_destroy(service->stats_roaming.timer);
3440 * __connman_service_put:
3441 * @service: service structure
3443 * Release service if no longer needed
3445 void __connman_service_put(struct connman_service *service)
3447 GSequenceIter *iter;
3449 DBG("service %p ref %d", service, service->refcount - 1);
3451 if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
3454 iter = g_hash_table_lookup(service_hash, service->identifier);
3456 reply_pending(service, ECONNABORTED);
3458 __connman_service_disconnect(service);
3460 g_sequence_remove(iter);
3462 service_free(service);
3466 static void stats_init(struct connman_service *service)
3469 service->stats.valid = FALSE;
3470 service->stats.enabled = FALSE;
3471 service->stats.timer = g_timer_new();
3474 service->stats_roaming.valid = FALSE;
3475 service->stats_roaming.enabled = FALSE;
3476 service->stats_roaming.timer = g_timer_new();
3479 static void service_initialize(struct connman_service *service)
3481 DBG("service %p", service);
3483 service->refcount = 1;
3484 service->session_usage_count = 0;
3486 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3487 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3489 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3490 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3491 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3493 service->favorite = FALSE;
3494 service->immutable = FALSE;
3495 service->hidden = FALSE;
3497 service->ignore = FALSE;
3499 service->userconnect = FALSE;
3503 stats_init(service);
3505 service->provider = NULL;
3507 service->wps = FALSE;
3511 * connman_service_create:
3513 * Allocate a new service.
3515 * Returns: a newly-allocated #connman_service structure
3517 struct connman_service *connman_service_create(void)
3520 struct connman_stats_counter *counters;
3521 const char *counter;
3523 struct connman_service *service;
3525 service = g_try_new0(struct connman_service, 1);
3526 if (service == NULL)
3529 DBG("service %p", service);
3531 service->counter_table = g_hash_table_new_full(g_str_hash,
3532 g_str_equal, NULL, g_free);
3534 for (list = counter_list; list; list = list->next) {
3535 counter = list->data;
3537 counters = g_try_new0(struct connman_stats_counter, 1);
3538 if (counters == NULL) {
3539 g_hash_table_destroy(service->counter_table);
3544 counters->append_all = TRUE;
3546 g_hash_table_replace(service->counter_table, (gpointer)counter,
3550 service_initialize(service);
3556 * connman_service_ref:
3557 * @service: service structure
3559 * Increase reference counter of service
3561 struct connman_service *connman_service_ref(struct connman_service *service)
3563 DBG("%p ref %d", service, service->refcount + 1);
3565 __sync_fetch_and_add(&service->refcount, 1);
3571 * connman_service_unref:
3572 * @service: service structure
3574 * Decrease reference counter of service
3576 void connman_service_unref(struct connman_service *service)
3578 __connman_service_put(service);
3581 static gint service_compare(gconstpointer a, gconstpointer b,
3584 struct connman_service *service_a = (void *) a;
3585 struct connman_service *service_b = (void *) b;
3586 enum connman_service_state state_a, state_b;
3588 state_a = service_a->state;
3589 state_b = service_b->state;
3591 if (state_a != state_b) {
3592 gboolean a_connected = is_connected(service_a);
3593 gboolean b_connected = is_connected(service_b);
3595 if (a_connected == TRUE && b_connected == TRUE) {
3596 /* We prefer online over ready state */
3597 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
3600 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
3604 if (a_connected == TRUE)
3606 if (b_connected == TRUE)
3609 if (is_connecting(service_a) == TRUE)
3611 if (is_connecting(service_b) == TRUE)
3615 if (service_a->order > service_b->order)
3618 if (service_a->order < service_b->order)
3621 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3624 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3627 if (service_a->type != service_b->type) {
3628 switch (service_a->type) {
3629 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3630 case CONNMAN_SERVICE_TYPE_SYSTEM:
3631 case CONNMAN_SERVICE_TYPE_ETHERNET:
3632 case CONNMAN_SERVICE_TYPE_GPS:
3633 case CONNMAN_SERVICE_TYPE_VPN:
3634 case CONNMAN_SERVICE_TYPE_GADGET:
3636 case CONNMAN_SERVICE_TYPE_WIFI:
3638 case CONNMAN_SERVICE_TYPE_WIMAX:
3639 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3640 case CONNMAN_SERVICE_TYPE_CELLULAR:
3645 return (gint) service_b->strength - (gint) service_a->strength;
3649 * connman_service_get_type:
3650 * @service: service structure
3652 * Get the type of service
3654 enum connman_service_type connman_service_get_type(struct connman_service *service)
3656 if (service == NULL)
3657 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3659 return service->type;
3663 * connman_service_get_interface:
3664 * @service: service structure
3666 * Get network interface of service
3668 char *connman_service_get_interface(struct connman_service *service)
3672 if (service == NULL)
3675 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3676 if (service->ipconfig_ipv4)
3677 index = __connman_ipconfig_get_index(
3678 service->ipconfig_ipv4);
3679 else if (service->ipconfig_ipv6)
3680 index = __connman_ipconfig_get_index(
3681 service->ipconfig_ipv6);
3685 return connman_inet_ifname(index);
3688 if (service->network == NULL)
3691 index = connman_network_get_index(service->network);
3693 return connman_inet_ifname(index);
3697 * connman_service_get_network:
3698 * @service: service structure
3700 * Get the service network
3702 struct connman_network *
3703 __connman_service_get_network(struct connman_service *service)
3705 if (service == NULL)
3708 return service->network;
3711 struct connman_ipconfig *
3712 __connman_service_get_ip4config(struct connman_service *service)
3714 if (service == NULL)
3717 return service->ipconfig_ipv4;
3720 struct connman_ipconfig *
3721 __connman_service_get_ip6config(struct connman_service *service)
3723 if (service == NULL)
3726 return service->ipconfig_ipv6;
3729 struct connman_ipconfig *
3730 __connman_service_get_ipconfig(struct connman_service *service, int family)
3732 if (family == AF_INET)
3733 return __connman_service_get_ip4config(service);
3734 else if (family == AF_INET6)
3735 return __connman_service_get_ip6config(service);
3741 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3743 if (service == NULL)
3744 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3746 return service->security;
3749 const char *__connman_service_get_phase2(struct connman_service *service)
3751 if (service == NULL)
3754 return service->phase2;
3757 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3759 if (service == NULL)
3762 return service->wps;
3766 * __connman_service_set_favorite:
3767 * @service: service structure
3768 * @favorite: favorite value
3770 * Change the favorite setting of service
3772 int __connman_service_set_favorite(struct connman_service *service,
3773 connman_bool_t favorite)
3775 GSequenceIter *iter;
3777 if (service->hidden == TRUE)
3779 iter = g_hash_table_lookup(service_hash, service->identifier);
3783 if (service->favorite == favorite)
3786 service->favorite = favorite;
3787 service->order = __connman_service_get_order(service);
3789 favorite_changed(service);
3791 g_sequence_sort_changed(iter, service_compare, NULL);
3793 services_changed(FALSE);
3798 int __connman_service_set_immutable(struct connman_service *service,
3799 connman_bool_t immutable)
3801 if (service->hidden == TRUE)
3803 service->immutable = immutable;
3805 immutable_changed(service);
3810 void __connman_service_set_string(struct connman_service *service,
3811 const char *key, const char *value)
3813 if (service->hidden == TRUE)
3815 if (g_str_equal(key, "EAP") == TRUE) {
3816 g_free(service->eap);
3817 service->eap = g_strdup(value);
3818 } else if (g_str_equal(key, "Identity") == TRUE) {
3819 g_free(service->identity);
3820 service->identity = g_strdup(value);
3821 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3822 g_free(service->ca_cert_file);
3823 service->ca_cert_file = g_strdup(value);
3824 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3825 g_free(service->client_cert_file);
3826 service->client_cert_file = g_strdup(value);
3827 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3828 g_free(service->private_key_file);
3829 service->private_key_file = g_strdup(value);
3830 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3831 g_free(service->private_key_passphrase);
3832 service->private_key_passphrase = g_strdup(value);
3833 } else if (g_str_equal(key, "Phase2") == TRUE) {
3834 g_free(service->phase2);
3835 service->phase2 = g_strdup(value);
3836 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3837 g_free(service->passphrase);
3838 service->passphrase = g_strdup(value);
3842 static void service_complete(struct connman_service *service)
3844 reply_pending(service, EIO);
3846 if (service->userconnect == FALSE)
3847 __connman_service_auto_connect();
3849 g_get_current_time(&service->modified);
3850 service_save(service);
3853 static void report_error_cb(struct connman_service *service,
3854 gboolean retry, void *user_data)
3857 __connman_service_connect(service);
3859 service_complete(service);
3860 services_changed(FALSE);
3861 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3865 void __connman_service_add_passphrase(struct connman_service *service,
3866 const gchar *passphrase)
3868 switch (service->security) {
3869 case CONNMAN_SERVICE_SECURITY_WEP:
3870 case CONNMAN_SERVICE_SECURITY_PSK:
3871 __connman_service_set_passphrase(service, passphrase);
3873 case CONNMAN_SERVICE_SECURITY_8021X:
3874 __connman_service_set_agent_passphrase(service,
3877 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3878 case CONNMAN_SERVICE_SECURITY_NONE:
3879 case CONNMAN_SERVICE_SECURITY_WPA:
3880 case CONNMAN_SERVICE_SECURITY_RSN:
3881 DBG("service security '%s' (%d) not handled",
3882 security2string(service->security),
3889 static void request_input_cb (struct connman_service *service,
3890 connman_bool_t values_received,
3891 const char *name, int name_len,
3892 const char *identity, const char *passphrase,
3895 struct connman_device *device;
3897 DBG ("RequestInput return, %p", service);
3899 if (service->hidden == TRUE && name_len > 0 && name_len <= 32) {
3900 device = connman_network_get_device(service->network);
3901 __connman_device_request_hidden_scan(device,
3903 identity, passphrase);
3906 if (values_received == FALSE || service->hidden == TRUE) {
3907 service_complete(service);
3908 services_changed(FALSE);
3909 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3913 if (identity != NULL)
3914 __connman_service_set_agent_identity(service, identity);
3916 if (passphrase != NULL)
3917 __connman_service_add_passphrase(service, passphrase);
3919 __connman_service_connect(service);
3921 /* Never cache agent provided credentials */
3922 __connman_service_set_agent_identity(service, NULL);
3923 __connman_service_set_agent_passphrase(service, NULL);
3926 static void downgrade_connected_services(void)
3928 struct connman_service *up_service;
3929 GSequenceIter *iter;
3931 iter = g_sequence_get_begin_iter(service_list);
3932 while (g_sequence_iter_is_end(iter) == FALSE) {
3933 up_service = g_sequence_get(iter);
3935 if (is_connected(up_service) == FALSE) {
3936 iter = g_sequence_iter_next(iter);
3940 if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3943 downgrade_state(up_service);
3945 iter = g_sequence_iter_next(iter);
3949 static int service_indicate_state(struct connman_service *service)
3951 enum connman_service_state old_state, new_state;
3952 struct connman_service *def_service;
3953 GSequenceIter *iter;
3955 if (service == NULL)
3958 old_state = service->state;
3959 new_state = combine_state(service->state_ipv4, service->state_ipv6);
3961 DBG("service %p old %s - new %s/%s => %s",
3963 state2string(old_state),
3964 state2string(service->state_ipv4),
3965 state2string(service->state_ipv6),
3966 state2string(new_state));
3968 if (old_state == new_state)
3971 def_service = get_default();
3973 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
3974 if (def_service != NULL && def_service != service &&
3975 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3979 service->state = new_state;
3980 state_changed(service);
3982 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
3983 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3984 reply_pending(service, ECONNABORTED);
3986 __connman_service_disconnect(service);
3989 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3990 if (__connman_stats_service_register(service) == 0) {
3991 __connman_stats_get(service, FALSE,
3992 &service->stats.data);
3993 __connman_stats_get(service, TRUE,
3994 &service->stats_roaming.data);
3998 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
3999 connman_timeserver_sync();
4002 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
4003 connman_bool_t reconnect;
4005 reconnect = get_reconnect_state(service);
4006 if (reconnect == TRUE)
4007 __connman_service_auto_connect();
4009 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4012 if (new_state == CONNMAN_SERVICE_STATE_READY) {
4013 enum connman_ipconfig_method method;
4015 set_reconnect_state(service, TRUE);
4017 __connman_service_set_favorite(service, TRUE);
4019 reply_pending(service, 0);
4021 service->userconnect = FALSE;
4023 g_get_current_time(&service->modified);
4024 service_save(service);
4026 update_nameservers(service);
4027 dns_changed(service);
4028 domain_changed(service);
4030 __connman_notifier_connect(service->type);
4032 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4033 connman_network_get_bool(service->network,
4034 "WiFi.UseWPS") == TRUE) {
4037 pass = connman_network_get_string(service->network,
4040 __connman_service_set_passphrase(service, pass);
4042 connman_network_set_bool(service->network,
4043 "WiFi.UseWPS", FALSE);
4048 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4049 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4050 __connman_ipconfig_disable_ipv6(
4051 service->ipconfig_ipv6);
4053 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4054 def_service = get_default();
4056 if (__connman_notifier_count_connected() == 0 &&
4057 def_service != NULL &&
4058 def_service->provider != NULL)
4059 __connman_provider_disconnect(def_service->provider);
4063 __connman_wispr_stop(service);
4065 __connman_wpad_stop(service);
4067 update_nameservers(service);
4068 dns_changed(service);
4069 domain_changed(service);
4071 __connman_notifier_disconnect(service->type);
4074 * Previous services which are connected and which states
4075 * are set to online should reset relevantly ipconfig_state
4076 * to ready so wispr/portal will be rerun on those
4078 downgrade_connected_services();
4081 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4082 if (service->userconnect == TRUE &&
4083 __connman_agent_report_error(service,
4084 error2string(service->error),
4085 report_error_cb, NULL) == -EIO)
4087 service_complete(service);
4089 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4091 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4093 iter = g_hash_table_lookup(service_hash, service->identifier);
4095 g_sequence_sort_changed(iter, service_compare, NULL);
4097 services_changed(FALSE);
4099 if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
4105 int __connman_service_indicate_error(struct connman_service *service,
4106 enum connman_service_error error)
4108 DBG("service %p error %d", service, error);
4110 if (service == NULL)
4113 service->error = error;
4115 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4116 __connman_service_set_passphrase(service, NULL);
4118 __connman_service_ipconfig_indicate_state(service,
4119 CONNMAN_SERVICE_STATE_FAILURE,
4120 CONNMAN_IPCONFIG_TYPE_IPV4);
4121 __connman_service_ipconfig_indicate_state(service,
4122 CONNMAN_SERVICE_STATE_FAILURE,
4123 CONNMAN_IPCONFIG_TYPE_IPV6);
4127 int __connman_service_clear_error(struct connman_service *service)
4129 DBG("service %p", service);
4131 if (service == NULL)
4134 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4137 service->state_ipv4 = service->state_ipv6 =
4138 CONNMAN_SERVICE_STATE_UNKNOWN;
4139 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
4141 if (service->favorite == TRUE)
4142 set_reconnect_state(service, TRUE);
4144 __connman_service_ipconfig_indicate_state(service,
4145 CONNMAN_SERVICE_STATE_IDLE,
4146 CONNMAN_IPCONFIG_TYPE_IPV6);
4149 * Toggling the IPv6 state to IDLE could trigger the auto connect
4150 * machinery and consequently the IPv4 state.
4152 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4153 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4156 return __connman_service_ipconfig_indicate_state(service,
4157 CONNMAN_SERVICE_STATE_IDLE,
4158 CONNMAN_IPCONFIG_TYPE_IPV4);
4161 int __connman_service_indicate_default(struct connman_service *service)
4163 DBG("service %p", service);
4170 enum connman_service_state __connman_service_ipconfig_get_state(
4171 struct connman_service *service,
4172 enum connman_ipconfig_type type)
4174 if (service == NULL)
4175 return CONNMAN_SERVICE_STATE_UNKNOWN;
4177 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4178 return service->state_ipv4;
4180 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4181 return service->state_ipv6;
4183 return CONNMAN_SERVICE_STATE_UNKNOWN;
4186 static void check_proxy_setup(struct connman_service *service)
4189 * We start WPAD if we haven't got a PAC URL from DHCP and
4190 * if our proxy manual configuration is either empty or set
4191 * to AUTO with an empty URL.
4194 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4197 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4198 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4199 service->pac != NULL))
4202 if (__connman_wpad_start(service) < 0) {
4203 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4204 __connman_notifier_proxy_changed(service);
4211 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
4215 * How many networks are connected at the same time. If more than 1,
4216 * then set the rp_filter setting properly (loose mode routing) so that network
4217 * connectivity works ok. This is only done for IPv4 networks as IPv6
4218 * does not have rp_filter knob.
4220 static int connected_networks_count;
4221 static int original_rp_filter;
4223 static void service_rp_filter(struct connman_service *service,
4226 enum connman_ipconfig_method method;
4228 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4231 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
4232 case CONNMAN_IPCONFIG_METHOD_OFF:
4233 case CONNMAN_IPCONFIG_METHOD_AUTO:
4235 case CONNMAN_IPCONFIG_METHOD_FIXED:
4236 case CONNMAN_IPCONFIG_METHOD_MANUAL:
4237 case CONNMAN_IPCONFIG_METHOD_DHCP:
4241 if (connected == TRUE) {
4242 if (connected_networks_count == 1) {
4244 filter_value = __connman_ipconfig_set_rp_filter();
4245 if (filter_value < 0)
4248 original_rp_filter = filter_value;
4250 connected_networks_count++;
4253 if (connected_networks_count == 2)
4254 __connman_ipconfig_unset_rp_filter(original_rp_filter);
4256 connected_networks_count--;
4257 if (connected_networks_count < 0)
4258 connected_networks_count = 0;
4261 DBG("%s %s ipconfig %p method %d count %d filter %d",
4262 connected ? "connected" : "disconnected", service->identifier,
4263 service->ipconfig_ipv4, method,
4264 connected_networks_count, original_rp_filter);
4267 static gboolean redo_wispr(gpointer user_data)
4269 struct connman_service *service = user_data;
4273 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV6);
4278 int __connman_service_online_check_failed(struct connman_service *service,
4279 enum connman_ipconfig_type type)
4281 DBG("service %p type %d count %d", service, type,
4282 service->online_check_count);
4284 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4285 /* currently we only retry IPv6 stuff */
4288 if (service->online_check_count != 1)
4291 service->online_check_count = 0;
4294 * We set the timeout to 1 sec so that we have a chance to get
4295 * necessary IPv6 router advertisement messages that might have
4298 g_timeout_add_seconds(1, redo_wispr, service);
4303 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
4304 enum connman_service_state new_state,
4305 enum connman_ipconfig_type type)
4307 struct connman_ipconfig *ipconfig = NULL;
4308 enum connman_service_state old_state;
4311 if (service == NULL)
4314 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4315 old_state = service->state_ipv4;
4316 ipconfig = service->ipconfig_ipv4;
4317 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4318 old_state = service->state_ipv6;
4319 ipconfig = service->ipconfig_ipv6;
4322 if (ipconfig == NULL)
4326 if (old_state == new_state)
4329 DBG("service %p (%s) state %d (%s) type %d (%s)",
4330 service, service ? service->identifier : NULL,
4331 new_state, state2string(new_state),
4332 type, __connman_ipconfig_type2string(type));
4334 switch (new_state) {
4335 case CONNMAN_SERVICE_STATE_UNKNOWN:
4336 case CONNMAN_SERVICE_STATE_IDLE:
4337 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
4340 case CONNMAN_SERVICE_STATE_ASSOCIATION:
4342 case CONNMAN_SERVICE_STATE_CONFIGURATION:
4343 __connman_ipconfig_enable(ipconfig);
4345 case CONNMAN_SERVICE_STATE_READY:
4346 update_nameservers(service);
4348 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4349 check_proxy_setup(service);
4350 service_rp_filter(service, TRUE);
4352 service->online_check_count = 1;
4353 __connman_wispr_start(service, type);
4356 case CONNMAN_SERVICE_STATE_ONLINE:
4358 case CONNMAN_SERVICE_STATE_DISCONNECT:
4359 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
4362 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4363 service_rp_filter(service, FALSE);
4366 case CONNMAN_SERVICE_STATE_FAILURE:
4370 /* We keep that state */
4371 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4372 service->state_ipv4 = new_state;
4373 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4374 service->state_ipv6 = new_state;
4376 ret = service_indicate_state(service);
4379 * If the ipconfig method is OFF, then we set the state to IDLE
4380 * so that it will not affect the combined state in the future.
4382 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4383 enum connman_ipconfig_method method;
4384 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4385 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4386 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4387 service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
4388 ret = service_indicate_state(service);
4391 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4392 enum connman_ipconfig_method method;
4393 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4394 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4395 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4396 service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4397 ret = service_indicate_state(service);
4404 static connman_bool_t prepare_network(struct connman_service *service)
4406 enum connman_network_type type;
4407 unsigned int ssid_len;
4409 type = connman_network_get_type(service->network);
4412 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4413 case CONNMAN_NETWORK_TYPE_VENDOR:
4415 case CONNMAN_NETWORK_TYPE_WIFI:
4416 if (connman_network_get_blob(service->network, "WiFi.SSID",
4420 if (service->passphrase != NULL)
4421 connman_network_set_string(service->network,
4422 "WiFi.Passphrase", service->passphrase);
4424 case CONNMAN_NETWORK_TYPE_ETHERNET:
4425 case CONNMAN_NETWORK_TYPE_WIMAX:
4426 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4427 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4428 case CONNMAN_NETWORK_TYPE_CELLULAR:
4435 static void prepare_8021x(struct connman_service *service)
4437 if (service->eap != NULL)
4438 connman_network_set_string(service->network, "WiFi.EAP",
4441 if (service->identity != NULL)
4442 connman_network_set_string(service->network, "WiFi.Identity",
4445 if (service->ca_cert_file != NULL)
4446 connman_network_set_string(service->network, "WiFi.CACertFile",
4447 service->ca_cert_file);
4449 if (service->client_cert_file != NULL)
4450 connman_network_set_string(service->network,
4451 "WiFi.ClientCertFile",
4452 service->client_cert_file);
4454 if (service->private_key_file != NULL)
4455 connman_network_set_string(service->network,
4456 "WiFi.PrivateKeyFile",
4457 service->private_key_file);
4459 if (service->private_key_passphrase != NULL)
4460 connman_network_set_string(service->network,
4461 "WiFi.PrivateKeyPassphrase",
4462 service->private_key_passphrase);
4464 if (service->phase2 != NULL)
4465 connman_network_set_string(service->network, "WiFi.Phase2",
4469 static int service_connect(struct connman_service *service)
4473 if (service->hidden == TRUE)
4476 switch (service->type) {
4477 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4478 case CONNMAN_SERVICE_TYPE_SYSTEM:
4479 case CONNMAN_SERVICE_TYPE_GPS:
4480 case CONNMAN_SERVICE_TYPE_GADGET:
4482 case CONNMAN_SERVICE_TYPE_ETHERNET:
4483 case CONNMAN_SERVICE_TYPE_WIMAX:
4484 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4485 case CONNMAN_SERVICE_TYPE_CELLULAR:
4486 case CONNMAN_SERVICE_TYPE_VPN:
4488 case CONNMAN_SERVICE_TYPE_WIFI:
4489 switch (service->security) {
4490 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4491 case CONNMAN_SERVICE_SECURITY_NONE:
4493 case CONNMAN_SERVICE_SECURITY_WEP:
4494 case CONNMAN_SERVICE_SECURITY_PSK:
4495 case CONNMAN_SERVICE_SECURITY_WPA:
4496 case CONNMAN_SERVICE_SECURITY_RSN:
4497 if (service->passphrase == NULL) {
4498 if (service->network == NULL)
4501 if (service->wps == FALSE ||
4502 connman_network_get_bool(
4504 "WiFi.UseWPS") == FALSE)
4508 case CONNMAN_SERVICE_SECURITY_8021X:
4509 if (service->eap == NULL)
4513 * never request credentials if using EAP-TLS
4514 * (EAP-TLS networks need to be fully provisioned)
4516 if (g_str_equal(service->eap, "tls") == TRUE)
4520 * Return -ENOKEY if either identity or passphrase is
4521 * missing. Agent provided credentials can be used as
4522 * fallback if needed.
4524 if ((service->identity == NULL &&
4525 service->agent_identity == NULL) ||
4526 (service->passphrase == NULL &&
4527 service->agent_passphrase == NULL))
4535 if (service->network != NULL) {
4536 if (prepare_network(service) == FALSE)
4539 switch (service->security) {
4540 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4541 case CONNMAN_SERVICE_SECURITY_NONE:
4542 case CONNMAN_SERVICE_SECURITY_WEP:
4543 case CONNMAN_SERVICE_SECURITY_PSK:
4544 case CONNMAN_SERVICE_SECURITY_WPA:
4545 case CONNMAN_SERVICE_SECURITY_RSN:
4547 case CONNMAN_SERVICE_SECURITY_8021X:
4548 prepare_8021x(service);
4552 if (__connman_stats_service_register(service) == 0) {
4553 __connman_stats_get(service, FALSE,
4554 &service->stats.data);
4555 __connman_stats_get(service, TRUE,
4556 &service->stats_roaming.data);
4559 if (service->ipconfig_ipv4)
4560 __connman_ipconfig_enable(service->ipconfig_ipv4);
4561 if (service->ipconfig_ipv6)
4562 __connman_ipconfig_enable(service->ipconfig_ipv6);
4564 err = __connman_network_connect(service->network);
4565 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4566 service->provider != NULL)
4567 err = __connman_provider_connect(service->provider);
4572 if (err != -EINPROGRESS) {
4573 __connman_ipconfig_disable(service->ipconfig_ipv4);
4574 __connman_ipconfig_disable(service->ipconfig_ipv6);
4575 __connman_stats_service_unregister(service);
4583 int __connman_service_connect(struct connman_service *service)
4587 DBG("service %p state %s", service, state2string(service->state));
4589 if (is_connected(service) == TRUE)
4592 if (is_connecting(service) == TRUE)
4595 switch (service->type) {
4596 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4597 case CONNMAN_SERVICE_TYPE_SYSTEM:
4598 case CONNMAN_SERVICE_TYPE_GPS:
4599 case CONNMAN_SERVICE_TYPE_GADGET:
4602 err = service_connect(service);
4608 if (err == -EINPROGRESS) {
4609 if (service->timeout == 0)
4610 service->timeout = g_timeout_add_seconds(
4611 CONNECT_TIMEOUT, connect_timeout, service);
4613 return -EINPROGRESS;
4616 __connman_service_ipconfig_indicate_state(service,
4617 CONNMAN_SERVICE_STATE_FAILURE,
4618 CONNMAN_IPCONFIG_TYPE_IPV4);
4619 __connman_service_ipconfig_indicate_state(service,
4620 CONNMAN_SERVICE_STATE_FAILURE,
4621 CONNMAN_IPCONFIG_TYPE_IPV6);
4623 if (service->network != NULL)
4624 __connman_network_disconnect(service->network);
4625 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4626 service->provider != NULL)
4627 __connman_provider_disconnect(service->provider);
4629 if (service->userconnect == TRUE) {
4630 if (err == -ENOKEY || err == -EPERM) {
4631 if (__connman_agent_request_passphrase_input(service,
4634 return -EINPROGRESS;
4636 reply_pending(service, -err);
4642 int __connman_service_disconnect(struct connman_service *service)
4646 DBG("service %p", service);
4648 if (service->network != NULL) {
4649 err = __connman_network_disconnect(service->network);
4650 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4651 service->provider != NULL)
4652 err = __connman_provider_disconnect(service->provider);
4656 if (err < 0 && err != -EINPROGRESS)
4659 __connman_6to4_remove(service->ipconfig_ipv4);
4661 if (service->ipconfig_ipv4)
4662 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
4665 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
4668 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
4669 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
4671 __connman_ipconfig_disable(service->ipconfig_ipv4);
4672 __connman_ipconfig_disable(service->ipconfig_ipv6);
4674 __connman_stats_service_unregister(service);
4679 int __connman_service_disconnect_all(void)
4681 GSequenceIter *iter;
4682 GSList *services = NULL, *list;
4686 iter = g_sequence_get_begin_iter(service_list);
4688 while (g_sequence_iter_is_end(iter) == FALSE) {
4689 struct connman_service *service = g_sequence_get(iter);
4691 services = g_slist_prepend(services, service);
4693 iter = g_sequence_iter_next(iter);
4696 for (list = services; list != NULL; list = list->next) {
4697 struct connman_service *service = list->data;
4699 service->ignore = TRUE;
4701 set_reconnect_state(service, FALSE);
4703 __connman_service_disconnect(service);
4713 * lookup_by_identifier:
4714 * @identifier: service identifier
4716 * Look up a service by identifier (reference count will not be increased)
4718 static struct connman_service *lookup_by_identifier(const char *identifier)
4720 GSequenceIter *iter;
4722 iter = g_hash_table_lookup(service_hash, identifier);
4724 return g_sequence_get(iter);
4729 static void provision_changed(gpointer value, gpointer user_data)
4731 struct connman_service *service = value;
4732 char *path = user_data;
4734 __connman_config_provision_service_ident(service, path);
4737 void __connman_service_provision_changed(const char *ident)
4739 g_sequence_foreach(service_list, provision_changed, (void *)ident);
4743 * __connman_service_get:
4744 * @identifier: service identifier
4746 * Look up a service by identifier or create a new one if not found
4748 static struct connman_service *service_get(const char *identifier)
4750 struct connman_service *service;
4751 GSequenceIter *iter;
4753 iter = g_hash_table_lookup(service_hash, identifier);
4755 service = g_sequence_get(iter);
4756 if (service != NULL)
4757 connman_service_ref(service);
4761 service = connman_service_create();
4762 if (service == NULL)
4765 DBG("service %p", service);
4767 service->identifier = g_strdup(identifier);
4769 iter = g_sequence_insert_sorted(service_list, service,
4770 service_compare, NULL);
4772 g_hash_table_insert(service_hash, service->identifier, iter);
4777 static int service_register(struct connman_service *service)
4779 GSequenceIter *iter;
4781 DBG("service %p", service);
4783 if (service->path != NULL)
4786 service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
4787 service->identifier);
4789 DBG("path %s", service->path);
4791 __connman_config_provision_service(service);
4793 service_load(service);
4795 g_dbus_register_interface(connection, service->path,
4796 CONNMAN_SERVICE_INTERFACE,
4797 service_methods, service_signals,
4798 NULL, service, NULL);
4800 iter = g_hash_table_lookup(service_hash, service->identifier);
4802 g_sequence_sort_changed(iter, service_compare, NULL);
4804 services_changed(TRUE);
4809 static void service_up(struct connman_ipconfig *ipconfig)
4811 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
4813 DBG("%s up", __connman_ipconfig_get_ifname(ipconfig));
4815 link_changed(service);
4817 service->stats.valid = FALSE;
4818 service->stats_roaming.valid = FALSE;
4821 static void service_down(struct connman_ipconfig *ipconfig)
4823 DBG("%s down", __connman_ipconfig_get_ifname(ipconfig));
4826 static void service_lower_up(struct connman_ipconfig *ipconfig)
4828 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
4830 DBG("%s lower up", __connman_ipconfig_get_ifname(ipconfig));
4832 stats_start(service);
4835 static void service_lower_down(struct connman_ipconfig *ipconfig)
4837 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
4839 DBG("%s lower down", __connman_ipconfig_get_ifname(ipconfig));
4841 stats_stop(service);
4842 service_save(service);
4845 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4847 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
4848 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4849 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4851 DBG("%s ip bound", __connman_ipconfig_get_ifname(ipconfig));
4853 type = __connman_ipconfig_get_config_type(ipconfig);
4854 method = __connman_ipconfig_get_method(ipconfig);
4856 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4859 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4860 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4861 __connman_service_ipconfig_indicate_state(service,
4862 CONNMAN_SERVICE_STATE_READY,
4863 CONNMAN_IPCONFIG_TYPE_IPV6);
4865 settings_changed(service, ipconfig);
4868 static void service_ip_release(struct connman_ipconfig *ipconfig)
4870 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
4871 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4872 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4874 DBG("%s ip release", __connman_ipconfig_get_ifname(ipconfig));
4876 type = __connman_ipconfig_get_config_type(ipconfig);
4877 method = __connman_ipconfig_get_method(ipconfig);
4879 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4882 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4883 method == CONNMAN_IPCONFIG_METHOD_OFF)
4884 __connman_service_ipconfig_indicate_state(service,
4885 CONNMAN_SERVICE_STATE_DISCONNECT,
4886 CONNMAN_IPCONFIG_TYPE_IPV6);
4888 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4889 method == CONNMAN_IPCONFIG_METHOD_OFF)
4890 __connman_service_ipconfig_indicate_state(service,
4891 CONNMAN_SERVICE_STATE_DISCONNECT,
4892 CONNMAN_IPCONFIG_TYPE_IPV4);
4894 settings_changed(service, ipconfig);
4897 static const struct connman_ipconfig_ops service_ops = {
4899 .down = service_down,
4900 .lower_up = service_lower_up,
4901 .lower_down = service_lower_down,
4902 .ip_bound = service_ip_bound,
4903 .ip_release = service_ip_release,
4906 static void setup_ip4config(struct connman_service *service, int index,
4907 enum connman_ipconfig_method method)
4909 service->ipconfig_ipv4 = __connman_ipconfig_create(index,
4910 CONNMAN_IPCONFIG_TYPE_IPV4);
4911 if (service->ipconfig_ipv4 == NULL)
4914 __connman_ipconfig_set_method(service->ipconfig_ipv4, method);
4916 __connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4918 __connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4921 static void setup_ip6config(struct connman_service *service, int index)
4923 service->ipconfig_ipv6 = __connman_ipconfig_create(index,
4924 CONNMAN_IPCONFIG_TYPE_IPV6);
4925 if (service->ipconfig_ipv6 == NULL)
4928 __connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4930 __connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4933 void __connman_service_read_ip4config(struct connman_service *service)
4937 if (service->ipconfig_ipv4 == NULL)
4940 keyfile = connman_storage_load_service(service->identifier);
4941 if (keyfile == NULL)
4944 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4945 service->identifier, "IPv4.");
4947 g_key_file_free(keyfile);
4950 void __connman_service_create_ip4config(struct connman_service *service,
4953 DBG("ipv4 %p", service->ipconfig_ipv4);
4955 if (service->ipconfig_ipv4 != NULL)
4958 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4959 __connman_service_read_ip4config(service);
4962 void __connman_service_read_ip6config(struct connman_service *service)
4966 if (service->ipconfig_ipv6 == NULL)
4969 keyfile = connman_storage_load_service(service->identifier);
4970 if (keyfile == NULL)
4973 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4974 service->identifier, "IPv6.");
4976 g_key_file_free(keyfile);
4979 void __connman_service_create_ip6config(struct connman_service *service,
4982 DBG("ipv6 %p", service->ipconfig_ipv6);
4984 if (service->ipconfig_ipv6 != NULL)
4987 setup_ip6config(service, index);
4989 __connman_service_read_ip6config(service);
4993 * __connman_service_lookup_from_network:
4994 * @network: network structure
4996 * Look up a service by network (reference count will not be increased)
4998 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
5000 struct connman_service *service;
5001 const char *ident, *group;
5004 DBG("network %p", network);
5006 if (network == NULL)
5009 ident = __connman_network_get_ident(network);
5013 group = connman_network_get_group(network);
5017 name = g_strdup_printf("%s_%s_%s",
5018 __connman_network_get_type(network), ident, group);
5019 service = lookup_by_identifier(name);
5025 struct connman_service *__connman_service_lookup_from_index(int index)
5027 struct connman_service *service;
5028 GSequenceIter *iter;
5030 iter = g_sequence_get_begin_iter(service_list);
5032 while (g_sequence_iter_is_end(iter) == FALSE) {
5033 service = g_sequence_get(iter);
5035 if (__connman_ipconfig_get_index(service->ipconfig_ipv4)
5039 if (__connman_ipconfig_get_index(service->ipconfig_ipv6)
5043 iter = g_sequence_iter_next(iter);
5049 const char *__connman_service_get_ident(struct connman_service *service)
5051 return service->identifier;
5054 const char *__connman_service_get_path(struct connman_service *service)
5056 return service->path;
5059 unsigned int __connman_service_get_order(struct connman_service *service)
5061 GSequenceIter *iter;
5063 if (service == NULL)
5066 if (service->favorite == FALSE) {
5071 iter = g_hash_table_lookup(service_hash, service->identifier);
5073 if (g_sequence_iter_get_position(iter) == 0)
5075 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
5076 service->order = 10;
5082 return service->order;
5085 static enum connman_service_type convert_network_type(struct connman_network *network)
5087 enum connman_network_type type = connman_network_get_type(network);
5090 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5091 case CONNMAN_NETWORK_TYPE_VENDOR:
5093 case CONNMAN_NETWORK_TYPE_ETHERNET:
5094 return CONNMAN_SERVICE_TYPE_ETHERNET;
5095 case CONNMAN_NETWORK_TYPE_WIFI:
5096 return CONNMAN_SERVICE_TYPE_WIFI;
5097 case CONNMAN_NETWORK_TYPE_WIMAX:
5098 return CONNMAN_SERVICE_TYPE_WIMAX;
5099 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5100 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5101 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5102 case CONNMAN_NETWORK_TYPE_CELLULAR:
5103 return CONNMAN_SERVICE_TYPE_CELLULAR;
5106 return CONNMAN_SERVICE_TYPE_UNKNOWN;
5109 static enum connman_service_security convert_wifi_security(const char *security)
5111 if (security == NULL)
5112 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5113 else if (g_str_equal(security, "none") == TRUE)
5114 return CONNMAN_SERVICE_SECURITY_NONE;
5115 else if (g_str_equal(security, "wep") == TRUE)
5116 return CONNMAN_SERVICE_SECURITY_WEP;
5117 else if (g_str_equal(security, "psk") == TRUE)
5118 return CONNMAN_SERVICE_SECURITY_PSK;
5119 else if (g_str_equal(security, "ieee8021x") == TRUE)
5120 return CONNMAN_SERVICE_SECURITY_8021X;
5121 else if (g_str_equal(security, "wpa") == TRUE)
5122 return CONNMAN_SERVICE_SECURITY_WPA;
5123 else if (g_str_equal(security, "rsn") == TRUE)
5124 return CONNMAN_SERVICE_SECURITY_RSN;
5126 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5129 static void update_from_network(struct connman_service *service,
5130 struct connman_network *network)
5132 connman_uint8_t strength = service->strength;
5133 GSequenceIter *iter;
5136 DBG("service %p network %p", service, network);
5138 if (is_connected(service) == TRUE)
5141 if (is_connecting(service) == TRUE)
5144 str = connman_network_get_string(network, "Name");
5146 g_free(service->name);
5147 service->name = g_strdup(str);
5148 service->hidden = FALSE;
5150 g_free(service->name);
5151 service->name = NULL;
5152 service->hidden = TRUE;
5155 service->strength = connman_network_get_strength(network);
5156 service->roaming = connman_network_get_bool(network, "Roaming");
5158 if (service->strength == 0) {
5160 * Filter out 0-values; it's unclear what they mean
5161 * and they cause anomalous sorting of the priority list.
5163 service->strength = strength;
5166 str = connman_network_get_string(network, "WiFi.Security");
5167 service->security = convert_wifi_security(str);
5169 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5170 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5172 if (service->strength > strength && service->network != NULL) {
5173 service->network = network;
5175 strength_changed(service);
5178 if (service->network == NULL)
5179 service->network = network;
5181 iter = g_hash_table_lookup(service_hash, service->identifier);
5183 g_sequence_sort_changed(iter, service_compare, NULL);
5187 * __connman_service_create_from_network:
5188 * @network: network structure
5190 * Look up service by network and if not found, create one
5192 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5194 struct connman_service *service;
5195 struct connman_device *device;
5196 const char *ident, *group;
5200 DBG("network %p", network);
5202 if (network == NULL)
5205 ident = __connman_network_get_ident(network);
5209 group = connman_network_get_group(network);
5213 name = g_strdup_printf("%s_%s_%s",
5214 __connman_network_get_type(network), ident, group);
5215 service = service_get(name);
5218 if (service == NULL)
5221 if (__connman_network_get_weakness(network) == TRUE)
5224 if (service->path != NULL) {
5225 update_from_network(service, network);
5226 services_changed(TRUE);
5230 service->type = convert_network_type(network);
5232 switch (service->type) {
5233 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5234 case CONNMAN_SERVICE_TYPE_SYSTEM:
5235 case CONNMAN_SERVICE_TYPE_ETHERNET:
5236 case CONNMAN_SERVICE_TYPE_WIMAX:
5237 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5238 case CONNMAN_SERVICE_TYPE_GPS:
5239 case CONNMAN_SERVICE_TYPE_VPN:
5240 case CONNMAN_SERVICE_TYPE_GADGET:
5241 service->autoconnect = FALSE;
5243 case CONNMAN_SERVICE_TYPE_WIFI:
5244 case CONNMAN_SERVICE_TYPE_CELLULAR:
5245 service->autoconnect = TRUE;
5249 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5250 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5252 update_from_network(service, network);
5254 index = connman_network_get_index(network);
5256 if (service->ipconfig_ipv4 == NULL)
5257 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5259 if (service->ipconfig_ipv6 == NULL)
5260 setup_ip6config(service, index);
5262 service_register(service);
5264 if (service->favorite == TRUE) {
5265 device = connman_network_get_device(service->network);
5266 if (device && __connman_device_scanning(device) == FALSE)
5267 __connman_service_auto_connect();
5270 __connman_notifier_service_add(service, service->name);
5275 void __connman_service_update_from_network(struct connman_network *network)
5277 connman_bool_t need_sort = FALSE;
5278 struct connman_service *service;
5279 connman_uint8_t strength;
5280 connman_bool_t roaming;
5281 GSequenceIter *iter;
5283 connman_bool_t stats_enable;
5285 DBG("network %p", network);
5287 service = __connman_service_lookup_from_network(network);
5288 if (service == NULL)
5291 if (service->network == NULL)
5294 name = connman_network_get_string(service->network, "Name");
5295 if (g_strcmp0(service->name, name) != 0) {
5296 g_free(service->name);
5297 service->name = g_strdup(name);
5298 connman_dbus_property_changed_basic(service->path,
5299 CONNMAN_SERVICE_INTERFACE, "Name",
5300 DBUS_TYPE_STRING, &service->name);
5303 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5304 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5306 strength = connman_network_get_strength(service->network);
5307 if (strength == service->strength)
5310 service->strength = strength;
5313 strength_changed(service);
5316 roaming = connman_network_get_bool(service->network, "Roaming");
5317 if (roaming == service->roaming)
5320 stats_enable = stats_enabled(service);
5321 if (stats_enable == TRUE)
5322 stats_stop(service);
5324 service->roaming = roaming;
5327 if (stats_enable == TRUE)
5328 stats_start(service);
5330 roaming_changed(service);
5333 if (need_sort == TRUE) {
5334 iter = g_hash_table_lookup(service_hash, service->identifier);
5336 g_sequence_sort_changed(iter, service_compare, NULL);
5340 void __connman_service_remove_from_network(struct connman_network *network)
5342 struct connman_service *service;
5344 DBG("network %p", network);
5346 service = __connman_service_lookup_from_network(network);
5347 if (service == NULL)
5350 __connman_connection_gateway_remove(service,
5351 CONNMAN_IPCONFIG_TYPE_ALL);
5353 __connman_service_put(service);
5357 * __connman_service_create_from_provider:
5358 * @provider: provider structure
5360 * Look up service by provider and if not found, create one
5362 struct connman_service *
5363 __connman_service_create_from_provider(struct connman_provider *provider)
5365 struct connman_service *service;
5366 const char *ident, *str;
5368 int index = connman_provider_get_index(provider);
5370 DBG("provider %p", provider);
5372 ident = __connman_provider_get_ident(provider);
5376 name = g_strdup_printf("vpn_%s", ident);
5377 service = service_get(name);
5380 if (service == NULL)
5383 service->type = CONNMAN_SERVICE_TYPE_VPN;
5384 service->provider = connman_provider_ref(provider);
5385 service->autoconnect = FALSE;
5386 service->userconnect = TRUE;
5388 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5390 str = connman_provider_get_string(provider, "Name");
5392 g_free(service->name);
5393 service->name = g_strdup(str);
5394 service->hidden = FALSE;
5396 g_free(service->name);
5397 service->name = NULL;
5398 service->hidden = TRUE;
5401 service->strength = 0;
5403 if (service->ipconfig_ipv4 == NULL)
5404 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5406 if (service->ipconfig_ipv6 == NULL)
5407 setup_ip6config(service, index);
5409 service_register(service);
5411 __connman_notifier_service_add(service, service->name);
5416 int __connman_service_init(void)
5420 connection = connman_dbus_get_connection();
5422 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5425 service_list = g_sequence_new(service_free);
5430 void __connman_service_cleanup(void)
5436 list = service_list;
5437 service_list = NULL;
5438 g_sequence_free(list);
5440 g_hash_table_destroy(service_hash);
5441 service_hash = NULL;
5443 g_slist_free(counter_list);
5444 counter_list = NULL;
5446 dbus_connection_unref(connection);