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 char **connman_service_get_timeservers(struct connman_service *service)
2159 if (service == NULL)
2162 if (service->timeservers != NULL)
2163 return service->timeservers;
2168 void connman_service_set_proxy_method(struct connman_service *service,
2169 enum connman_service_proxy_method method)
2171 if (service == NULL || service->hidden == TRUE)
2174 service->proxy = method;
2176 proxy_changed(service);
2178 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
2179 __connman_notifier_proxy_changed(service);
2182 enum connman_service_proxy_method connman_service_get_proxy_method(
2183 struct connman_service *service)
2185 if (service == NULL)
2186 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2188 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
2189 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
2190 service->pac == NULL)
2191 return service->proxy;
2193 return service->proxy_config;
2196 return service->proxy;
2199 char **connman_service_get_proxy_servers(struct connman_service *service)
2201 return g_strdupv(service->proxies);
2204 char **connman_service_get_proxy_excludes(struct connman_service *service)
2206 return g_strdupv(service->excludes);
2209 const char *connman_service_get_proxy_url(struct connman_service *service)
2211 if (service == NULL)
2214 return service->pac;
2217 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
2220 if (service == NULL || service->hidden == TRUE)
2223 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
2225 if (service->ipconfig_ipv4) {
2226 if (__connman_ipconfig_set_proxy_autoconfig(
2227 service->ipconfig_ipv4, url) < 0)
2229 } else if (service->ipconfig_ipv6) {
2230 if (__connman_ipconfig_set_proxy_autoconfig(
2231 service->ipconfig_ipv6, url) < 0)
2236 proxy_changed(service);
2238 __connman_notifier_proxy_changed(service);
2241 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
2243 if (service == NULL)
2246 if (service->ipconfig_ipv4)
2247 return __connman_ipconfig_get_proxy_autoconfig(
2248 service->ipconfig_ipv4);
2249 else if (service->ipconfig_ipv6)
2250 return __connman_ipconfig_get_proxy_autoconfig(
2251 service->ipconfig_ipv6);
2255 int __connman_service_timeserver_append(struct connman_service *service,
2256 const char *timeserver)
2260 DBG("service %p timeserver %s", service, timeserver);
2262 if (timeserver == NULL)
2265 if (service->timeservers != NULL) {
2268 for (i = 0; service->timeservers[i] != NULL; i++)
2269 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2272 len = g_strv_length(service->timeservers);
2273 service->timeservers = g_try_renew(char *, service->timeservers,
2277 service->timeservers = g_try_new0(char *, len + 2);
2280 if (service->timeservers == NULL)
2283 service->timeservers[len] = g_strdup(timeserver);
2284 service->timeservers[len + 1] = NULL;
2289 int __connman_service_timeserver_remove(struct connman_service *service,
2290 const char *timeserver)
2293 int len, i, j, found = 0;
2295 DBG("service %p timeserver %s", service, timeserver);
2297 if (timeserver == NULL)
2300 if (service->timeservers == NULL)
2303 for (i = 0; service->timeservers != NULL &&
2304 service->timeservers[i] != NULL; i++)
2305 if (g_strcmp0(service->timeservers[i], timeserver) == 0) {
2313 len = g_strv_length(service->timeservers);
2316 g_strfreev(service->timeservers);
2317 service->timeservers = NULL;
2322 servers = g_try_new0(char *, len);
2323 if (servers == NULL)
2326 for (i = 0, j = 0; i < len; i++) {
2327 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2328 servers[j] = g_strdup(service->timeservers[i]);
2329 if (servers[j] == NULL)
2334 servers[len - 1] = NULL;
2336 g_strfreev(service->timeservers);
2337 service->timeservers = servers;
2342 void __connman_service_set_pac(struct connman_service *service,
2345 if (service->hidden == TRUE)
2347 g_free(service->pac);
2348 service->pac = g_strdup(pac);
2350 proxy_changed(service);
2353 void __connman_service_set_identity(struct connman_service *service,
2354 const char *identity)
2356 if (service->immutable || service->hidden == TRUE)
2359 g_free(service->identity);
2360 service->identity = g_strdup(identity);
2362 if (service->network != NULL)
2363 connman_network_set_string(service->network,
2368 void __connman_service_set_agent_identity(struct connman_service *service,
2369 const char *agent_identity)
2371 if (service->hidden == TRUE)
2373 g_free(service->agent_identity);
2374 service->agent_identity = g_strdup(agent_identity);
2376 if (service->network != NULL)
2377 connman_network_set_string(service->network,
2378 "WiFi.AgentIdentity",
2379 service->agent_identity);
2382 void __connman_service_set_passphrase(struct connman_service *service,
2383 const char* passphrase)
2385 if (service->immutable == TRUE || service->hidden == TRUE)
2388 g_free(service->passphrase);
2389 service->passphrase = g_strdup(passphrase);
2391 if (service->network != NULL)
2392 connman_network_set_string(service->network,
2394 service->passphrase);
2396 service_save(service);
2399 void __connman_service_set_agent_passphrase(struct connman_service *service,
2400 const char *agent_passphrase)
2402 if (service->hidden == TRUE)
2404 g_free(service->agent_passphrase);
2405 service->agent_passphrase = g_strdup(agent_passphrase);
2407 if (service->network != NULL)
2408 connman_network_set_string(service->network,
2409 "WiFi.AgentPassphrase",
2410 service->agent_passphrase);
2413 static DBusMessage *get_properties(DBusConnection *conn,
2414 DBusMessage *msg, void *user_data)
2416 struct connman_service *service = user_data;
2418 DBusMessageIter array, dict;
2420 DBG("service %p", service);
2422 reply = dbus_message_new_method_return(msg);
2426 dbus_message_iter_init_append(reply, &array);
2428 connman_dbus_dict_open(&array, &dict);
2429 append_properties(&dict, FALSE, service);
2430 connman_dbus_dict_close(&array, &dict);
2435 static int update_proxy_configuration(struct connman_service *service,
2436 DBusMessageIter *array)
2438 DBusMessageIter dict;
2439 enum connman_service_proxy_method method;
2440 GString *servers_str = NULL;
2441 GString *excludes_str = NULL;
2442 const char *url = NULL;
2444 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2446 dbus_message_iter_recurse(array, &dict);
2448 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2449 DBusMessageIter entry, variant;
2453 dbus_message_iter_recurse(&dict, &entry);
2455 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2458 dbus_message_iter_get_basic(&entry, &key);
2459 dbus_message_iter_next(&entry);
2461 if (dbus_message_iter_get_arg_type(&entry) !=
2465 dbus_message_iter_recurse(&entry, &variant);
2466 type = dbus_message_iter_get_arg_type(&variant);
2468 if (g_str_equal(key, "Method") == TRUE) {
2471 if (type != DBUS_TYPE_STRING)
2474 dbus_message_iter_get_basic(&variant, &val);
2475 method = string2proxymethod(val);
2476 } else if (g_str_equal(key, "URL") == TRUE) {
2477 if (type != DBUS_TYPE_STRING)
2480 dbus_message_iter_get_basic(&variant, &url);
2481 } else if (g_str_equal(key, "Servers") == TRUE) {
2482 DBusMessageIter str_array;
2484 if (type != DBUS_TYPE_ARRAY)
2487 servers_str = g_string_new(NULL);
2488 if (servers_str == NULL)
2491 dbus_message_iter_recurse(&variant, &str_array);
2493 while (dbus_message_iter_get_arg_type(&str_array) ==
2497 dbus_message_iter_get_basic(&str_array, &val);
2499 if (servers_str->len > 0)
2500 g_string_append_printf(servers_str,
2503 g_string_append(servers_str, val);
2505 dbus_message_iter_next(&str_array);
2507 } else if (g_str_equal(key, "Excludes") == TRUE) {
2508 DBusMessageIter str_array;
2510 if (type != DBUS_TYPE_ARRAY)
2513 excludes_str = g_string_new(NULL);
2514 if (excludes_str == NULL)
2517 dbus_message_iter_recurse(&variant, &str_array);
2519 while (dbus_message_iter_get_arg_type(&str_array) ==
2523 dbus_message_iter_get_basic(&str_array, &val);
2525 if (excludes_str->len > 0)
2526 g_string_append_printf(excludes_str,
2529 g_string_append(excludes_str, val);
2531 dbus_message_iter_next(&str_array);
2535 dbus_message_iter_next(&dict);
2539 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2541 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2542 if (servers_str == NULL && service->proxies == NULL)
2545 if (servers_str != NULL) {
2546 g_strfreev(service->proxies);
2548 if (servers_str->len > 0)
2549 service->proxies = g_strsplit_set(
2550 servers_str->str, " ", 0);
2552 service->proxies = NULL;
2555 if (excludes_str != NULL) {
2556 g_strfreev(service->excludes);
2558 if (excludes_str->len > 0)
2559 service->excludes = g_strsplit_set(
2560 excludes_str->str, " ", 0);
2562 service->excludes = NULL;
2565 if (service->proxies == NULL)
2566 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2569 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2570 g_free(service->pac);
2572 if (url != NULL && strlen(url) > 0)
2573 service->pac = g_strdup(url);
2575 service->pac = NULL;
2577 /* if we are connected:
2578 - if service->pac == NULL
2579 - if __connman_ipconfig_get_proxy_autoconfig(
2580 service->ipconfig) == NULL
2581 --> We should start WPAD */
2584 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2588 if (servers_str != NULL)
2589 g_string_free(servers_str, TRUE);
2591 if (excludes_str != NULL)
2592 g_string_free(excludes_str, TRUE);
2594 service->proxy_config = method;
2599 if (servers_str != NULL)
2600 g_string_free(servers_str, TRUE);
2602 if (excludes_str != NULL)
2603 g_string_free(excludes_str, TRUE);
2608 static int set_ipconfig(struct connman_service *service,
2609 struct connman_ipconfig *ipconfig,
2610 DBusMessageIter *array,
2611 enum connman_service_state state,
2612 enum connman_service_state *new_state)
2614 enum connman_ipconfig_method old_method;
2615 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2616 enum connman_ipconfig_type type;
2619 if (ipconfig == NULL)
2622 old_method = __connman_ipconfig_get_method(ipconfig);
2624 if (is_connecting_state(service, state) ||
2625 is_connected_state(service, state))
2626 __connman_network_clear_ipconfig(service->network, ipconfig);
2628 err = __connman_ipconfig_set_config(ipconfig, array);
2629 method = __connman_ipconfig_get_method(ipconfig);
2630 type = __connman_ipconfig_get_config_type(ipconfig);
2632 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2633 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2634 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2635 *new_state = service->state_ipv4 =
2636 CONNMAN_SERVICE_STATE_CONFIGURATION;
2637 __connman_ipconfig_enable(ipconfig);
2640 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2641 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2642 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2643 *new_state = service->state_ipv6;
2644 __connman_ipconfig_enable(ipconfig);
2648 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2649 type, method, state2string(*new_state));
2654 static DBusMessage *set_property(DBusConnection *conn,
2655 DBusMessage *msg, void *user_data)
2657 struct connman_service *service = user_data;
2658 DBusMessageIter iter, value;
2662 DBG("service %p", service);
2664 if (dbus_message_iter_init(msg, &iter) == FALSE)
2665 return __connman_error_invalid_arguments(msg);
2667 dbus_message_iter_get_basic(&iter, &name);
2668 dbus_message_iter_next(&iter);
2669 dbus_message_iter_recurse(&iter, &value);
2671 type = dbus_message_iter_get_arg_type(&value);
2673 if (g_str_equal(name, "AutoConnect") == TRUE) {
2674 connman_bool_t autoconnect;
2676 if (type != DBUS_TYPE_BOOLEAN)
2677 return __connman_error_invalid_arguments(msg);
2679 if (service->favorite == FALSE)
2680 return __connman_error_invalid_service(msg);
2682 dbus_message_iter_get_basic(&value, &autoconnect);
2684 if (service->autoconnect == autoconnect)
2685 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2687 service->autoconnect = autoconnect;
2689 autoconnect_changed(service);
2691 service_save(service);
2692 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2693 DBusMessageIter entry;
2698 if (type != DBUS_TYPE_ARRAY)
2699 return __connman_error_invalid_arguments(msg);
2701 str = g_string_new(NULL);
2703 return __connman_error_invalid_arguments(msg);
2705 index = connman_network_get_index(service->network);
2706 gw = __connman_ipconfig_get_gateway_from_index(index);
2708 if (gw && strlen(gw))
2709 __connman_service_nameserver_del_routes(service);
2711 dbus_message_iter_recurse(&value, &entry);
2713 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2715 dbus_message_iter_get_basic(&entry, &val);
2716 dbus_message_iter_next(&entry);
2718 g_string_append_printf(str, " %s", val);
2720 g_string_append(str, val);
2723 g_strfreev(service->nameservers_config);
2726 service->nameservers_config =
2727 g_strsplit_set(str->str, " ", 0);
2729 service->nameservers_config = NULL;
2732 g_string_free(str, TRUE);
2734 if (gw && strlen(gw))
2735 __connman_service_nameserver_add_routes(service, gw);
2737 update_nameservers(service);
2738 dns_configuration_changed(service);
2740 service_save(service);
2741 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2742 DBusMessageIter entry;
2745 if (type != DBUS_TYPE_ARRAY)
2746 return __connman_error_invalid_arguments(msg);
2748 str = g_string_new(NULL);
2750 return __connman_error_invalid_arguments(msg);
2752 dbus_message_iter_recurse(&value, &entry);
2754 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2756 dbus_message_iter_get_basic(&entry, &val);
2757 dbus_message_iter_next(&entry);
2759 g_string_append_printf(str, " %s", val);
2761 g_string_append(str, val);
2764 g_strfreev(service->domains);
2767 service->domains = g_strsplit_set(str->str, " ", 0);
2769 service->domains = NULL;
2771 g_string_free(str, TRUE);
2773 update_nameservers(service);
2774 domain_configuration_changed(service);
2776 service_save(service);
2777 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2780 if (type != DBUS_TYPE_ARRAY)
2781 return __connman_error_invalid_arguments(msg);
2783 err = update_proxy_configuration(service, &value);
2786 return __connman_error_failed(msg, -err);
2788 proxy_configuration_changed(service);
2790 __connman_notifier_proxy_changed(service);
2792 service_save(service);
2793 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2794 g_str_equal(name, "IPv6.Configuration")) {
2796 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2797 enum connman_service_state state =
2798 CONNMAN_SERVICE_STATE_UNKNOWN;
2803 if (service->ipconfig_ipv4 == NULL &&
2804 service->ipconfig_ipv6 == NULL)
2805 return __connman_error_invalid_property(msg);
2807 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2808 ipv4 = service->ipconfig_ipv4;
2809 err = set_ipconfig(service, ipv4, &value,
2810 service->state_ipv4, &state);
2812 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2813 ipv6 = service->ipconfig_ipv6;
2814 err = set_ipconfig(service, ipv6, &value,
2815 service->state_ipv6, &state);
2819 if (is_connected_state(service, state) ||
2820 is_connecting_state(service, state))
2821 __connman_network_set_ipconfig(service->network,
2823 return __connman_error_failed(msg, -err);
2827 ipv4_configuration_changed(service);
2829 ipv6_configuration_changed(service);
2831 if (is_connecting(service) || is_connected(service))
2832 __connman_network_set_ipconfig(service->network,
2835 service_save(service);
2837 return __connman_error_invalid_property(msg);
2839 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2842 static void set_idle(struct connman_service *service)
2844 service->state = service->state_ipv4 = service->state_ipv6 =
2845 CONNMAN_SERVICE_STATE_IDLE;
2846 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2847 state_changed(service);
2850 static DBusMessage *clear_property(DBusConnection *conn,
2851 DBusMessage *msg, void *user_data)
2853 struct connman_service *service = user_data;
2856 DBG("service %p", service);
2858 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2861 if (g_str_equal(name, "Error") == TRUE) {
2864 g_get_current_time(&service->modified);
2865 service_save(service);
2867 return __connman_error_invalid_property(msg);
2869 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2872 static connman_bool_t is_ignore(struct connman_service *service)
2874 if (service->autoconnect == FALSE)
2877 if (service->roaming == TRUE)
2880 if (service->ignore == TRUE)
2883 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
2889 void __connman_service_auto_connect(void)
2891 struct connman_service *service = NULL;
2892 GSequenceIter *iter;
2896 if (__connman_session_mode() == TRUE) {
2897 DBG("Session mode enabled: auto connect disabled");
2901 iter = g_sequence_get_begin_iter(service_list);
2903 while (g_sequence_iter_is_end(iter) == FALSE) {
2904 service = g_sequence_get(iter);
2906 if (service->pending != NULL)
2909 if (is_connecting(service) == TRUE)
2912 if (service->favorite == FALSE)
2915 if (is_connected(service) == TRUE)
2918 if (is_ignore(service) == FALSE && service->state ==
2919 CONNMAN_SERVICE_STATE_IDLE)
2924 iter = g_sequence_iter_next(iter);
2927 if (service != NULL) {
2928 service->userconnect = FALSE;
2929 __connman_service_connect(service);
2933 static void remove_timeout(struct connman_service *service)
2935 if (service->timeout > 0) {
2936 g_source_remove(service->timeout);
2937 service->timeout = 0;
2941 static void reply_pending(struct connman_service *service, int error)
2943 remove_timeout(service);
2945 if (service->pending != NULL) {
2949 reply = __connman_error_failed(service->pending,
2952 g_dbus_send_message(connection, reply);
2956 sender = dbus_message_get_interface(service->pending);
2958 DBG("sender %s", sender);
2960 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2961 g_dbus_send_reply(connection, service->pending,
2962 DBUS_TYPE_OBJECT_PATH, &service->path,
2965 g_dbus_send_reply(connection, service->pending,
2969 dbus_message_unref(service->pending);
2970 service->pending = NULL;
2974 static gboolean connect_timeout(gpointer user_data)
2976 struct connman_service *service = user_data;
2977 connman_bool_t autoconnect = FALSE;
2979 DBG("service %p", service);
2981 service->timeout = 0;
2983 if (service->network != NULL)
2984 __connman_network_disconnect(service->network);
2986 __connman_ipconfig_disable(service->ipconfig_ipv4);
2987 __connman_ipconfig_disable(service->ipconfig_ipv6);
2989 __connman_stats_service_unregister(service);
2991 if (service->pending != NULL) {
2994 reply = __connman_error_operation_timeout(service->pending);
2996 g_dbus_send_message(connection, reply);
2998 dbus_message_unref(service->pending);
2999 service->pending = NULL;
3003 __connman_service_ipconfig_indicate_state(service,
3004 CONNMAN_SERVICE_STATE_FAILURE,
3005 CONNMAN_IPCONFIG_TYPE_IPV4);
3006 __connman_service_ipconfig_indicate_state(service,
3007 CONNMAN_SERVICE_STATE_FAILURE,
3008 CONNMAN_IPCONFIG_TYPE_IPV6);
3010 if (autoconnect == TRUE && service->userconnect == FALSE)
3011 __connman_service_auto_connect();
3016 static void set_reconnect_state(struct connman_service *service,
3017 connman_bool_t reconnect)
3019 struct connman_device *device;
3021 if (service->network == NULL)
3024 device = connman_network_get_device(service->network);
3028 __connman_device_set_reconnect(device, reconnect);
3031 static connman_bool_t get_reconnect_state(struct connman_service *service)
3033 struct connman_device *device;
3035 if (service->network == NULL)
3038 device = connman_network_get_device(service->network);
3042 return __connman_device_get_reconnect(device);
3045 static DBusMessage *connect_service(DBusConnection *conn,
3046 DBusMessage *msg, void *user_data)
3048 struct connman_service *service = user_data;
3049 GSequenceIter *iter;
3052 DBG("service %p", service);
3054 if (service->pending != NULL)
3055 return __connman_error_in_progress(msg);
3057 iter = g_sequence_get_begin_iter(service_list);
3059 while (g_sequence_iter_is_end(iter) == FALSE) {
3060 struct connman_service *temp = g_sequence_get(iter);
3062 if (service->type == temp->type && is_connecting(temp) == TRUE)
3063 return __connman_error_in_progress(msg);
3065 iter = g_sequence_iter_next(iter);
3068 service->ignore = FALSE;
3070 service->userconnect = TRUE;
3072 service->pending = dbus_message_ref(msg);
3074 set_reconnect_state(service, FALSE);
3076 err = __connman_service_connect(service);
3078 if (service->pending == NULL)
3081 if (err != -EINPROGRESS) {
3082 dbus_message_unref(service->pending);
3083 service->pending = NULL;
3085 return __connman_error_failed(msg, -err);
3091 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3094 static DBusMessage *disconnect_service(DBusConnection *conn,
3095 DBusMessage *msg, void *user_data)
3097 struct connman_service *service = user_data;
3100 DBG("service %p", service);
3102 reply_pending(service, ECONNABORTED);
3104 service->ignore = TRUE;
3106 set_reconnect_state(service, FALSE);
3108 err = __connman_service_disconnect(service);
3110 if (err != -EINPROGRESS)
3111 return __connman_error_failed(msg, -err);
3116 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3119 static DBusMessage *remove_service(DBusConnection *conn,
3120 DBusMessage *msg, void *user_data)
3122 struct connman_service *service = user_data;
3124 DBG("service %p", service);
3126 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
3127 return __connman_error_not_supported(msg);
3129 if (service->immutable == TRUE || service->hidden == TRUE)
3130 return __connman_error_not_supported(msg);
3132 if (service->favorite == FALSE && service->state !=
3133 CONNMAN_SERVICE_STATE_FAILURE)
3134 return __connman_error_not_supported(msg);
3136 set_reconnect_state(service, FALSE);
3138 __connman_service_disconnect(service);
3140 g_free(service->passphrase);
3141 service->passphrase = NULL;
3145 __connman_service_set_favorite(service, FALSE);
3146 service_save(service);
3148 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3151 static gboolean check_suitable_state(enum connman_service_state a,
3152 enum connman_service_state b)
3155 * Special check so that "ready" service can be moved before
3158 if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
3159 b == CONNMAN_SERVICE_STATE_READY) ||
3160 (b == CONNMAN_SERVICE_STATE_ONLINE &&
3161 a == CONNMAN_SERVICE_STATE_READY))
3167 static void downgrade_state(struct connman_service *service)
3169 if (service == NULL)
3172 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
3173 service->state_ipv6);
3175 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
3176 __connman_service_ipconfig_indicate_state(service,
3177 CONNMAN_SERVICE_STATE_READY,
3178 CONNMAN_IPCONFIG_TYPE_IPV4);
3180 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
3181 __connman_service_ipconfig_indicate_state(service,
3182 CONNMAN_SERVICE_STATE_READY,
3183 CONNMAN_IPCONFIG_TYPE_IPV6);
3186 static void apply_relevant_default_downgrade(struct connman_service *service)
3188 struct connman_service *def_service;
3190 def_service = get_default();
3191 if (def_service == NULL)
3194 if (def_service == service &&
3195 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3196 def_service->state = CONNMAN_SERVICE_STATE_READY;
3199 static DBusMessage *move_service(DBusConnection *conn,
3200 DBusMessage *msg, void *user_data,
3203 struct connman_service *service = user_data;
3204 struct connman_service *target;
3206 GSequenceIter *src, *dst;
3207 enum connman_ipconfig_method target4, target6;
3208 enum connman_ipconfig_method service4, service6;
3210 DBG("service %p", service);
3212 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3215 if (service->favorite == FALSE)
3216 return __connman_error_not_supported(msg);
3218 target = find_service(path);
3219 if (target == NULL || target->favorite == FALSE || target == service ||
3220 target->type == CONNMAN_SERVICE_TYPE_VPN)
3221 return __connman_error_invalid_service(msg);
3223 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3224 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3225 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3226 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3228 DBG("target %s method %d/%d state %d/%d", target->identifier,
3230 target->state_ipv4, target->state_ipv6);
3232 DBG("service %s method %d/%d state %d/%d", service->identifier,
3234 service->state_ipv4, service->state_ipv6);
3237 * If method is OFF, then we do not need to check the corresponding
3240 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3241 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3242 if (check_suitable_state(target->state_ipv6,
3243 service->state_ipv6) == FALSE)
3244 return __connman_error_invalid_service(msg);
3248 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3249 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3250 if (check_suitable_state(target->state_ipv4,
3251 service->state_ipv4) == FALSE)
3252 return __connman_error_invalid_service(msg);
3256 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3257 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3258 if (check_suitable_state(target->state_ipv6,
3259 service->state_ipv6) == FALSE)
3260 return __connman_error_invalid_service(msg);
3264 if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3265 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3266 if (check_suitable_state(target->state_ipv4,
3267 service->state_ipv4) == FALSE)
3268 return __connman_error_invalid_service(msg);
3272 g_get_current_time(&service->modified);
3273 service_save(service);
3275 src = g_hash_table_lookup(service_hash, service->identifier);
3276 dst = g_hash_table_lookup(service_hash, target->identifier);
3279 * If the service which goes down is the default service and is
3280 * online, we downgrade directly its state to ready so:
3281 * the service which goes up, needs to recompute its state which
3282 * is triggered via downgrading it - if relevant - to state ready.
3284 if (before == TRUE) {
3285 apply_relevant_default_downgrade(target);
3286 g_sequence_move(src, dst);
3287 downgrade_state(service);
3289 apply_relevant_default_downgrade(service);
3290 g_sequence_move(dst, src);
3291 downgrade_state(target);
3294 services_changed(FALSE);
3296 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3299 static DBusMessage *move_before(DBusConnection *conn,
3300 DBusMessage *msg, void *user_data)
3302 return move_service(conn, msg, user_data, TRUE);
3305 static DBusMessage *move_after(DBusConnection *conn,
3306 DBusMessage *msg, void *user_data)
3308 return move_service(conn, msg, user_data, FALSE);
3311 static DBusMessage *reset_counters(DBusConnection *conn,
3312 DBusMessage *msg, void *user_data)
3314 struct connman_service *service = user_data;
3316 reset_stats(service);
3318 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3321 static GDBusMethodTable service_methods[] = {
3322 { "GetProperties", "", "a{sv}", get_properties },
3323 { "SetProperty", "sv", "", set_property },
3324 { "ClearProperty", "s", "", clear_property },
3325 { "Connect", "", "", connect_service,
3326 G_DBUS_METHOD_FLAG_ASYNC },
3327 { "Disconnect", "", "", disconnect_service },
3328 { "Remove", "", "", remove_service },
3329 { "MoveBefore", "o", "", move_before },
3330 { "MoveAfter", "o", "", move_after },
3331 { "ResetCounters", "", "", reset_counters },
3335 static GDBusSignalTable service_signals[] = {
3336 { "PropertyChanged", "sv" },
3340 static void service_free(gpointer user_data)
3342 struct connman_service *service = user_data;
3343 char *path = service->path;
3345 DBG("service %p", service);
3347 reply_pending(service, ENOENT);
3349 g_hash_table_remove(service_hash, service->identifier);
3351 __connman_notifier_service_remove(service);
3353 stats_stop(service);
3355 service->path = NULL;
3358 services_changed(FALSE);
3360 g_dbus_unregister_interface(connection, path,
3361 CONNMAN_SERVICE_INTERFACE);
3365 g_hash_table_destroy(service->counter_table);
3367 if (service->network != NULL) {
3368 __connman_network_disconnect(service->network);
3369 connman_network_unref(service->network);
3370 service->network = NULL;
3373 if (service->provider != NULL)
3374 connman_provider_unref(service->provider);
3376 if (service->ipconfig_ipv4 != NULL) {
3377 __connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3378 __connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3379 __connman_ipconfig_unref(service->ipconfig_ipv4);
3380 service->ipconfig_ipv4 = NULL;
3383 if (service->ipconfig_ipv6 != NULL) {
3384 __connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3385 __connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3386 __connman_ipconfig_unref(service->ipconfig_ipv6);
3387 service->ipconfig_ipv6 = NULL;
3390 g_strfreev(service->timeservers);
3391 g_strfreev(service->nameservers);
3392 g_strfreev(service->nameservers_config);
3393 g_strfreev(service->nameservers_auto);
3394 g_strfreev(service->domains);
3395 g_strfreev(service->proxies);
3396 g_strfreev(service->excludes);
3398 g_free(service->domainname);
3399 g_free(service->pac);
3400 g_free(service->name);
3401 g_free(service->passphrase);
3402 g_free(service->agent_passphrase);
3403 g_free(service->identifier);
3404 g_free(service->eap);
3405 g_free(service->identity);
3406 g_free(service->agent_identity);
3407 g_free(service->ca_cert_file);
3408 g_free(service->client_cert_file);
3409 g_free(service->private_key_file);
3410 g_free(service->private_key_passphrase);
3411 g_free(service->phase2);
3413 if (service->stats.timer != NULL)
3414 g_timer_destroy(service->stats.timer);
3415 if (service->stats_roaming.timer != NULL)
3416 g_timer_destroy(service->stats_roaming.timer);
3421 static void stats_init(struct connman_service *service)
3424 service->stats.valid = FALSE;
3425 service->stats.enabled = FALSE;
3426 service->stats.timer = g_timer_new();
3429 service->stats_roaming.valid = FALSE;
3430 service->stats_roaming.enabled = FALSE;
3431 service->stats_roaming.timer = g_timer_new();
3434 static void service_initialize(struct connman_service *service)
3436 DBG("service %p", service);
3438 service->refcount = 1;
3439 service->session_usage_count = 0;
3441 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3442 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3444 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3445 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3446 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3448 service->favorite = FALSE;
3449 service->immutable = FALSE;
3450 service->hidden = FALSE;
3452 service->ignore = FALSE;
3454 service->userconnect = FALSE;
3458 stats_init(service);
3460 service->provider = NULL;
3462 service->wps = FALSE;
3466 * connman_service_create:
3468 * Allocate a new service.
3470 * Returns: a newly-allocated #connman_service structure
3472 struct connman_service *connman_service_create(void)
3475 struct connman_stats_counter *counters;
3476 const char *counter;
3478 struct connman_service *service;
3480 service = g_try_new0(struct connman_service, 1);
3481 if (service == NULL)
3484 DBG("service %p", service);
3486 service->counter_table = g_hash_table_new_full(g_str_hash,
3487 g_str_equal, NULL, g_free);
3489 for (list = counter_list; list; list = list->next) {
3490 counter = list->data;
3492 counters = g_try_new0(struct connman_stats_counter, 1);
3493 if (counters == NULL) {
3494 g_hash_table_destroy(service->counter_table);
3499 counters->append_all = TRUE;
3501 g_hash_table_replace(service->counter_table, (gpointer)counter,
3505 service_initialize(service);
3511 * connman_service_ref:
3512 * @service: service structure
3514 * Increase reference counter of service
3516 struct connman_service *
3517 connman_service_ref_debug(struct connman_service *service,
3518 const char *file, int line, const char *caller)
3520 DBG("%p ref %d by %s:%d:%s()", service, service->refcount + 1,
3521 file, line, caller);
3523 __sync_fetch_and_add(&service->refcount, 1);
3529 * connman_service_unref:
3530 * @service: service structure
3532 * Decrease reference counter of service and release service if no
3535 void connman_service_unref_debug(struct connman_service *service,
3536 const char *file, int line, const char *caller)
3538 GSequenceIter *iter;
3540 DBG("%p ref %d by %s:%d:%s()", service, service->refcount - 1,
3541 file, line, caller);
3543 if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
3546 iter = g_hash_table_lookup(service_hash, service->identifier);
3548 reply_pending(service, ECONNABORTED);
3550 __connman_service_disconnect(service);
3552 g_sequence_remove(iter);
3554 service_free(service);
3558 static gint service_compare(gconstpointer a, gconstpointer b,
3561 struct connman_service *service_a = (void *) a;
3562 struct connman_service *service_b = (void *) b;
3563 enum connman_service_state state_a, state_b;
3565 state_a = service_a->state;
3566 state_b = service_b->state;
3568 if (state_a != state_b) {
3569 gboolean a_connected = is_connected(service_a);
3570 gboolean b_connected = is_connected(service_b);
3572 if (a_connected == TRUE && b_connected == TRUE) {
3573 /* We prefer online over ready state */
3574 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
3577 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
3581 if (a_connected == TRUE)
3583 if (b_connected == TRUE)
3586 if (is_connecting(service_a) == TRUE)
3588 if (is_connecting(service_b) == TRUE)
3592 if (service_a->order > service_b->order)
3595 if (service_a->order < service_b->order)
3598 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3601 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3604 if (service_a->type != service_b->type) {
3605 switch (service_a->type) {
3606 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3607 case CONNMAN_SERVICE_TYPE_SYSTEM:
3608 case CONNMAN_SERVICE_TYPE_ETHERNET:
3609 case CONNMAN_SERVICE_TYPE_GPS:
3610 case CONNMAN_SERVICE_TYPE_VPN:
3611 case CONNMAN_SERVICE_TYPE_GADGET:
3613 case CONNMAN_SERVICE_TYPE_WIFI:
3615 case CONNMAN_SERVICE_TYPE_WIMAX:
3616 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3617 case CONNMAN_SERVICE_TYPE_CELLULAR:
3622 return (gint) service_b->strength - (gint) service_a->strength;
3626 * connman_service_get_type:
3627 * @service: service structure
3629 * Get the type of service
3631 enum connman_service_type connman_service_get_type(struct connman_service *service)
3633 if (service == NULL)
3634 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3636 return service->type;
3640 * connman_service_get_interface:
3641 * @service: service structure
3643 * Get network interface of service
3645 char *connman_service_get_interface(struct connman_service *service)
3649 if (service == NULL)
3652 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3653 if (service->ipconfig_ipv4)
3654 index = __connman_ipconfig_get_index(
3655 service->ipconfig_ipv4);
3656 else if (service->ipconfig_ipv6)
3657 index = __connman_ipconfig_get_index(
3658 service->ipconfig_ipv6);
3662 return connman_inet_ifname(index);
3665 if (service->network == NULL)
3668 index = connman_network_get_index(service->network);
3670 return connman_inet_ifname(index);
3674 * connman_service_get_network:
3675 * @service: service structure
3677 * Get the service network
3679 struct connman_network *
3680 __connman_service_get_network(struct connman_service *service)
3682 if (service == NULL)
3685 return service->network;
3688 struct connman_ipconfig *
3689 __connman_service_get_ip4config(struct connman_service *service)
3691 if (service == NULL)
3694 return service->ipconfig_ipv4;
3697 struct connman_ipconfig *
3698 __connman_service_get_ip6config(struct connman_service *service)
3700 if (service == NULL)
3703 return service->ipconfig_ipv6;
3706 struct connman_ipconfig *
3707 __connman_service_get_ipconfig(struct connman_service *service, int family)
3709 if (family == AF_INET)
3710 return __connman_service_get_ip4config(service);
3711 else if (family == AF_INET6)
3712 return __connman_service_get_ip6config(service);
3718 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3720 if (service == NULL)
3721 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3723 return service->security;
3726 const char *__connman_service_get_phase2(struct connman_service *service)
3728 if (service == NULL)
3731 return service->phase2;
3734 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3736 if (service == NULL)
3739 return service->wps;
3743 * __connman_service_set_favorite:
3744 * @service: service structure
3745 * @favorite: favorite value
3747 * Change the favorite setting of service
3749 int __connman_service_set_favorite(struct connman_service *service,
3750 connman_bool_t favorite)
3752 GSequenceIter *iter;
3754 if (service->hidden == TRUE)
3756 iter = g_hash_table_lookup(service_hash, service->identifier);
3760 if (service->favorite == favorite)
3763 service->favorite = favorite;
3764 service->order = __connman_service_get_order(service);
3766 favorite_changed(service);
3768 g_sequence_sort_changed(iter, service_compare, NULL);
3770 services_changed(FALSE);
3775 int __connman_service_set_immutable(struct connman_service *service,
3776 connman_bool_t immutable)
3778 if (service->hidden == TRUE)
3780 service->immutable = immutable;
3782 immutable_changed(service);
3787 void __connman_service_set_string(struct connman_service *service,
3788 const char *key, const char *value)
3790 if (service->hidden == TRUE)
3792 if (g_str_equal(key, "EAP") == TRUE) {
3793 g_free(service->eap);
3794 service->eap = g_strdup(value);
3795 } else if (g_str_equal(key, "Identity") == TRUE) {
3796 g_free(service->identity);
3797 service->identity = g_strdup(value);
3798 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3799 g_free(service->ca_cert_file);
3800 service->ca_cert_file = g_strdup(value);
3801 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3802 g_free(service->client_cert_file);
3803 service->client_cert_file = g_strdup(value);
3804 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3805 g_free(service->private_key_file);
3806 service->private_key_file = g_strdup(value);
3807 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3808 g_free(service->private_key_passphrase);
3809 service->private_key_passphrase = g_strdup(value);
3810 } else if (g_str_equal(key, "Phase2") == TRUE) {
3811 g_free(service->phase2);
3812 service->phase2 = g_strdup(value);
3813 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3814 g_free(service->passphrase);
3815 service->passphrase = g_strdup(value);
3819 static void service_complete(struct connman_service *service)
3821 reply_pending(service, EIO);
3823 if (service->userconnect == FALSE)
3824 __connman_service_auto_connect();
3826 g_get_current_time(&service->modified);
3827 service_save(service);
3830 static void report_error_cb(struct connman_service *service,
3831 gboolean retry, void *user_data)
3834 __connman_service_connect(service);
3836 service_complete(service);
3837 services_changed(FALSE);
3838 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3842 void __connman_service_add_passphrase(struct connman_service *service,
3843 const gchar *passphrase)
3845 switch (service->security) {
3846 case CONNMAN_SERVICE_SECURITY_WEP:
3847 case CONNMAN_SERVICE_SECURITY_PSK:
3848 __connman_service_set_passphrase(service, passphrase);
3850 case CONNMAN_SERVICE_SECURITY_8021X:
3851 __connman_service_set_agent_passphrase(service,
3854 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3855 case CONNMAN_SERVICE_SECURITY_NONE:
3856 case CONNMAN_SERVICE_SECURITY_WPA:
3857 case CONNMAN_SERVICE_SECURITY_RSN:
3858 DBG("service security '%s' (%d) not handled",
3859 security2string(service->security),
3866 static void request_input_cb (struct connman_service *service,
3867 connman_bool_t values_received,
3868 const char *name, int name_len,
3869 const char *identity, const char *passphrase,
3872 struct connman_device *device;
3874 DBG ("RequestInput return, %p", service);
3876 if (service->hidden == TRUE && name_len > 0 && name_len <= 32) {
3877 device = connman_network_get_device(service->network);
3878 __connman_device_request_hidden_scan(device,
3880 identity, passphrase);
3883 if (values_received == FALSE || service->hidden == TRUE) {
3884 service_complete(service);
3885 services_changed(FALSE);
3886 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3890 if (identity != NULL)
3891 __connman_service_set_agent_identity(service, identity);
3893 if (passphrase != NULL)
3894 __connman_service_add_passphrase(service, passphrase);
3896 __connman_service_connect(service);
3898 /* Never cache agent provided credentials */
3899 __connman_service_set_agent_identity(service, NULL);
3900 __connman_service_set_agent_passphrase(service, NULL);
3903 static void downgrade_connected_services(void)
3905 struct connman_service *up_service;
3906 GSequenceIter *iter;
3908 iter = g_sequence_get_begin_iter(service_list);
3909 while (g_sequence_iter_is_end(iter) == FALSE) {
3910 up_service = g_sequence_get(iter);
3912 if (is_connected(up_service) == FALSE) {
3913 iter = g_sequence_iter_next(iter);
3917 if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3920 downgrade_state(up_service);
3922 iter = g_sequence_iter_next(iter);
3926 static int service_indicate_state(struct connman_service *service)
3928 enum connman_service_state old_state, new_state;
3929 struct connman_service *def_service;
3930 GSequenceIter *iter;
3932 if (service == NULL)
3935 old_state = service->state;
3936 new_state = combine_state(service->state_ipv4, service->state_ipv6);
3938 DBG("service %p old %s - new %s/%s => %s",
3940 state2string(old_state),
3941 state2string(service->state_ipv4),
3942 state2string(service->state_ipv6),
3943 state2string(new_state));
3945 if (old_state == new_state)
3948 def_service = get_default();
3950 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
3951 if (def_service != NULL && def_service != service &&
3952 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3956 service->state = new_state;
3957 state_changed(service);
3959 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
3960 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3961 reply_pending(service, ECONNABORTED);
3963 __connman_service_disconnect(service);
3966 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3967 if (__connman_stats_service_register(service) == 0) {
3968 __connman_stats_get(service, FALSE,
3969 &service->stats.data);
3970 __connman_stats_get(service, TRUE,
3971 &service->stats_roaming.data);
3975 if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
3976 __connman_timeserver_sync(service);
3978 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
3979 connman_bool_t reconnect;
3981 reconnect = get_reconnect_state(service);
3982 if (reconnect == TRUE)
3983 __connman_service_auto_connect();
3985 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3988 if (new_state == CONNMAN_SERVICE_STATE_READY) {
3989 enum connman_ipconfig_method method;
3991 set_reconnect_state(service, TRUE);
3993 __connman_service_set_favorite(service, TRUE);
3995 reply_pending(service, 0);
3997 service->userconnect = FALSE;
3999 g_get_current_time(&service->modified);
4000 service_save(service);
4002 update_nameservers(service);
4003 dns_changed(service);
4004 domain_changed(service);
4006 __connman_notifier_connect(service->type);
4008 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4009 connman_network_get_bool(service->network,
4010 "WiFi.UseWPS") == TRUE) {
4013 pass = connman_network_get_string(service->network,
4016 __connman_service_set_passphrase(service, pass);
4018 connman_network_set_bool(service->network,
4019 "WiFi.UseWPS", FALSE);
4024 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4025 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4026 __connman_ipconfig_disable_ipv6(
4027 service->ipconfig_ipv6);
4029 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4030 def_service = get_default();
4032 if (__connman_notifier_count_connected() == 0 &&
4033 def_service != NULL &&
4034 def_service->provider != NULL)
4035 __connman_provider_disconnect(def_service->provider);
4039 __connman_wispr_stop(service);
4041 __connman_wpad_stop(service);
4043 update_nameservers(service);
4044 dns_changed(service);
4045 domain_changed(service);
4047 __connman_timeserver_stop();
4049 __connman_notifier_disconnect(service->type);
4052 * Previous services which are connected and which states
4053 * are set to online should reset relevantly ipconfig_state
4054 * to ready so wispr/portal will be rerun on those
4056 downgrade_connected_services();
4059 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4060 if (service->userconnect == TRUE &&
4061 __connman_agent_report_error(service,
4062 error2string(service->error),
4063 report_error_cb, NULL) == -EIO)
4065 service_complete(service);
4067 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4069 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4071 iter = g_hash_table_lookup(service_hash, service->identifier);
4073 g_sequence_sort_changed(iter, service_compare, NULL);
4075 services_changed(FALSE);
4077 if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
4083 int __connman_service_indicate_error(struct connman_service *service,
4084 enum connman_service_error error)
4086 DBG("service %p error %d", service, error);
4088 if (service == NULL)
4091 service->error = error;
4093 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4094 __connman_service_set_passphrase(service, NULL);
4096 __connman_service_ipconfig_indicate_state(service,
4097 CONNMAN_SERVICE_STATE_FAILURE,
4098 CONNMAN_IPCONFIG_TYPE_IPV4);
4099 __connman_service_ipconfig_indicate_state(service,
4100 CONNMAN_SERVICE_STATE_FAILURE,
4101 CONNMAN_IPCONFIG_TYPE_IPV6);
4105 int __connman_service_clear_error(struct connman_service *service)
4107 DBG("service %p", service);
4109 if (service == NULL)
4112 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4115 service->state_ipv4 = service->state_ipv6 =
4116 CONNMAN_SERVICE_STATE_UNKNOWN;
4117 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
4119 if (service->favorite == TRUE)
4120 set_reconnect_state(service, TRUE);
4122 __connman_service_ipconfig_indicate_state(service,
4123 CONNMAN_SERVICE_STATE_IDLE,
4124 CONNMAN_IPCONFIG_TYPE_IPV6);
4127 * Toggling the IPv6 state to IDLE could trigger the auto connect
4128 * machinery and consequently the IPv4 state.
4130 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4131 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4134 return __connman_service_ipconfig_indicate_state(service,
4135 CONNMAN_SERVICE_STATE_IDLE,
4136 CONNMAN_IPCONFIG_TYPE_IPV4);
4139 int __connman_service_indicate_default(struct connman_service *service)
4141 DBG("service %p", service);
4148 enum connman_service_state __connman_service_ipconfig_get_state(
4149 struct connman_service *service,
4150 enum connman_ipconfig_type type)
4152 if (service == NULL)
4153 return CONNMAN_SERVICE_STATE_UNKNOWN;
4155 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4156 return service->state_ipv4;
4158 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4159 return service->state_ipv6;
4161 return CONNMAN_SERVICE_STATE_UNKNOWN;
4164 static void check_proxy_setup(struct connman_service *service)
4167 * We start WPAD if we haven't got a PAC URL from DHCP and
4168 * if our proxy manual configuration is either empty or set
4169 * to AUTO with an empty URL.
4172 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4175 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4176 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4177 service->pac != NULL))
4180 if (__connman_wpad_start(service) < 0) {
4181 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4182 __connman_notifier_proxy_changed(service);
4189 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
4193 * How many networks are connected at the same time. If more than 1,
4194 * then set the rp_filter setting properly (loose mode routing) so that network
4195 * connectivity works ok. This is only done for IPv4 networks as IPv6
4196 * does not have rp_filter knob.
4198 static int connected_networks_count;
4199 static int original_rp_filter;
4201 static void service_rp_filter(struct connman_service *service,
4204 enum connman_ipconfig_method method;
4206 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4209 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
4210 case CONNMAN_IPCONFIG_METHOD_OFF:
4211 case CONNMAN_IPCONFIG_METHOD_AUTO:
4213 case CONNMAN_IPCONFIG_METHOD_FIXED:
4214 case CONNMAN_IPCONFIG_METHOD_MANUAL:
4215 case CONNMAN_IPCONFIG_METHOD_DHCP:
4219 if (connected == TRUE) {
4220 if (connected_networks_count == 1) {
4222 filter_value = __connman_ipconfig_set_rp_filter();
4223 if (filter_value < 0)
4226 original_rp_filter = filter_value;
4228 connected_networks_count++;
4231 if (connected_networks_count == 2)
4232 __connman_ipconfig_unset_rp_filter(original_rp_filter);
4234 connected_networks_count--;
4235 if (connected_networks_count < 0)
4236 connected_networks_count = 0;
4239 DBG("%s %s ipconfig %p method %d count %d filter %d",
4240 connected ? "connected" : "disconnected", service->identifier,
4241 service->ipconfig_ipv4, method,
4242 connected_networks_count, original_rp_filter);
4245 static gboolean redo_wispr(gpointer user_data)
4247 struct connman_service *service = user_data;
4251 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV6);
4256 int __connman_service_online_check_failed(struct connman_service *service,
4257 enum connman_ipconfig_type type)
4259 DBG("service %p type %d count %d", service, type,
4260 service->online_check_count);
4262 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4263 /* currently we only retry IPv6 stuff */
4266 if (service->online_check_count != 1)
4269 service->online_check_count = 0;
4272 * We set the timeout to 1 sec so that we have a chance to get
4273 * necessary IPv6 router advertisement messages that might have
4276 g_timeout_add_seconds(1, redo_wispr, service);
4281 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
4282 enum connman_service_state new_state,
4283 enum connman_ipconfig_type type)
4285 struct connman_ipconfig *ipconfig = NULL;
4286 enum connman_service_state old_state;
4289 if (service == NULL)
4292 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4293 old_state = service->state_ipv4;
4294 ipconfig = service->ipconfig_ipv4;
4295 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4296 old_state = service->state_ipv6;
4297 ipconfig = service->ipconfig_ipv6;
4300 if (ipconfig == NULL)
4304 if (old_state == new_state)
4307 DBG("service %p (%s) state %d (%s) type %d (%s)",
4308 service, service ? service->identifier : NULL,
4309 new_state, state2string(new_state),
4310 type, __connman_ipconfig_type2string(type));
4312 switch (new_state) {
4313 case CONNMAN_SERVICE_STATE_UNKNOWN:
4314 case CONNMAN_SERVICE_STATE_IDLE:
4315 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
4318 case CONNMAN_SERVICE_STATE_ASSOCIATION:
4320 case CONNMAN_SERVICE_STATE_CONFIGURATION:
4321 __connman_ipconfig_enable(ipconfig);
4323 case CONNMAN_SERVICE_STATE_READY:
4324 update_nameservers(service);
4326 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4327 check_proxy_setup(service);
4328 service_rp_filter(service, TRUE);
4330 service->online_check_count = 1;
4331 __connman_wispr_start(service, type);
4334 case CONNMAN_SERVICE_STATE_ONLINE:
4336 case CONNMAN_SERVICE_STATE_DISCONNECT:
4337 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
4340 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4341 service_rp_filter(service, FALSE);
4344 case CONNMAN_SERVICE_STATE_FAILURE:
4348 /* We keep that state */
4349 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4350 service->state_ipv4 = new_state;
4351 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4352 service->state_ipv6 = new_state;
4354 ret = service_indicate_state(service);
4357 * If the ipconfig method is OFF, then we set the state to IDLE
4358 * so that it will not affect the combined state in the future.
4360 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4361 enum connman_ipconfig_method method;
4362 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4363 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4364 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4365 service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
4366 ret = service_indicate_state(service);
4369 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4370 enum connman_ipconfig_method method;
4371 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4372 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4373 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4374 service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4375 ret = service_indicate_state(service);
4382 static connman_bool_t prepare_network(struct connman_service *service)
4384 enum connman_network_type type;
4385 unsigned int ssid_len;
4387 type = connman_network_get_type(service->network);
4390 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4391 case CONNMAN_NETWORK_TYPE_VENDOR:
4393 case CONNMAN_NETWORK_TYPE_WIFI:
4394 if (connman_network_get_blob(service->network, "WiFi.SSID",
4398 if (service->passphrase != NULL)
4399 connman_network_set_string(service->network,
4400 "WiFi.Passphrase", service->passphrase);
4402 case CONNMAN_NETWORK_TYPE_ETHERNET:
4403 case CONNMAN_NETWORK_TYPE_WIMAX:
4404 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4405 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4406 case CONNMAN_NETWORK_TYPE_CELLULAR:
4413 static void prepare_8021x(struct connman_service *service)
4415 if (service->eap != NULL)
4416 connman_network_set_string(service->network, "WiFi.EAP",
4419 if (service->identity != NULL)
4420 connman_network_set_string(service->network, "WiFi.Identity",
4423 if (service->ca_cert_file != NULL)
4424 connman_network_set_string(service->network, "WiFi.CACertFile",
4425 service->ca_cert_file);
4427 if (service->client_cert_file != NULL)
4428 connman_network_set_string(service->network,
4429 "WiFi.ClientCertFile",
4430 service->client_cert_file);
4432 if (service->private_key_file != NULL)
4433 connman_network_set_string(service->network,
4434 "WiFi.PrivateKeyFile",
4435 service->private_key_file);
4437 if (service->private_key_passphrase != NULL)
4438 connman_network_set_string(service->network,
4439 "WiFi.PrivateKeyPassphrase",
4440 service->private_key_passphrase);
4442 if (service->phase2 != NULL)
4443 connman_network_set_string(service->network, "WiFi.Phase2",
4447 static int service_connect(struct connman_service *service)
4451 if (service->hidden == TRUE)
4454 switch (service->type) {
4455 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4456 case CONNMAN_SERVICE_TYPE_SYSTEM:
4457 case CONNMAN_SERVICE_TYPE_GPS:
4458 case CONNMAN_SERVICE_TYPE_GADGET:
4460 case CONNMAN_SERVICE_TYPE_ETHERNET:
4461 case CONNMAN_SERVICE_TYPE_WIMAX:
4462 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4463 case CONNMAN_SERVICE_TYPE_CELLULAR:
4464 case CONNMAN_SERVICE_TYPE_VPN:
4466 case CONNMAN_SERVICE_TYPE_WIFI:
4467 switch (service->security) {
4468 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4469 case CONNMAN_SERVICE_SECURITY_NONE:
4471 case CONNMAN_SERVICE_SECURITY_WEP:
4472 case CONNMAN_SERVICE_SECURITY_PSK:
4473 case CONNMAN_SERVICE_SECURITY_WPA:
4474 case CONNMAN_SERVICE_SECURITY_RSN:
4475 if (service->passphrase == NULL) {
4476 if (service->network == NULL)
4479 if (service->wps == FALSE ||
4480 connman_network_get_bool(
4482 "WiFi.UseWPS") == FALSE)
4486 case CONNMAN_SERVICE_SECURITY_8021X:
4487 if (service->eap == NULL)
4491 * never request credentials if using EAP-TLS
4492 * (EAP-TLS networks need to be fully provisioned)
4494 if (g_str_equal(service->eap, "tls") == TRUE)
4498 * Return -ENOKEY if either identity or passphrase is
4499 * missing. Agent provided credentials can be used as
4500 * fallback if needed.
4502 if ((service->identity == NULL &&
4503 service->agent_identity == NULL) ||
4504 (service->passphrase == NULL &&
4505 service->agent_passphrase == NULL))
4513 if (service->network != NULL) {
4514 if (prepare_network(service) == FALSE)
4517 switch (service->security) {
4518 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4519 case CONNMAN_SERVICE_SECURITY_NONE:
4520 case CONNMAN_SERVICE_SECURITY_WEP:
4521 case CONNMAN_SERVICE_SECURITY_PSK:
4522 case CONNMAN_SERVICE_SECURITY_WPA:
4523 case CONNMAN_SERVICE_SECURITY_RSN:
4525 case CONNMAN_SERVICE_SECURITY_8021X:
4526 prepare_8021x(service);
4530 if (__connman_stats_service_register(service) == 0) {
4531 __connman_stats_get(service, FALSE,
4532 &service->stats.data);
4533 __connman_stats_get(service, TRUE,
4534 &service->stats_roaming.data);
4537 if (service->ipconfig_ipv4)
4538 __connman_ipconfig_enable(service->ipconfig_ipv4);
4539 if (service->ipconfig_ipv6)
4540 __connman_ipconfig_enable(service->ipconfig_ipv6);
4542 err = __connman_network_connect(service->network);
4543 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4544 service->provider != NULL)
4545 err = __connman_provider_connect(service->provider);
4550 if (err != -EINPROGRESS) {
4551 __connman_ipconfig_disable(service->ipconfig_ipv4);
4552 __connman_ipconfig_disable(service->ipconfig_ipv6);
4553 __connman_stats_service_unregister(service);
4561 int __connman_service_connect(struct connman_service *service)
4565 DBG("service %p state %s", service, state2string(service->state));
4567 if (is_connected(service) == TRUE)
4570 if (is_connecting(service) == TRUE)
4573 switch (service->type) {
4574 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4575 case CONNMAN_SERVICE_TYPE_SYSTEM:
4576 case CONNMAN_SERVICE_TYPE_GPS:
4577 case CONNMAN_SERVICE_TYPE_GADGET:
4580 err = service_connect(service);
4586 if (err == -EINPROGRESS) {
4587 if (service->timeout == 0)
4588 service->timeout = g_timeout_add_seconds(
4589 CONNECT_TIMEOUT, connect_timeout, service);
4591 return -EINPROGRESS;
4594 __connman_service_ipconfig_indicate_state(service,
4595 CONNMAN_SERVICE_STATE_FAILURE,
4596 CONNMAN_IPCONFIG_TYPE_IPV4);
4597 __connman_service_ipconfig_indicate_state(service,
4598 CONNMAN_SERVICE_STATE_FAILURE,
4599 CONNMAN_IPCONFIG_TYPE_IPV6);
4601 if (service->network != NULL)
4602 __connman_network_disconnect(service->network);
4603 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4604 service->provider != NULL)
4605 __connman_provider_disconnect(service->provider);
4607 if (service->userconnect == TRUE) {
4608 if (err == -ENOKEY || err == -EPERM) {
4609 if (__connman_agent_request_passphrase_input(service,
4612 return -EINPROGRESS;
4614 reply_pending(service, -err);
4620 int __connman_service_disconnect(struct connman_service *service)
4624 DBG("service %p", service);
4626 if (service->network != NULL) {
4627 err = __connman_network_disconnect(service->network);
4628 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4629 service->provider != NULL)
4630 err = __connman_provider_disconnect(service->provider);
4634 if (err < 0 && err != -EINPROGRESS)
4637 __connman_6to4_remove(service->ipconfig_ipv4);
4639 if (service->ipconfig_ipv4)
4640 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
4643 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
4646 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
4647 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
4649 __connman_ipconfig_disable(service->ipconfig_ipv4);
4650 __connman_ipconfig_disable(service->ipconfig_ipv6);
4652 __connman_stats_service_unregister(service);
4657 int __connman_service_disconnect_all(void)
4659 GSequenceIter *iter;
4660 GSList *services = NULL, *list;
4664 iter = g_sequence_get_begin_iter(service_list);
4666 while (g_sequence_iter_is_end(iter) == FALSE) {
4667 struct connman_service *service = g_sequence_get(iter);
4669 services = g_slist_prepend(services, service);
4671 iter = g_sequence_iter_next(iter);
4674 for (list = services; list != NULL; list = list->next) {
4675 struct connman_service *service = list->data;
4677 service->ignore = TRUE;
4679 set_reconnect_state(service, FALSE);
4681 __connman_service_disconnect(service);
4691 * lookup_by_identifier:
4692 * @identifier: service identifier
4694 * Look up a service by identifier (reference count will not be increased)
4696 static struct connman_service *lookup_by_identifier(const char *identifier)
4698 GSequenceIter *iter;
4700 iter = g_hash_table_lookup(service_hash, identifier);
4702 return g_sequence_get(iter);
4707 static void provision_changed(gpointer value, gpointer user_data)
4709 struct connman_service *service = value;
4710 char *path = user_data;
4712 __connman_config_provision_service_ident(service, path);
4715 void __connman_service_provision_changed(const char *ident)
4717 g_sequence_foreach(service_list, provision_changed, (void *)ident);
4721 * __connman_service_get:
4722 * @identifier: service identifier
4724 * Look up a service by identifier or create a new one if not found
4726 static struct connman_service *service_get(const char *identifier)
4728 struct connman_service *service;
4729 GSequenceIter *iter;
4731 iter = g_hash_table_lookup(service_hash, identifier);
4733 service = g_sequence_get(iter);
4734 if (service != NULL)
4735 connman_service_ref(service);
4739 service = connman_service_create();
4740 if (service == NULL)
4743 DBG("service %p", service);
4745 service->identifier = g_strdup(identifier);
4747 iter = g_sequence_insert_sorted(service_list, service,
4748 service_compare, NULL);
4750 g_hash_table_insert(service_hash, service->identifier, iter);
4755 static int service_register(struct connman_service *service)
4757 GSequenceIter *iter;
4759 DBG("service %p", service);
4761 if (service->path != NULL)
4764 service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
4765 service->identifier);
4767 DBG("path %s", service->path);
4769 __connman_config_provision_service(service);
4771 service_load(service);
4773 g_dbus_register_interface(connection, service->path,
4774 CONNMAN_SERVICE_INTERFACE,
4775 service_methods, service_signals,
4776 NULL, service, NULL);
4778 iter = g_hash_table_lookup(service_hash, service->identifier);
4780 g_sequence_sort_changed(iter, service_compare, NULL);
4782 services_changed(TRUE);
4787 static void service_up(struct connman_ipconfig *ipconfig)
4789 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
4791 DBG("%s up", __connman_ipconfig_get_ifname(ipconfig));
4793 link_changed(service);
4795 service->stats.valid = FALSE;
4796 service->stats_roaming.valid = FALSE;
4799 static void service_down(struct connman_ipconfig *ipconfig)
4801 DBG("%s down", __connman_ipconfig_get_ifname(ipconfig));
4804 static void service_lower_up(struct connman_ipconfig *ipconfig)
4806 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
4808 DBG("%s lower up", __connman_ipconfig_get_ifname(ipconfig));
4810 stats_start(service);
4813 static void service_lower_down(struct connman_ipconfig *ipconfig)
4815 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
4817 DBG("%s lower down", __connman_ipconfig_get_ifname(ipconfig));
4819 stats_stop(service);
4820 service_save(service);
4823 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4825 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
4826 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4827 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4829 DBG("%s ip bound", __connman_ipconfig_get_ifname(ipconfig));
4831 type = __connman_ipconfig_get_config_type(ipconfig);
4832 method = __connman_ipconfig_get_method(ipconfig);
4834 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4837 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4838 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4839 __connman_service_ipconfig_indicate_state(service,
4840 CONNMAN_SERVICE_STATE_READY,
4841 CONNMAN_IPCONFIG_TYPE_IPV6);
4843 settings_changed(service, ipconfig);
4846 static void service_ip_release(struct connman_ipconfig *ipconfig)
4848 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
4849 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4850 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4852 DBG("%s ip release", __connman_ipconfig_get_ifname(ipconfig));
4854 type = __connman_ipconfig_get_config_type(ipconfig);
4855 method = __connman_ipconfig_get_method(ipconfig);
4857 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4860 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4861 method == CONNMAN_IPCONFIG_METHOD_OFF)
4862 __connman_service_ipconfig_indicate_state(service,
4863 CONNMAN_SERVICE_STATE_DISCONNECT,
4864 CONNMAN_IPCONFIG_TYPE_IPV6);
4866 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4867 method == CONNMAN_IPCONFIG_METHOD_OFF)
4868 __connman_service_ipconfig_indicate_state(service,
4869 CONNMAN_SERVICE_STATE_DISCONNECT,
4870 CONNMAN_IPCONFIG_TYPE_IPV4);
4872 settings_changed(service, ipconfig);
4875 static const struct connman_ipconfig_ops service_ops = {
4877 .down = service_down,
4878 .lower_up = service_lower_up,
4879 .lower_down = service_lower_down,
4880 .ip_bound = service_ip_bound,
4881 .ip_release = service_ip_release,
4884 static void setup_ip4config(struct connman_service *service, int index,
4885 enum connman_ipconfig_method method)
4887 service->ipconfig_ipv4 = __connman_ipconfig_create(index,
4888 CONNMAN_IPCONFIG_TYPE_IPV4);
4889 if (service->ipconfig_ipv4 == NULL)
4892 __connman_ipconfig_set_method(service->ipconfig_ipv4, method);
4894 __connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4896 __connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4899 static void setup_ip6config(struct connman_service *service, int index)
4901 service->ipconfig_ipv6 = __connman_ipconfig_create(index,
4902 CONNMAN_IPCONFIG_TYPE_IPV6);
4903 if (service->ipconfig_ipv6 == NULL)
4906 __connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4908 __connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4911 void __connman_service_read_ip4config(struct connman_service *service)
4915 if (service->ipconfig_ipv4 == NULL)
4918 keyfile = connman_storage_load_service(service->identifier);
4919 if (keyfile == NULL)
4922 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4923 service->identifier, "IPv4.");
4925 g_key_file_free(keyfile);
4928 void __connman_service_create_ip4config(struct connman_service *service,
4931 DBG("ipv4 %p", service->ipconfig_ipv4);
4933 if (service->ipconfig_ipv4 != NULL)
4936 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4937 __connman_service_read_ip4config(service);
4940 void __connman_service_read_ip6config(struct connman_service *service)
4944 if (service->ipconfig_ipv6 == NULL)
4947 keyfile = connman_storage_load_service(service->identifier);
4948 if (keyfile == NULL)
4951 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4952 service->identifier, "IPv6.");
4954 g_key_file_free(keyfile);
4957 void __connman_service_create_ip6config(struct connman_service *service,
4960 DBG("ipv6 %p", service->ipconfig_ipv6);
4962 if (service->ipconfig_ipv6 != NULL)
4965 setup_ip6config(service, index);
4967 __connman_service_read_ip6config(service);
4971 * __connman_service_lookup_from_network:
4972 * @network: network structure
4974 * Look up a service by network (reference count will not be increased)
4976 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4978 struct connman_service *service;
4979 const char *ident, *group;
4982 DBG("network %p", network);
4984 if (network == NULL)
4987 ident = __connman_network_get_ident(network);
4991 group = connman_network_get_group(network);
4995 name = g_strdup_printf("%s_%s_%s",
4996 __connman_network_get_type(network), ident, group);
4997 service = lookup_by_identifier(name);
5003 struct connman_service *__connman_service_lookup_from_index(int index)
5005 struct connman_service *service;
5006 GSequenceIter *iter;
5008 iter = g_sequence_get_begin_iter(service_list);
5010 while (g_sequence_iter_is_end(iter) == FALSE) {
5011 service = g_sequence_get(iter);
5013 if (__connman_ipconfig_get_index(service->ipconfig_ipv4)
5017 if (__connman_ipconfig_get_index(service->ipconfig_ipv6)
5021 iter = g_sequence_iter_next(iter);
5027 const char *__connman_service_get_ident(struct connman_service *service)
5029 return service->identifier;
5032 const char *__connman_service_get_path(struct connman_service *service)
5034 return service->path;
5037 unsigned int __connman_service_get_order(struct connman_service *service)
5039 GSequenceIter *iter;
5041 if (service == NULL)
5044 if (service->favorite == FALSE) {
5049 iter = g_hash_table_lookup(service_hash, service->identifier);
5051 if (g_sequence_iter_get_position(iter) == 0)
5053 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
5054 service->order = 10;
5060 return service->order;
5063 static enum connman_service_type convert_network_type(struct connman_network *network)
5065 enum connman_network_type type = connman_network_get_type(network);
5068 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5069 case CONNMAN_NETWORK_TYPE_VENDOR:
5071 case CONNMAN_NETWORK_TYPE_ETHERNET:
5072 return CONNMAN_SERVICE_TYPE_ETHERNET;
5073 case CONNMAN_NETWORK_TYPE_WIFI:
5074 return CONNMAN_SERVICE_TYPE_WIFI;
5075 case CONNMAN_NETWORK_TYPE_WIMAX:
5076 return CONNMAN_SERVICE_TYPE_WIMAX;
5077 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5078 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5079 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5080 case CONNMAN_NETWORK_TYPE_CELLULAR:
5081 return CONNMAN_SERVICE_TYPE_CELLULAR;
5084 return CONNMAN_SERVICE_TYPE_UNKNOWN;
5087 static enum connman_service_security convert_wifi_security(const char *security)
5089 if (security == NULL)
5090 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5091 else if (g_str_equal(security, "none") == TRUE)
5092 return CONNMAN_SERVICE_SECURITY_NONE;
5093 else if (g_str_equal(security, "wep") == TRUE)
5094 return CONNMAN_SERVICE_SECURITY_WEP;
5095 else if (g_str_equal(security, "psk") == TRUE)
5096 return CONNMAN_SERVICE_SECURITY_PSK;
5097 else if (g_str_equal(security, "ieee8021x") == TRUE)
5098 return CONNMAN_SERVICE_SECURITY_8021X;
5099 else if (g_str_equal(security, "wpa") == TRUE)
5100 return CONNMAN_SERVICE_SECURITY_WPA;
5101 else if (g_str_equal(security, "rsn") == TRUE)
5102 return CONNMAN_SERVICE_SECURITY_RSN;
5104 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5107 static void update_from_network(struct connman_service *service,
5108 struct connman_network *network)
5110 connman_uint8_t strength = service->strength;
5111 GSequenceIter *iter;
5114 DBG("service %p network %p", service, network);
5116 if (is_connected(service) == TRUE)
5119 if (is_connecting(service) == TRUE)
5122 str = connman_network_get_string(network, "Name");
5124 g_free(service->name);
5125 service->name = g_strdup(str);
5126 service->hidden = FALSE;
5128 g_free(service->name);
5129 service->name = NULL;
5130 service->hidden = TRUE;
5133 service->strength = connman_network_get_strength(network);
5134 service->roaming = connman_network_get_bool(network, "Roaming");
5136 if (service->strength == 0) {
5138 * Filter out 0-values; it's unclear what they mean
5139 * and they cause anomalous sorting of the priority list.
5141 service->strength = strength;
5144 str = connman_network_get_string(network, "WiFi.Security");
5145 service->security = convert_wifi_security(str);
5147 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5148 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5150 if (service->strength > strength && service->network != NULL) {
5151 connman_network_unref(service->network);
5152 service->network = connman_network_ref(network);
5154 strength_changed(service);
5157 if (service->network == NULL)
5158 service->network = connman_network_ref(network);
5160 iter = g_hash_table_lookup(service_hash, service->identifier);
5162 g_sequence_sort_changed(iter, service_compare, NULL);
5166 * __connman_service_create_from_network:
5167 * @network: network structure
5169 * Look up service by network and if not found, create one
5171 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5173 struct connman_service *service;
5174 struct connman_device *device;
5175 const char *ident, *group;
5179 DBG("network %p", network);
5181 if (network == NULL)
5184 ident = __connman_network_get_ident(network);
5188 group = connman_network_get_group(network);
5192 name = g_strdup_printf("%s_%s_%s",
5193 __connman_network_get_type(network), ident, group);
5194 service = service_get(name);
5197 if (service == NULL)
5200 if (__connman_network_get_weakness(network) == TRUE)
5203 if (service->path != NULL) {
5204 update_from_network(service, network);
5205 services_changed(TRUE);
5209 service->type = convert_network_type(network);
5211 switch (service->type) {
5212 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5213 case CONNMAN_SERVICE_TYPE_SYSTEM:
5214 case CONNMAN_SERVICE_TYPE_ETHERNET:
5215 case CONNMAN_SERVICE_TYPE_WIMAX:
5216 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5217 case CONNMAN_SERVICE_TYPE_GPS:
5218 case CONNMAN_SERVICE_TYPE_VPN:
5219 case CONNMAN_SERVICE_TYPE_GADGET:
5220 service->autoconnect = FALSE;
5222 case CONNMAN_SERVICE_TYPE_WIFI:
5223 case CONNMAN_SERVICE_TYPE_CELLULAR:
5224 service->autoconnect = TRUE;
5228 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5229 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5231 update_from_network(service, network);
5233 index = connman_network_get_index(network);
5235 if (service->ipconfig_ipv4 == NULL)
5236 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5238 if (service->ipconfig_ipv6 == NULL)
5239 setup_ip6config(service, index);
5241 service_register(service);
5243 if (service->favorite == TRUE) {
5244 device = connman_network_get_device(service->network);
5245 if (device && __connman_device_scanning(device) == FALSE)
5246 __connman_service_auto_connect();
5249 __connman_notifier_service_add(service, service->name);
5254 void __connman_service_update_from_network(struct connman_network *network)
5256 connman_bool_t need_sort = FALSE;
5257 struct connman_service *service;
5258 connman_uint8_t strength;
5259 connman_bool_t roaming;
5260 GSequenceIter *iter;
5262 connman_bool_t stats_enable;
5264 DBG("network %p", network);
5266 service = __connman_service_lookup_from_network(network);
5267 if (service == NULL)
5270 if (service->network == NULL)
5273 name = connman_network_get_string(service->network, "Name");
5274 if (g_strcmp0(service->name, name) != 0) {
5275 g_free(service->name);
5276 service->name = g_strdup(name);
5277 connman_dbus_property_changed_basic(service->path,
5278 CONNMAN_SERVICE_INTERFACE, "Name",
5279 DBUS_TYPE_STRING, &service->name);
5282 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5283 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5285 strength = connman_network_get_strength(service->network);
5286 if (strength == service->strength)
5289 service->strength = strength;
5292 strength_changed(service);
5295 roaming = connman_network_get_bool(service->network, "Roaming");
5296 if (roaming == service->roaming)
5299 stats_enable = stats_enabled(service);
5300 if (stats_enable == TRUE)
5301 stats_stop(service);
5303 service->roaming = roaming;
5306 if (stats_enable == TRUE)
5307 stats_start(service);
5309 roaming_changed(service);
5312 if (need_sort == TRUE) {
5313 iter = g_hash_table_lookup(service_hash, service->identifier);
5315 g_sequence_sort_changed(iter, service_compare, NULL);
5319 void __connman_service_remove_from_network(struct connman_network *network)
5321 struct connman_service *service;
5323 DBG("network %p", network);
5325 service = __connman_service_lookup_from_network(network);
5326 if (service == NULL)
5329 __connman_connection_gateway_remove(service,
5330 CONNMAN_IPCONFIG_TYPE_ALL);
5332 connman_service_unref(service);
5336 * __connman_service_create_from_provider:
5337 * @provider: provider structure
5339 * Look up service by provider and if not found, create one
5341 struct connman_service *
5342 __connman_service_create_from_provider(struct connman_provider *provider)
5344 struct connman_service *service;
5345 const char *ident, *str;
5347 int index = connman_provider_get_index(provider);
5349 DBG("provider %p", provider);
5351 ident = __connman_provider_get_ident(provider);
5355 name = g_strdup_printf("vpn_%s", ident);
5356 service = service_get(name);
5359 if (service == NULL)
5362 service->type = CONNMAN_SERVICE_TYPE_VPN;
5363 service->provider = connman_provider_ref(provider);
5364 service->autoconnect = FALSE;
5365 service->userconnect = TRUE;
5367 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5368 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5370 str = connman_provider_get_string(provider, "Name");
5372 g_free(service->name);
5373 service->name = g_strdup(str);
5374 service->hidden = FALSE;
5376 g_free(service->name);
5377 service->name = NULL;
5378 service->hidden = TRUE;
5381 service->strength = 0;
5383 if (service->ipconfig_ipv4 == NULL)
5384 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5386 if (service->ipconfig_ipv6 == NULL)
5387 setup_ip6config(service, index);
5389 service_register(service);
5391 __connman_notifier_service_add(service, service->name);
5396 int __connman_service_init(void)
5400 connection = connman_dbus_get_connection();
5402 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5405 service_list = g_sequence_new(service_free);
5410 void __connman_service_cleanup(void)
5416 list = service_list;
5417 service_list = NULL;
5418 g_sequence_free(list);
5420 g_hash_table_destroy(service_hash);
5421 service_hash = NULL;
5423 g_slist_free(counter_list);
5424 counter_list = NULL;
5426 dbus_connection_unref(connection);