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 connman_bool_t network_created;
83 struct connman_ipconfig *ipconfig_ipv4;
84 struct connman_ipconfig *ipconfig_ipv6;
85 struct connman_network *network;
86 struct connman_provider *provider;
88 char **nameservers_config;
89 char **nameservers_auto;
93 /* 802.1x settings from the config files */
98 char *client_cert_file;
99 char *private_key_file;
100 char *private_key_passphrase;
102 DBusMessage *pending;
104 struct connman_stats stats;
105 struct connman_stats stats_roaming;
106 GHashTable *counter_table;
107 enum connman_service_proxy_method proxy;
108 enum connman_service_proxy_method proxy_config;
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:
202 case CONNMAN_SERVICE_SECURITY_8021X:
204 case CONNMAN_SERVICE_SECURITY_WPA:
206 case CONNMAN_SERVICE_SECURITY_RSN:
213 static const char *state2string(enum connman_service_state state)
216 case CONNMAN_SERVICE_STATE_UNKNOWN:
218 case CONNMAN_SERVICE_STATE_IDLE:
220 case CONNMAN_SERVICE_STATE_ASSOCIATION:
221 return "association";
222 case CONNMAN_SERVICE_STATE_CONFIGURATION:
223 return "configuration";
224 case CONNMAN_SERVICE_STATE_READY:
226 case CONNMAN_SERVICE_STATE_ONLINE:
228 case CONNMAN_SERVICE_STATE_DISCONNECT:
230 case CONNMAN_SERVICE_STATE_FAILURE:
237 static const char *error2string(enum connman_service_error error)
240 case CONNMAN_SERVICE_ERROR_UNKNOWN:
242 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
243 return "out-of-range";
244 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
245 return "pin-missing";
246 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
247 return "dhcp-failed";
248 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
249 return "connect-failed";
250 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
251 return "login-failed";
252 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
253 return "auth-failed";
254 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
255 return "invalid-key";
261 static enum connman_service_error string2error(const char *error)
263 if (g_strcmp0(error, "dhcp-failed") == 0)
264 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
265 else if (g_strcmp0(error, "pin-missing") == 0)
266 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
267 else if (g_strcmp0(error, "invalid-key") == 0)
268 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
270 return CONNMAN_SERVICE_ERROR_UNKNOWN;
273 static const char *proxymethod2string(enum connman_service_proxy_method method)
276 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
278 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
280 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
282 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
289 static enum connman_service_proxy_method string2proxymethod(const char *method)
291 if (g_strcmp0(method, "direct") == 0)
292 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
293 else if (g_strcmp0(method, "auto") == 0)
294 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
295 else if (g_strcmp0(method, "manual") == 0)
296 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
298 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
301 static int service_load(struct connman_service *service)
304 GError *error = NULL;
307 connman_bool_t autoconnect;
308 unsigned int ssid_len;
311 DBG("service %p", service);
313 keyfile = connman_storage_load_service(service->identifier);
317 switch (service->type) {
318 case CONNMAN_SERVICE_TYPE_UNKNOWN:
319 case CONNMAN_SERVICE_TYPE_SYSTEM:
320 case CONNMAN_SERVICE_TYPE_ETHERNET:
321 case CONNMAN_SERVICE_TYPE_GPS:
322 case CONNMAN_SERVICE_TYPE_VPN:
323 case CONNMAN_SERVICE_TYPE_GADGET:
325 case CONNMAN_SERVICE_TYPE_WIFI:
326 if (service->name == NULL) {
329 name = g_key_file_get_string(keyfile,
330 service->identifier, "Name", NULL);
332 g_free(service->name);
333 service->name = name;
336 if (service->network != NULL)
337 connman_network_set_name(service->network,
341 if (service->network &&
342 connman_network_get_blob(service->network,
343 "WiFi.SSID", &ssid_len) == NULL) {
346 hex_ssid = g_key_file_get_string(keyfile,
350 if (hex_ssid != NULL) {
352 unsigned int i, j = 0, hex;
353 size_t hex_ssid_len = strlen(hex_ssid);
355 ssid = g_try_malloc0(hex_ssid_len / 2);
362 for (i = 0; i < hex_ssid_len; i += 2) {
363 sscanf(hex_ssid + i, "%02x", &hex);
367 connman_network_set_blob(service->network,
368 "WiFi.SSID", ssid, hex_ssid_len / 2);
375 case CONNMAN_SERVICE_TYPE_WIMAX:
376 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
377 case CONNMAN_SERVICE_TYPE_CELLULAR:
378 service->favorite = g_key_file_get_boolean(keyfile,
379 service->identifier, "Favorite", NULL);
381 autoconnect = g_key_file_get_boolean(keyfile,
382 service->identifier, "AutoConnect", &error);
384 service->autoconnect = autoconnect;
385 g_clear_error(&error);
387 str = g_key_file_get_string(keyfile,
388 service->identifier, "Failure", NULL);
390 if (service->favorite == FALSE)
391 service->state_ipv4 = service->state_ipv6 =
392 CONNMAN_SERVICE_STATE_FAILURE;
393 service->error = string2error(str);
399 str = g_key_file_get_string(keyfile,
400 service->identifier, "Modified", NULL);
402 g_time_val_from_iso8601(str, &service->modified);
406 str = g_key_file_get_string(keyfile,
407 service->identifier, "Passphrase", NULL);
409 g_free(service->passphrase);
410 service->passphrase = str;
413 if (service->ipconfig_ipv4 != NULL)
414 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
415 service->identifier, "IPv4.");
417 if (service->ipconfig_ipv6 != NULL)
418 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
419 service->identifier, "IPv6.");
421 service->nameservers_config = g_key_file_get_string_list(keyfile,
422 service->identifier, "Nameservers", &length, NULL);
423 if (service->nameservers_config != NULL && length == 0) {
424 g_strfreev(service->nameservers_config);
425 service->nameservers_config = NULL;
428 service->domains = g_key_file_get_string_list(keyfile,
429 service->identifier, "Domains", &length, NULL);
430 if (service->domains != NULL && length == 0) {
431 g_strfreev(service->domains);
432 service->domains = NULL;
435 str = g_key_file_get_string(keyfile,
436 service->identifier, "Proxy.Method", NULL);
438 service->proxy_config = string2proxymethod(str);
442 service->proxies = g_key_file_get_string_list(keyfile,
443 service->identifier, "Proxy.Servers", &length, NULL);
444 if (service->proxies != NULL && length == 0) {
445 g_strfreev(service->proxies);
446 service->proxies = NULL;
449 service->excludes = g_key_file_get_string_list(keyfile,
450 service->identifier, "Proxy.Excludes", &length, NULL);
451 if (service->excludes != NULL && length == 0) {
452 g_strfreev(service->excludes);
453 service->excludes = NULL;
456 str = g_key_file_get_string(keyfile,
457 service->identifier, "Proxy.URL", NULL);
459 g_free(service->pac);
464 g_key_file_free(keyfile);
469 static int service_save(struct connman_service *service)
474 const char *cst_str = NULL;
477 DBG("service %p", service);
479 keyfile = __connman_storage_open_service(service->identifier);
483 if (service->name != NULL)
484 g_key_file_set_string(keyfile, service->identifier,
485 "Name", service->name);
487 switch (service->type) {
488 case CONNMAN_SERVICE_TYPE_UNKNOWN:
489 case CONNMAN_SERVICE_TYPE_SYSTEM:
490 case CONNMAN_SERVICE_TYPE_ETHERNET:
491 case CONNMAN_SERVICE_TYPE_GPS:
492 case CONNMAN_SERVICE_TYPE_VPN:
493 case CONNMAN_SERVICE_TYPE_GADGET:
495 case CONNMAN_SERVICE_TYPE_WIFI:
496 if (service->network) {
497 const unsigned char *ssid;
498 unsigned int ssid_len = 0;
500 ssid = connman_network_get_blob(service->network,
501 "WiFi.SSID", &ssid_len);
503 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
504 char *identifier = service->identifier;
508 str = g_string_sized_new(ssid_len * 2);
514 for (i = 0; i < ssid_len; i++)
515 g_string_append_printf(str,
518 g_key_file_set_string(keyfile, identifier,
521 g_string_free(str, TRUE);
524 freq = connman_network_get_frequency(service->network);
525 g_key_file_set_integer(keyfile, service->identifier,
530 case CONNMAN_SERVICE_TYPE_WIMAX:
531 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
532 case CONNMAN_SERVICE_TYPE_CELLULAR:
533 g_key_file_set_boolean(keyfile, service->identifier,
534 "Favorite", service->favorite);
536 if (service->favorite == TRUE)
537 g_key_file_set_boolean(keyfile, service->identifier,
538 "AutoConnect", service->autoconnect);
540 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
541 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
542 const char *failure = error2string(service->error);
544 g_key_file_set_string(keyfile,
548 g_key_file_remove_key(keyfile, service->identifier,
554 str = g_time_val_to_iso8601(&service->modified);
556 g_key_file_set_string(keyfile, service->identifier,
561 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
562 g_key_file_set_string(keyfile, service->identifier,
563 "Passphrase", service->passphrase);
565 g_key_file_remove_key(keyfile, service->identifier,
568 if (service->ipconfig_ipv4 != NULL)
569 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
570 service->identifier, "IPv4.");
572 if (service->ipconfig_ipv6 != NULL)
573 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
574 service->identifier, "IPv6.");
576 if (service->nameservers_config != NULL) {
577 guint len = g_strv_length(service->nameservers_config);
579 g_key_file_set_string_list(keyfile, service->identifier,
581 (const gchar **) service->nameservers_config, len);
583 g_key_file_remove_key(keyfile, service->identifier,
584 "Nameservers", NULL);
586 if (service->domains != NULL) {
587 guint len = g_strv_length(service->domains);
589 g_key_file_set_string_list(keyfile, service->identifier,
591 (const gchar **) service->domains, len);
593 g_key_file_remove_key(keyfile, service->identifier,
596 cst_str = proxymethod2string(service->proxy_config);
598 g_key_file_set_string(keyfile, service->identifier,
599 "Proxy.Method", cst_str);
601 if (service->proxies != NULL) {
602 guint len = g_strv_length(service->proxies);
604 g_key_file_set_string_list(keyfile, service->identifier,
606 (const gchar **) service->proxies, len);
608 g_key_file_remove_key(keyfile, service->identifier,
609 "Proxy.Servers", NULL);
611 if (service->excludes != NULL) {
612 guint len = g_strv_length(service->excludes);
614 g_key_file_set_string_list(keyfile, service->identifier,
616 (const gchar **) service->excludes, len);
618 g_key_file_remove_key(keyfile, service->identifier,
619 "Proxy.Excludes", NULL);
621 if (service->pac != NULL && strlen(service->pac) > 0)
622 g_key_file_set_string(keyfile, service->identifier,
623 "Proxy.URL", service->pac);
625 g_key_file_remove_key(keyfile, service->identifier,
629 __connman_storage_save_service(keyfile, service->identifier);
631 g_key_file_free(keyfile);
636 static guint changed_timeout = 0;
638 static gboolean notify_services_changed(gpointer user_data)
642 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
643 CONNMAN_MANAGER_INTERFACE, "Services",
644 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
650 static void services_changed(gboolean delayed)
654 if (changed_timeout > 0) {
655 g_source_remove(changed_timeout);
659 if (__connman_connection_update_gateway() == TRUE) {
660 notify_services_changed(NULL);
664 if (delayed == FALSE) {
665 notify_services_changed(NULL);
669 changed_timeout = g_timeout_add_seconds(1, notify_services_changed,
673 static enum connman_service_state combine_state(
674 enum connman_service_state state_a,
675 enum connman_service_state state_b)
677 enum connman_service_state result;
679 if (state_a == state_b) {
684 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
689 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
694 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
699 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
704 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
709 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
714 if (state_a == CONNMAN_SERVICE_STATE_READY) {
719 if (state_b == CONNMAN_SERVICE_STATE_READY) {
724 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
729 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
734 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
739 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
744 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
749 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
754 result = CONNMAN_SERVICE_STATE_FAILURE;
760 static connman_bool_t is_connecting_state(struct connman_service *service,
761 enum connman_service_state state)
764 case CONNMAN_SERVICE_STATE_UNKNOWN:
765 case CONNMAN_SERVICE_STATE_IDLE:
766 case CONNMAN_SERVICE_STATE_FAILURE:
767 if (service->network != NULL)
768 return connman_network_get_connecting(service->network);
769 case CONNMAN_SERVICE_STATE_DISCONNECT:
770 case CONNMAN_SERVICE_STATE_READY:
771 case CONNMAN_SERVICE_STATE_ONLINE:
773 case CONNMAN_SERVICE_STATE_ASSOCIATION:
774 case CONNMAN_SERVICE_STATE_CONFIGURATION:
781 static connman_bool_t is_connected_state(const struct connman_service *service,
782 enum connman_service_state state)
785 case CONNMAN_SERVICE_STATE_UNKNOWN:
786 case CONNMAN_SERVICE_STATE_IDLE:
787 case CONNMAN_SERVICE_STATE_ASSOCIATION:
788 case CONNMAN_SERVICE_STATE_CONFIGURATION:
789 case CONNMAN_SERVICE_STATE_DISCONNECT:
790 case CONNMAN_SERVICE_STATE_FAILURE:
792 case CONNMAN_SERVICE_STATE_READY:
793 case CONNMAN_SERVICE_STATE_ONLINE:
800 static connman_bool_t is_connecting(struct connman_service *service)
802 return is_connecting_state(service, service->state);
805 static connman_bool_t is_connected(struct connman_service *service)
807 return is_connected_state(service, service->state);
810 static void update_nameservers(struct connman_service *service)
814 if (service->ipconfig_ipv4)
815 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
816 else if (service->ipconfig_ipv6)
817 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
824 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
825 case CONNMAN_SERVICE_STATE_UNKNOWN:
826 case CONNMAN_SERVICE_STATE_IDLE:
827 case CONNMAN_SERVICE_STATE_ASSOCIATION:
828 case CONNMAN_SERVICE_STATE_CONFIGURATION:
830 case CONNMAN_SERVICE_STATE_FAILURE:
831 case CONNMAN_SERVICE_STATE_DISCONNECT:
832 connman_resolver_remove_all(ifname);
834 case CONNMAN_SERVICE_STATE_READY:
835 case CONNMAN_SERVICE_STATE_ONLINE:
839 if (service->nameservers_config != NULL) {
842 for (i = 0; service->nameservers_config[i] != NULL; i++) {
843 connman_resolver_append(ifname, NULL,
844 service->nameservers_config[i]);
846 } else if (service->nameservers != NULL) {
849 for (i = 0; service->nameservers[i] != NULL; i++) {
850 connman_resolver_append(ifname, NULL,
851 service->nameservers[i]);
855 if (service->domains != NULL) {
858 for (i = 0; service->domains[i]; i++)
859 connman_resolver_append(ifname, service->domains[i],
861 } else if (service->domainname != NULL)
862 connman_resolver_append(ifname, service->domainname, NULL);
864 connman_resolver_flush();
868 * The is_auto variable is set to true when IPv6 autoconf nameservers are
869 * inserted to resolver via netlink message (see rtnl.c:rtnl_newnduseropt()
870 * for details) and not through service.c
872 int __connman_service_nameserver_append(struct connman_service *service,
873 const char *nameserver, gboolean is_auto)
878 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
880 if (nameserver == NULL)
884 nameservers = service->nameservers_auto;
886 nameservers = service->nameservers;
888 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
889 if (g_strcmp0(nameservers[i], nameserver) == 0)
892 if (nameservers != NULL) {
893 len = g_strv_length(nameservers);
894 nameservers = g_try_renew(char *, nameservers, len + 2);
897 nameservers = g_try_new0(char *, len + 2);
900 if (nameservers == NULL)
903 nameservers[len] = g_strdup(nameserver);
904 if (nameservers[len] == NULL)
907 nameservers[len + 1] = NULL;
909 if (is_auto == TRUE) {
910 service->nameservers_auto = nameservers;
912 service->nameservers = nameservers;
913 update_nameservers(service);
919 int __connman_service_nameserver_remove(struct connman_service *service,
920 const char *nameserver, gboolean is_auto)
922 char **servers, **nameservers;
923 gboolean found = FALSE;
926 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
928 if (nameserver == NULL)
932 nameservers = service->nameservers_auto;
934 nameservers = service->nameservers;
936 if (nameservers == NULL)
939 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
940 if (g_strcmp0(nameservers[i], nameserver) == 0) {
948 len = g_strv_length(nameservers);
951 g_strfreev(nameservers);
953 service->nameservers_auto = NULL;
955 service->nameservers = NULL;
960 servers = g_try_new0(char *, len);
964 for (i = 0, j = 0; i < len; i++) {
965 if (g_strcmp0(nameservers[i], nameserver) != 0) {
966 servers[j] = g_strdup(nameservers[i]);
967 if (servers[j] == NULL)
972 servers[len - 1] = NULL;
974 g_strfreev(nameservers);
975 nameservers = servers;
977 if (is_auto == TRUE) {
978 service->nameservers_auto = nameservers;
980 service->nameservers = nameservers;
981 update_nameservers(service);
987 void __connman_service_nameserver_clear(struct connman_service *service)
989 g_strfreev(service->nameservers);
990 service->nameservers = NULL;
992 update_nameservers(service);
995 static void nameserver_add_routes(int index, char **nameservers,
999 struct addrinfo hints;
1000 struct addrinfo *addr;
1002 for (i = 0; nameservers[i] != NULL; i++) {
1003 memset(&hints, 0, sizeof(struct addrinfo));
1004 hints.ai_flags = AI_NUMERICHOST;
1007 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1008 if (ret == EAI_NONAME)
1009 family = AF_INET; /* use the IPv4 as a default */
1013 family = addr->ai_family;
1015 if (family == AF_INET) {
1016 if (connman_inet_compare_subnet(index,
1017 nameservers[i]) != TRUE)
1018 connman_inet_add_host_route(index,
1019 nameservers[i], gw);
1020 } else if (family == AF_INET6)
1021 connman_inet_add_ipv6_host_route(index,
1022 nameservers[i], gw);
1028 static void nameserver_del_routes(int index, char **nameservers)
1031 struct addrinfo hints;
1032 struct addrinfo *addr;
1034 for (i = 0; nameservers[i] != NULL; i++) {
1035 memset(&hints, 0, sizeof(struct addrinfo));
1036 hints.ai_flags = AI_NUMERICHOST;
1039 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1040 if (ret == EAI_NONAME)
1041 family = AF_INET; /* use the IPv4 as a default */
1045 family = addr->ai_family;
1047 if (family == AF_INET)
1048 connman_inet_del_host_route(index, nameservers[i]);
1049 else if (family == AF_INET6)
1050 connman_inet_del_ipv6_host_route(index,
1057 void __connman_service_nameserver_add_routes(struct connman_service *service,
1062 if (service == NULL)
1065 if (service->network != NULL)
1066 index = connman_network_get_index(service->network);
1067 else if (service->provider != NULL)
1068 index = connman_provider_get_index(service->provider);
1070 if (service->nameservers_config != NULL) {
1072 * Configured nameserver takes preference over the
1073 * discoverd nameserver gathered from DHCP, VPN, etc.
1075 nameserver_add_routes(index, service->nameservers_config, gw);
1076 } else if (service->nameservers != NULL) {
1078 * We add nameservers host routes for nameservers that
1079 * are not on our subnet. For those who are, the subnet
1080 * route will be installed by the time the dns proxy code
1081 * tries to reach them. The subnet route is installed
1082 * when setting the interface IP address.
1084 nameserver_add_routes(index, service->nameservers, gw);
1088 void __connman_service_nameserver_del_routes(struct connman_service *service)
1092 if (service == NULL)
1095 if (service->network != NULL)
1096 index = connman_network_get_index(service->network);
1097 else if (service->provider != NULL)
1098 index = connman_provider_get_index(service->provider);
1100 if (service->nameservers_config != NULL)
1101 nameserver_del_routes(index, service->nameservers_config);
1102 else if (service->nameservers != NULL)
1103 nameserver_del_routes(index, service->nameservers);
1106 static struct connman_stats *stats_get(struct connman_service *service)
1108 if (service->roaming == TRUE)
1109 return &service->stats_roaming;
1111 return &service->stats;
1114 static connman_bool_t stats_enabled(struct connman_service *service)
1116 struct connman_stats *stats = stats_get(service);
1118 return stats->enabled;
1121 static void stats_start(struct connman_service *service)
1123 struct connman_stats *stats = stats_get(service);
1125 DBG("service %p", service);
1127 if (stats->timer == NULL)
1130 stats->enabled = TRUE;
1131 stats->data_last.time = stats->data.time;
1133 g_timer_start(stats->timer);
1136 static void stats_stop(struct connman_service *service)
1138 struct connman_stats *stats = stats_get(service);
1139 unsigned int seconds;
1141 DBG("service %p", service);
1143 if (stats->timer == NULL)
1146 if (stats->enabled == FALSE)
1149 g_timer_stop(stats->timer);
1151 seconds = g_timer_elapsed(stats->timer, NULL);
1152 stats->data.time = stats->data_last.time + seconds;
1154 stats->enabled = FALSE;
1157 static void reset_stats(struct connman_service *service)
1159 DBG("service %p", service);
1162 service->stats.valid = FALSE;
1164 service->stats.data.rx_packets = 0;
1165 service->stats.data.tx_packets = 0;
1166 service->stats.data.rx_bytes = 0;
1167 service->stats.data.tx_bytes = 0;
1168 service->stats.data.rx_errors = 0;
1169 service->stats.data.tx_errors = 0;
1170 service->stats.data.rx_dropped = 0;
1171 service->stats.data.tx_dropped = 0;
1172 service->stats.data.time = 0;
1173 service->stats.data_last.time = 0;
1175 g_timer_reset(service->stats.timer);
1178 service->stats_roaming.valid = FALSE;
1180 service->stats_roaming.data.rx_packets = 0;
1181 service->stats_roaming.data.tx_packets = 0;
1182 service->stats_roaming.data.rx_bytes = 0;
1183 service->stats_roaming.data.tx_bytes = 0;
1184 service->stats_roaming.data.rx_errors = 0;
1185 service->stats_roaming.data.tx_errors = 0;
1186 service->stats_roaming.data.rx_dropped = 0;
1187 service->stats_roaming.data.tx_dropped = 0;
1188 service->stats_roaming.data.time = 0;
1189 service->stats_roaming.data_last.time = 0;
1191 g_timer_reset(service->stats_roaming.timer);
1194 static struct connman_service *get_default(void)
1196 struct connman_service *service;
1197 GSequenceIter *iter;
1199 iter = g_sequence_get_begin_iter(service_list);
1201 if (g_sequence_iter_is_end(iter) == TRUE)
1204 service = g_sequence_get(iter);
1206 if (is_connected(service) == FALSE)
1212 static void default_changed(void)
1214 struct connman_service *service = get_default();
1216 __connman_notifier_default_changed(service);
1219 const char *__connman_service_default(void)
1221 struct connman_service *service;
1223 service = get_default();
1224 if (service == NULL)
1227 return __connman_service_type2string(service->type);
1230 static void state_changed(struct connman_service *service)
1234 __connman_notifier_service_state_changed(service, service->state);
1236 str = state2string(service->state);
1240 connman_dbus_property_changed_basic(service->path,
1241 CONNMAN_SERVICE_INTERFACE, "State",
1242 DBUS_TYPE_STRING, &str);
1245 static void strength_changed(struct connman_service *service)
1247 if (service->strength == 0)
1250 connman_dbus_property_changed_basic(service->path,
1251 CONNMAN_SERVICE_INTERFACE, "Strength",
1252 DBUS_TYPE_BYTE, &service->strength);
1255 static void favorite_changed(struct connman_service *service)
1257 if (service->path == NULL)
1260 connman_dbus_property_changed_basic(service->path,
1261 CONNMAN_SERVICE_INTERFACE, "Favorite",
1262 DBUS_TYPE_BOOLEAN, &service->favorite);
1265 static void immutable_changed(struct connman_service *service)
1267 if (service->path == NULL)
1270 connman_dbus_property_changed_basic(service->path,
1271 CONNMAN_SERVICE_INTERFACE, "Immutable",
1272 DBUS_TYPE_BOOLEAN, &service->immutable);
1275 static void roaming_changed(struct connman_service *service)
1277 if (service->path == NULL)
1280 connman_dbus_property_changed_basic(service->path,
1281 CONNMAN_SERVICE_INTERFACE, "Roaming",
1282 DBUS_TYPE_BOOLEAN, &service->roaming);
1285 static void autoconnect_changed(struct connman_service *service)
1287 if (service->path == NULL)
1290 connman_dbus_property_changed_basic(service->path,
1291 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
1292 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1295 static void append_security(DBusMessageIter *iter, void *user_data)
1297 struct connman_service *service = user_data;
1300 str = security2string(service->security);
1302 dbus_message_iter_append_basic(iter,
1303 DBUS_TYPE_STRING, &str);
1306 * Some access points incorrectly advertise WPS even when they
1307 * are configured as open or no security, so filter
1310 if (service->wps == TRUE) {
1311 switch (service->security) {
1312 case CONNMAN_SERVICE_SECURITY_PSK:
1313 case CONNMAN_SERVICE_SECURITY_WPA:
1314 case CONNMAN_SERVICE_SECURITY_RSN:
1316 dbus_message_iter_append_basic(iter,
1317 DBUS_TYPE_STRING, &str);
1319 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1320 case CONNMAN_SERVICE_SECURITY_NONE:
1321 case CONNMAN_SERVICE_SECURITY_WEP:
1322 case CONNMAN_SERVICE_SECURITY_8021X:
1328 static void append_ethernet(DBusMessageIter *iter, void *user_data)
1330 struct connman_service *service = user_data;
1332 if (service->ipconfig_ipv4 != NULL)
1333 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
1335 else if (service->ipconfig_ipv6 != NULL)
1336 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
1340 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1342 struct connman_service *service = user_data;
1344 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1345 state2string(service->state_ipv4));
1347 if (is_connected_state(service, service->state_ipv4) == FALSE)
1350 if (service->ipconfig_ipv4 != NULL)
1351 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1354 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1356 struct connman_service *service = user_data;
1358 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1359 state2string(service->state_ipv6));
1361 if (is_connected_state(service, service->state_ipv6) == FALSE)
1364 if (service->ipconfig_ipv6 != NULL)
1365 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1366 service->ipconfig_ipv4);
1369 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1371 struct connman_service *service = user_data;
1373 if (service->ipconfig_ipv4 != NULL)
1374 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1378 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1380 struct connman_service *service = user_data;
1382 if (service->ipconfig_ipv6 != NULL)
1383 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1387 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1392 servers = *nameservers;
1394 for (i = 0; servers[i] != NULL; i++) {
1395 dbus_message_iter_append_basic(iter,
1396 DBUS_TYPE_STRING, &servers[i]);
1400 static void append_dns(DBusMessageIter *iter, void *user_data)
1402 struct connman_service *service = user_data;
1404 if (is_connected(service) == FALSE)
1407 if (service->nameservers_config != NULL) {
1408 append_nameserver(iter, &service->nameservers_config);
1411 if (service->nameservers != NULL)
1412 append_nameserver(iter, &service->nameservers);
1414 if (service->nameservers_auto != NULL)
1415 append_nameserver(iter, &service->nameservers_auto);
1419 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1421 struct connman_service *service = user_data;
1424 if (service->nameservers_config == NULL)
1427 for (i = 0; service->nameservers_config[i]; i++) {
1428 dbus_message_iter_append_basic(iter,
1430 &service->nameservers_config[i]);
1434 static void append_domain(DBusMessageIter *iter, void *user_data)
1436 struct connman_service *service = user_data;
1438 if (is_connected(service) == FALSE &&
1439 is_connecting(service) == FALSE)
1442 if (service->domainname == NULL)
1445 dbus_message_iter_append_basic(iter,
1446 DBUS_TYPE_STRING, &service->domainname);
1449 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1451 struct connman_service *service = user_data;
1454 if (service->domains == NULL)
1457 for (i = 0; service->domains[i]; i++)
1458 dbus_message_iter_append_basic(iter,
1459 DBUS_TYPE_STRING, &service->domains[i]);
1462 static void append_proxies(DBusMessageIter *iter, void *user_data)
1464 struct connman_service *service = user_data;
1467 if (service->proxies == NULL)
1470 for (i = 0; service->proxies[i]; i++)
1471 dbus_message_iter_append_basic(iter,
1472 DBUS_TYPE_STRING, &service->proxies[i]);
1475 static void append_excludes(DBusMessageIter *iter, void *user_data)
1477 struct connman_service *service = user_data;
1480 if (service->excludes == NULL)
1483 for (i = 0; service->excludes[i]; i++)
1484 dbus_message_iter_append_basic(iter,
1485 DBUS_TYPE_STRING, &service->excludes[i]);
1488 static void append_proxy(DBusMessageIter *iter, void *user_data)
1490 struct connman_service *service = user_data;
1491 enum connman_service_proxy_method proxy;
1492 const char *pac = NULL;
1493 const char *method = proxymethod2string(
1494 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1498 if (is_connected(service) == FALSE)
1501 proxy = connman_service_get_proxy_method(service);
1504 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1506 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1508 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1509 connman_dbus_dict_append_array(iter, "Servers",
1510 DBUS_TYPE_STRING, append_proxies,
1513 connman_dbus_dict_append_array(iter, "Excludes",
1514 DBUS_TYPE_STRING, append_excludes,
1517 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1518 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1519 if (service->ipconfig_ipv4 != NULL)
1520 pac = __connman_ipconfig_get_proxy_autoconfig(
1521 service->ipconfig_ipv4);
1522 else if (service->ipconfig_ipv6 != NULL)
1523 pac = __connman_ipconfig_get_proxy_autoconfig(
1524 service->ipconfig_ipv6);
1526 if (service->pac == NULL && pac == NULL)
1529 if (service->pac != NULL)
1532 connman_dbus_dict_append_basic(iter, "URL",
1533 DBUS_TYPE_STRING, &pac);
1537 method = proxymethod2string(proxy);
1540 connman_dbus_dict_append_basic(iter, "Method",
1541 DBUS_TYPE_STRING, &method);
1544 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1546 struct connman_service *service = user_data;
1549 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1552 switch (service->proxy_config) {
1553 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1555 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1557 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1558 if (service->proxies != NULL)
1559 connman_dbus_dict_append_array(iter, "Servers",
1561 append_proxies, service);
1563 if (service->excludes != NULL)
1564 connman_dbus_dict_append_array(iter, "Excludes",
1566 append_excludes, service);
1568 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1569 if (service->pac != NULL)
1570 connman_dbus_dict_append_basic(iter, "URL",
1571 DBUS_TYPE_STRING, &service->pac);
1575 method = proxymethod2string(service->proxy_config);
1577 connman_dbus_dict_append_basic(iter, "Method",
1578 DBUS_TYPE_STRING, &method);
1581 static void append_provider(DBusMessageIter *iter, void *user_data)
1583 struct connman_service *service = user_data;
1585 DBG("%p %p", service, service->provider);
1587 if (is_connected(service) == FALSE)
1590 if (service->provider != NULL)
1591 __connman_provider_append_properties(service->provider, iter);
1595 static void settings_changed(struct connman_service *service,
1596 struct connman_ipconfig *ipconfig)
1598 connman_dbus_property_changed_dict(service->path,
1599 CONNMAN_SERVICE_INTERFACE, "IPv4",
1600 append_ipv4, service);
1602 connman_dbus_property_changed_dict(service->path,
1603 CONNMAN_SERVICE_INTERFACE, "IPv6",
1604 append_ipv6, service);
1606 __connman_notifier_ipconfig_changed(service, ipconfig);
1609 static void ipv4_configuration_changed(struct connman_service *service)
1611 connman_dbus_property_changed_dict(service->path,
1612 CONNMAN_SERVICE_INTERFACE,
1613 "IPv4.Configuration",
1618 static void ipv6_configuration_changed(struct connman_service *service)
1620 connman_dbus_property_changed_dict(service->path,
1621 CONNMAN_SERVICE_INTERFACE,
1622 "IPv6.Configuration",
1627 static void dns_changed(struct connman_service *service)
1629 connman_dbus_property_changed_array(service->path,
1630 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1631 DBUS_TYPE_STRING, append_dns, service);
1634 static void dns_configuration_changed(struct connman_service *service)
1636 connman_dbus_property_changed_array(service->path,
1637 CONNMAN_SERVICE_INTERFACE,
1638 "Nameservers.Configuration",
1639 DBUS_TYPE_STRING, append_dnsconfig, service);
1641 dns_changed(service);
1644 static void domain_changed(struct connman_service *service)
1646 connman_dbus_property_changed_array(service->path,
1647 CONNMAN_SERVICE_INTERFACE, "Domains",
1648 DBUS_TYPE_STRING, append_domain, service);
1651 static void domain_configuration_changed(struct connman_service *service)
1653 connman_dbus_property_changed_array(service->path,
1654 CONNMAN_SERVICE_INTERFACE,
1655 "Domains.Configuration",
1656 DBUS_TYPE_STRING, append_domainconfig, service);
1659 static void proxy_changed(struct connman_service *service)
1661 connman_dbus_property_changed_dict(service->path,
1662 CONNMAN_SERVICE_INTERFACE, "Proxy",
1663 append_proxy, service);
1666 static void proxy_configuration_changed(struct connman_service *service)
1668 connman_dbus_property_changed_dict(service->path,
1669 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1670 append_proxyconfig, service);
1672 proxy_changed(service);
1675 static void link_changed(struct connman_service *service)
1677 connman_dbus_property_changed_dict(service->path,
1678 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1679 append_ethernet, service);
1682 static void stats_append_counters(DBusMessageIter *dict,
1683 struct connman_stats_data *stats,
1684 struct connman_stats_data *counters,
1685 connman_bool_t append_all)
1687 if (counters->rx_packets != stats->rx_packets || append_all) {
1688 counters->rx_packets = stats->rx_packets;
1689 connman_dbus_dict_append_basic(dict, "RX.Packets",
1690 DBUS_TYPE_UINT32, &stats->rx_packets);
1693 if (counters->tx_packets != stats->tx_packets || append_all) {
1694 counters->tx_packets = stats->tx_packets;
1695 connman_dbus_dict_append_basic(dict, "TX.Packets",
1696 DBUS_TYPE_UINT32, &stats->tx_packets);
1699 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1700 counters->rx_bytes = stats->rx_bytes;
1701 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1702 DBUS_TYPE_UINT32, &stats->rx_bytes);
1705 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1706 counters->tx_bytes = stats->tx_bytes;
1707 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1708 DBUS_TYPE_UINT32, &stats->tx_bytes);
1711 if (counters->rx_errors != stats->rx_errors || append_all) {
1712 counters->rx_errors = stats->rx_errors;
1713 connman_dbus_dict_append_basic(dict, "RX.Errors",
1714 DBUS_TYPE_UINT32, &stats->rx_errors);
1717 if (counters->tx_errors != stats->tx_errors || append_all) {
1718 counters->tx_errors = stats->tx_errors;
1719 connman_dbus_dict_append_basic(dict, "TX.Errors",
1720 DBUS_TYPE_UINT32, &stats->tx_errors);
1723 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1724 counters->rx_dropped = stats->rx_dropped;
1725 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1726 DBUS_TYPE_UINT32, &stats->rx_dropped);
1729 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1730 counters->tx_dropped = stats->tx_dropped;
1731 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1732 DBUS_TYPE_UINT32, &stats->tx_dropped);
1735 if (counters->time != stats->time || append_all) {
1736 counters->time = stats->time;
1737 connman_dbus_dict_append_basic(dict, "Time",
1738 DBUS_TYPE_UINT32, &stats->time);
1742 static void stats_append(struct connman_service *service,
1743 const char *counter,
1744 struct connman_stats_counter *counters,
1745 connman_bool_t append_all)
1747 DBusMessageIter array, dict;
1750 DBG("service %p counter %s", service, counter);
1752 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1756 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1757 &service->path, DBUS_TYPE_INVALID);
1759 dbus_message_iter_init_append(msg, &array);
1762 connman_dbus_dict_open(&array, &dict);
1764 stats_append_counters(&dict, &service->stats.data,
1765 &counters->stats.data, append_all);
1767 connman_dbus_dict_close(&array, &dict);
1769 /* roaming counter */
1770 connman_dbus_dict_open(&array, &dict);
1772 stats_append_counters(&dict, &service->stats_roaming.data,
1773 &counters->stats_roaming.data, append_all);
1775 connman_dbus_dict_close(&array, &dict);
1777 __connman_counter_send_usage(counter, msg);
1780 static void stats_update(struct connman_service *service,
1781 unsigned int rx_packets, unsigned int tx_packets,
1782 unsigned int rx_bytes, unsigned int tx_bytes,
1783 unsigned int rx_errors, unsigned int tx_errors,
1784 unsigned int rx_dropped, unsigned int tx_dropped)
1786 struct connman_stats *stats = stats_get(service);
1787 struct connman_stats_data *data_last = &stats->data_last;
1788 struct connman_stats_data *data = &stats->data;
1789 unsigned int seconds;
1791 DBG("service %p", service);
1793 if (stats->valid == TRUE) {
1795 rx_packets - data_last->rx_packets;
1797 tx_packets - data_last->tx_packets;
1799 rx_bytes - data_last->rx_bytes;
1801 tx_bytes - data_last->tx_bytes;
1803 rx_errors - data_last->rx_errors;
1805 tx_errors - data_last->tx_errors;
1807 rx_dropped - data_last->rx_dropped;
1809 tx_dropped - data_last->tx_dropped;
1811 stats->valid = TRUE;
1814 data_last->rx_packets = rx_packets;
1815 data_last->tx_packets = tx_packets;
1816 data_last->rx_bytes = rx_bytes;
1817 data_last->tx_bytes = tx_bytes;
1818 data_last->rx_errors = rx_errors;
1819 data_last->tx_errors = tx_errors;
1820 data_last->rx_dropped = rx_dropped;
1821 data_last->tx_dropped = tx_dropped;
1823 seconds = g_timer_elapsed(stats->timer, NULL);
1824 stats->data.time = stats->data_last.time + seconds;
1827 void __connman_service_notify(struct connman_service *service,
1828 unsigned int rx_packets, unsigned int tx_packets,
1829 unsigned int rx_bytes, unsigned int tx_bytes,
1830 unsigned int rx_errors, unsigned int tx_errors,
1831 unsigned int rx_dropped, unsigned int tx_dropped)
1833 GHashTableIter iter;
1834 gpointer key, value;
1835 const char *counter;
1836 struct connman_stats_counter *counters;
1837 struct connman_stats_data *data;
1840 if (service == NULL)
1843 if (is_connected(service) == FALSE)
1846 stats_update(service,
1847 rx_packets, tx_packets,
1849 rx_errors, tx_errors,
1850 rx_dropped, tx_dropped);
1852 data = &stats_get(service)->data;
1853 err = __connman_stats_update(service, service->roaming, data);
1855 connman_error("Failed to store statistics for %s",
1856 service->identifier);
1858 g_hash_table_iter_init(&iter, service->counter_table);
1859 while (g_hash_table_iter_next(&iter, &key, &value)) {
1863 stats_append(service, counter, counters, counters->append_all);
1864 counters->append_all = FALSE;
1868 int __connman_service_counter_register(const char *counter)
1870 struct connman_service *service;
1871 GSequenceIter *iter;
1872 struct connman_stats_counter *counters;
1874 DBG("counter %s", counter);
1876 counter_list = g_slist_append(counter_list, (gpointer)counter);
1878 iter = g_sequence_get_begin_iter(service_list);
1880 while (g_sequence_iter_is_end(iter) == FALSE) {
1881 service = g_sequence_get(iter);
1883 counters = g_try_new0(struct connman_stats_counter, 1);
1884 if (counters == NULL)
1887 counters->append_all = TRUE;
1889 g_hash_table_replace(service->counter_table, (gpointer)counter,
1892 iter = g_sequence_iter_next(iter);
1898 void __connman_service_counter_unregister(const char *counter)
1900 struct connman_service *service;
1901 GSequenceIter *iter;
1903 DBG("counter %s", counter);
1905 iter = g_sequence_get_begin_iter(service_list);
1907 while (g_sequence_iter_is_end(iter) == FALSE) {
1908 service = g_sequence_get(iter);
1910 g_hash_table_remove(service->counter_table, counter);
1912 iter = g_sequence_iter_next(iter);
1915 counter_list = g_slist_remove(counter_list, counter);
1918 GSequence *__connman_service_get_list(struct connman_session *session,
1919 service_match_cb service_match,
1920 create_service_entry_cb create_service_entry,
1921 GDestroyNotify destroy_service_entry)
1924 GSequenceIter *iter;
1925 struct connman_service *service;
1926 struct service_entry *entry;
1928 list = g_sequence_new(destroy_service_entry);
1932 iter = g_sequence_get_begin_iter(service_list);
1934 while (g_sequence_iter_is_end(iter) == FALSE) {
1935 service = g_sequence_get(iter);
1937 if (service_match(session, service) == TRUE) {
1938 entry = create_service_entry(service, service->name,
1943 g_sequence_append(list, entry);
1946 iter = g_sequence_iter_next(iter);
1952 void __connman_service_session_inc(struct connman_service *service)
1954 DBG("service %p ref count %d", service,
1955 service->session_usage_count + 1);
1957 __sync_fetch_and_add(&service->session_usage_count, 1);
1960 connman_bool_t __connman_service_session_dec(struct connman_service *service)
1962 DBG("service %p ref count %d", service,
1963 service->session_usage_count - 1);
1965 if (__sync_fetch_and_sub(&service->session_usage_count, 1) != 1)
1971 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1972 struct connman_service *service)
1976 str = __connman_service_type2string(service->type);
1978 connman_dbus_dict_append_basic(dict, "Type",
1979 DBUS_TYPE_STRING, &str);
1981 connman_dbus_dict_append_array(dict, "Security",
1982 DBUS_TYPE_STRING, append_security, service);
1984 str = state2string(service->state);
1986 connman_dbus_dict_append_basic(dict, "State",
1987 DBUS_TYPE_STRING, &str);
1989 str = error2string(service->error);
1991 connman_dbus_dict_append_basic(dict, "Error",
1992 DBUS_TYPE_STRING, &str);
1994 if (service->strength > 0)
1995 connman_dbus_dict_append_basic(dict, "Strength",
1996 DBUS_TYPE_BYTE, &service->strength);
1998 connman_dbus_dict_append_basic(dict, "Favorite",
1999 DBUS_TYPE_BOOLEAN, &service->favorite);
2001 connman_dbus_dict_append_basic(dict, "Immutable",
2002 DBUS_TYPE_BOOLEAN, &service->immutable);
2004 if (service->favorite == TRUE)
2005 connman_dbus_dict_append_basic(dict, "AutoConnect",
2006 DBUS_TYPE_BOOLEAN, &service->autoconnect);
2008 connman_dbus_dict_append_basic(dict, "AutoConnect",
2009 DBUS_TYPE_BOOLEAN, &service->favorite);
2011 if (service->name != NULL)
2012 connman_dbus_dict_append_basic(dict, "Name",
2013 DBUS_TYPE_STRING, &service->name);
2015 switch (service->type) {
2016 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2017 case CONNMAN_SERVICE_TYPE_SYSTEM:
2018 case CONNMAN_SERVICE_TYPE_GPS:
2019 case CONNMAN_SERVICE_TYPE_VPN:
2020 case CONNMAN_SERVICE_TYPE_GADGET:
2022 case CONNMAN_SERVICE_TYPE_CELLULAR:
2023 connman_dbus_dict_append_basic(dict, "Roaming",
2024 DBUS_TYPE_BOOLEAN, &service->roaming);
2026 connman_dbus_dict_append_dict(dict, "Ethernet",
2027 append_ethernet, service);
2029 case CONNMAN_SERVICE_TYPE_WIFI:
2030 if (service->passphrase != NULL && limited == FALSE)
2031 connman_dbus_dict_append_basic(dict, "Passphrase",
2032 DBUS_TYPE_STRING, &service->passphrase);
2035 case CONNMAN_SERVICE_TYPE_ETHERNET:
2036 case CONNMAN_SERVICE_TYPE_WIMAX:
2037 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2038 connman_dbus_dict_append_dict(dict, "Ethernet",
2039 append_ethernet, service);
2043 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
2045 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
2046 append_ipv4config, service);
2048 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
2050 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
2051 append_ipv6config, service);
2053 connman_dbus_dict_append_array(dict, "Nameservers",
2054 DBUS_TYPE_STRING, append_dns, service);
2056 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
2057 DBUS_TYPE_STRING, append_dnsconfig, service);
2059 connman_dbus_dict_append_array(dict, "Domains",
2060 DBUS_TYPE_STRING, append_domain, service);
2062 connman_dbus_dict_append_array(dict, "Domains.Configuration",
2063 DBUS_TYPE_STRING, append_domainconfig, service);
2065 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
2067 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
2068 append_proxyconfig, service);
2070 connman_dbus_dict_append_dict(dict, "Provider",
2071 append_provider, service);
2074 static void append_struct(gpointer value, gpointer user_data)
2076 struct connman_service *service = value;
2077 DBusMessageIter *iter = user_data;
2078 DBusMessageIter entry, dict;
2080 if (service->path == NULL)
2083 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
2085 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2088 connman_dbus_dict_open(&entry, &dict);
2089 append_properties(&dict, TRUE, service);
2090 connman_dbus_dict_close(&entry, &dict);
2092 dbus_message_iter_close_container(iter, &entry);
2095 void __connman_service_list_struct(DBusMessageIter *iter)
2097 g_sequence_foreach(service_list, append_struct, iter);
2100 connman_bool_t __connman_service_is_hidden(struct connman_service *service)
2102 return service->hidden;
2105 int __connman_service_get_index(struct connman_service *service)
2107 if (service == NULL)
2110 if (service->network != NULL)
2111 return connman_network_get_index(service->network);
2112 else if (service->provider != NULL)
2113 return connman_provider_get_index(service->provider);
2118 void __connman_service_set_domainname(struct connman_service *service,
2119 const char *domainname)
2121 if (service == NULL || service->hidden == TRUE)
2124 g_free(service->domainname);
2125 service->domainname = g_strdup(domainname);
2127 domain_changed(service);
2130 const char *connman_service_get_domainname(struct connman_service *service)
2132 if (service == NULL)
2135 if (service->domains != NULL)
2136 return service->domains[0];
2138 return service->domainname;
2141 char **connman_service_get_nameservers(struct connman_service *service)
2143 if (service == NULL)
2146 if (service->nameservers_config != NULL)
2147 return service->nameservers_config;
2148 else if (service->nameservers != NULL)
2149 return service->nameservers;
2154 void connman_service_set_proxy_method(struct connman_service *service,
2155 enum connman_service_proxy_method method)
2157 if (service == NULL || service->hidden == TRUE)
2160 service->proxy = method;
2162 proxy_changed(service);
2164 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
2165 __connman_notifier_proxy_changed(service);
2168 enum connman_service_proxy_method connman_service_get_proxy_method(
2169 struct connman_service *service)
2171 if (service == NULL)
2172 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2174 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
2175 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
2176 service->pac == NULL)
2177 return service->proxy;
2179 return service->proxy_config;
2182 return service->proxy;
2185 char **connman_service_get_proxy_servers(struct connman_service *service)
2187 return g_strdupv(service->proxies);
2190 char **connman_service_get_proxy_excludes(struct connman_service *service)
2192 return g_strdupv(service->excludes);
2195 const char *connman_service_get_proxy_url(struct connman_service *service)
2197 if (service == NULL)
2200 return service->pac;
2203 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
2206 if (service == NULL || service->hidden == TRUE)
2209 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
2211 if (service->ipconfig_ipv4) {
2212 if (__connman_ipconfig_set_proxy_autoconfig(
2213 service->ipconfig_ipv4, url) < 0)
2215 } else if (service->ipconfig_ipv6) {
2216 if (__connman_ipconfig_set_proxy_autoconfig(
2217 service->ipconfig_ipv6, url) < 0)
2222 proxy_changed(service);
2224 __connman_notifier_proxy_changed(service);
2227 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
2229 if (service == NULL)
2232 if (service->ipconfig_ipv4)
2233 return __connman_ipconfig_get_proxy_autoconfig(
2234 service->ipconfig_ipv4);
2235 else if (service->ipconfig_ipv6)
2236 return __connman_ipconfig_get_proxy_autoconfig(
2237 service->ipconfig_ipv6);
2241 static void update_timeservers(struct connman_service *service)
2245 if (service->timeservers == NULL)
2248 switch (service->state) {
2249 case CONNMAN_SERVICE_STATE_UNKNOWN:
2250 case CONNMAN_SERVICE_STATE_IDLE:
2251 case CONNMAN_SERVICE_STATE_ASSOCIATION:
2252 case CONNMAN_SERVICE_STATE_CONFIGURATION:
2254 case CONNMAN_SERVICE_STATE_FAILURE:
2255 case CONNMAN_SERVICE_STATE_DISCONNECT:
2256 for (i = 0; service->timeservers[i] != NULL; i++)
2257 connman_timeserver_remove(service->timeservers[i]);
2259 case CONNMAN_SERVICE_STATE_READY:
2260 case CONNMAN_SERVICE_STATE_ONLINE:
2264 for (i = 0; service->timeservers[i] != NULL; i++)
2265 connman_timeserver_append(service->timeservers[i]);
2268 int __connman_service_timeserver_append(struct connman_service *service,
2269 const char *timeserver)
2273 DBG("service %p timeserver %s", service, timeserver);
2275 if (timeserver == NULL)
2278 if (service->timeservers != NULL) {
2281 for (i = 0; service->timeservers[i] != NULL; i++)
2282 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2285 len = g_strv_length(service->timeservers);
2286 service->timeservers = g_try_renew(char *, service->timeservers,
2290 service->timeservers = g_try_new0(char *, len + 2);
2293 if (service->timeservers == NULL)
2296 service->timeservers[len] = g_strdup(timeserver);
2297 service->timeservers[len + 1] = NULL;
2299 update_timeservers(service);
2304 int __connman_service_timeserver_remove(struct connman_service *service,
2305 const char *timeserver)
2308 int len, i, j, found = 0;
2310 DBG("service %p timeserver %s", service, timeserver);
2312 if (timeserver == NULL)
2315 if (service->timeservers == NULL)
2318 for (i = 0; service->timeservers != NULL &&
2319 service->timeservers[i] != NULL; i++)
2320 if (g_strcmp0(service->timeservers[i], timeserver) == 0) {
2328 len = g_strv_length(service->timeservers);
2331 g_strfreev(service->timeservers);
2332 service->timeservers = NULL;
2337 servers = g_try_new0(char *, len);
2338 if (servers == NULL)
2341 for (i = 0, j = 0; i < len; i++) {
2342 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2343 servers[j] = g_strdup(service->timeservers[i]);
2344 if (servers[j] == NULL)
2349 servers[len - 1] = NULL;
2351 g_strfreev(service->timeservers);
2352 service->timeservers = servers;
2354 update_timeservers(service);
2359 void __connman_service_set_pac(struct connman_service *service,
2362 if (service->hidden == TRUE)
2364 g_free(service->pac);
2365 service->pac = g_strdup(pac);
2367 proxy_changed(service);
2370 void __connman_service_set_identity(struct connman_service *service,
2371 const char *identity)
2373 if (service->immutable || service->hidden == TRUE)
2376 g_free(service->identity);
2377 service->identity = g_strdup(identity);
2379 if (service->network != NULL)
2380 connman_network_set_string(service->network,
2385 void __connman_service_set_agent_identity(struct connman_service *service,
2386 const char *agent_identity)
2388 if (service->hidden == TRUE)
2390 g_free(service->agent_identity);
2391 service->agent_identity = g_strdup(agent_identity);
2393 if (service->network != NULL)
2394 connman_network_set_string(service->network,
2395 "WiFi.AgentIdentity",
2396 service->agent_identity);
2399 void __connman_service_set_passphrase(struct connman_service *service,
2400 const char* passphrase)
2402 if (service->immutable == TRUE || service->hidden == TRUE)
2405 g_free(service->passphrase);
2406 service->passphrase = g_strdup(passphrase);
2408 if (service->network != NULL)
2409 connman_network_set_string(service->network,
2411 service->passphrase);
2413 service_save(service);
2416 void __connman_service_set_agent_passphrase(struct connman_service *service,
2417 const char *agent_passphrase)
2419 if (service->hidden == TRUE)
2421 g_free(service->agent_passphrase);
2422 service->agent_passphrase = g_strdup(agent_passphrase);
2424 if (service->network != NULL)
2425 connman_network_set_string(service->network,
2426 "WiFi.AgentPassphrase",
2427 service->agent_passphrase);
2430 static DBusMessage *get_properties(DBusConnection *conn,
2431 DBusMessage *msg, void *user_data)
2433 struct connman_service *service = user_data;
2435 DBusMessageIter array, dict;
2437 DBG("service %p", service);
2439 reply = dbus_message_new_method_return(msg);
2443 dbus_message_iter_init_append(reply, &array);
2445 connman_dbus_dict_open(&array, &dict);
2446 append_properties(&dict, FALSE, service);
2447 connman_dbus_dict_close(&array, &dict);
2452 static int update_proxy_configuration(struct connman_service *service,
2453 DBusMessageIter *array)
2455 DBusMessageIter dict;
2456 enum connman_service_proxy_method method;
2457 GString *servers_str = NULL;
2458 GString *excludes_str = NULL;
2459 const char *url = NULL;
2461 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2463 dbus_message_iter_recurse(array, &dict);
2465 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2466 DBusMessageIter entry, variant;
2470 dbus_message_iter_recurse(&dict, &entry);
2472 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2475 dbus_message_iter_get_basic(&entry, &key);
2476 dbus_message_iter_next(&entry);
2478 if (dbus_message_iter_get_arg_type(&entry) !=
2482 dbus_message_iter_recurse(&entry, &variant);
2483 type = dbus_message_iter_get_arg_type(&variant);
2485 if (g_str_equal(key, "Method") == TRUE) {
2488 if (type != DBUS_TYPE_STRING)
2491 dbus_message_iter_get_basic(&variant, &val);
2492 method = string2proxymethod(val);
2493 } else if (g_str_equal(key, "URL") == TRUE) {
2494 if (type != DBUS_TYPE_STRING)
2497 dbus_message_iter_get_basic(&variant, &url);
2498 } else if (g_str_equal(key, "Servers") == TRUE) {
2499 DBusMessageIter str_array;
2501 if (type != DBUS_TYPE_ARRAY)
2504 servers_str = g_string_new(NULL);
2505 if (servers_str == NULL)
2508 dbus_message_iter_recurse(&variant, &str_array);
2510 while (dbus_message_iter_get_arg_type(&str_array) ==
2514 dbus_message_iter_get_basic(&str_array, &val);
2516 if (servers_str->len > 0)
2517 g_string_append_printf(servers_str,
2520 g_string_append(servers_str, val);
2522 dbus_message_iter_next(&str_array);
2524 } else if (g_str_equal(key, "Excludes") == TRUE) {
2525 DBusMessageIter str_array;
2527 if (type != DBUS_TYPE_ARRAY)
2530 excludes_str = g_string_new(NULL);
2531 if (excludes_str == NULL)
2534 dbus_message_iter_recurse(&variant, &str_array);
2536 while (dbus_message_iter_get_arg_type(&str_array) ==
2540 dbus_message_iter_get_basic(&str_array, &val);
2542 if (excludes_str->len > 0)
2543 g_string_append_printf(excludes_str,
2546 g_string_append(excludes_str, val);
2548 dbus_message_iter_next(&str_array);
2552 dbus_message_iter_next(&dict);
2556 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2558 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2559 if (servers_str == NULL && service->proxies == NULL)
2562 if (servers_str != NULL) {
2563 g_strfreev(service->proxies);
2565 if (servers_str->len > 0)
2566 service->proxies = g_strsplit_set(
2567 servers_str->str, " ", 0);
2569 service->proxies = NULL;
2572 if (excludes_str != NULL) {
2573 g_strfreev(service->excludes);
2575 if (excludes_str->len > 0)
2576 service->excludes = g_strsplit_set(
2577 excludes_str->str, " ", 0);
2579 service->excludes = NULL;
2582 if (service->proxies == NULL)
2583 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2586 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2587 g_free(service->pac);
2589 if (url != NULL && strlen(url) > 0)
2590 service->pac = g_strdup(url);
2592 service->pac = NULL;
2594 /* if we are connected:
2595 - if service->pac == NULL
2596 - if __connman_ipconfig_get_proxy_autoconfig(
2597 service->ipconfig) == NULL
2598 --> We should start WPAD */
2601 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2605 if (servers_str != NULL)
2606 g_string_free(servers_str, TRUE);
2608 if (excludes_str != NULL)
2609 g_string_free(excludes_str, TRUE);
2611 service->proxy_config = method;
2616 if (servers_str != NULL)
2617 g_string_free(servers_str, TRUE);
2619 if (excludes_str != NULL)
2620 g_string_free(excludes_str, TRUE);
2625 static int set_ipconfig(struct connman_service *service,
2626 struct connman_ipconfig *ipconfig,
2627 DBusMessageIter *array,
2628 enum connman_service_state state,
2629 enum connman_service_state *new_state)
2631 enum connman_ipconfig_method old_method;
2632 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2633 enum connman_ipconfig_type type;
2636 if (ipconfig == NULL)
2639 old_method = __connman_ipconfig_get_method(ipconfig);
2641 if (is_connecting_state(service, state) ||
2642 is_connected_state(service, state))
2643 __connman_network_clear_ipconfig(service->network, ipconfig);
2645 err = __connman_ipconfig_set_config(ipconfig, array);
2646 method = __connman_ipconfig_get_method(ipconfig);
2647 type = __connman_ipconfig_get_config_type(ipconfig);
2649 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2650 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2651 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2652 *new_state = service->state_ipv4 =
2653 CONNMAN_SERVICE_STATE_CONFIGURATION;
2654 __connman_ipconfig_enable(ipconfig);
2657 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2658 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2659 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2660 *new_state = service->state_ipv6;
2661 __connman_ipconfig_enable(ipconfig);
2665 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2666 type, method, state2string(*new_state));
2671 static DBusMessage *set_property(DBusConnection *conn,
2672 DBusMessage *msg, void *user_data)
2674 struct connman_service *service = user_data;
2675 DBusMessageIter iter, value;
2679 DBG("service %p", service);
2681 if (dbus_message_iter_init(msg, &iter) == FALSE)
2682 return __connman_error_invalid_arguments(msg);
2684 dbus_message_iter_get_basic(&iter, &name);
2685 dbus_message_iter_next(&iter);
2686 dbus_message_iter_recurse(&iter, &value);
2688 type = dbus_message_iter_get_arg_type(&value);
2690 if (g_str_equal(name, "AutoConnect") == TRUE) {
2691 connman_bool_t autoconnect;
2693 if (type != DBUS_TYPE_BOOLEAN)
2694 return __connman_error_invalid_arguments(msg);
2696 if (service->favorite == FALSE)
2697 return __connman_error_invalid_service(msg);
2699 dbus_message_iter_get_basic(&value, &autoconnect);
2701 if (service->autoconnect == autoconnect)
2702 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2704 service->autoconnect = autoconnect;
2706 autoconnect_changed(service);
2708 service_save(service);
2709 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2710 const char *passphrase;
2712 if (type != DBUS_TYPE_STRING)
2713 return __connman_error_invalid_arguments(msg);
2715 if (service->immutable == TRUE || service->hidden == TRUE)
2716 return __connman_error_not_supported(msg);
2718 dbus_message_iter_get_basic(&value, &passphrase);
2720 __connman_service_set_passphrase(service, passphrase);
2721 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2722 DBusMessageIter entry;
2727 if (type != DBUS_TYPE_ARRAY)
2728 return __connman_error_invalid_arguments(msg);
2730 str = g_string_new(NULL);
2732 return __connman_error_invalid_arguments(msg);
2734 index = connman_network_get_index(service->network);
2735 gw = __connman_ipconfig_get_gateway_from_index(index);
2737 if (gw && strlen(gw))
2738 __connman_service_nameserver_del_routes(service);
2740 dbus_message_iter_recurse(&value, &entry);
2742 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2744 dbus_message_iter_get_basic(&entry, &val);
2745 dbus_message_iter_next(&entry);
2747 g_string_append_printf(str, " %s", val);
2749 g_string_append(str, val);
2752 g_strfreev(service->nameservers_config);
2755 service->nameservers_config =
2756 g_strsplit_set(str->str, " ", 0);
2758 service->nameservers_config = NULL;
2761 g_string_free(str, TRUE);
2763 if (gw && strlen(gw))
2764 __connman_service_nameserver_add_routes(service, gw);
2766 update_nameservers(service);
2767 dns_configuration_changed(service);
2769 service_save(service);
2770 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2771 DBusMessageIter entry;
2774 if (type != DBUS_TYPE_ARRAY)
2775 return __connman_error_invalid_arguments(msg);
2777 str = g_string_new(NULL);
2779 return __connman_error_invalid_arguments(msg);
2781 dbus_message_iter_recurse(&value, &entry);
2783 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2785 dbus_message_iter_get_basic(&entry, &val);
2786 dbus_message_iter_next(&entry);
2788 g_string_append_printf(str, " %s", val);
2790 g_string_append(str, val);
2793 g_strfreev(service->domains);
2796 service->domains = g_strsplit_set(str->str, " ", 0);
2798 service->domains = NULL;
2800 g_string_free(str, TRUE);
2802 update_nameservers(service);
2803 domain_configuration_changed(service);
2805 service_save(service);
2806 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2809 if (type != DBUS_TYPE_ARRAY)
2810 return __connman_error_invalid_arguments(msg);
2812 err = update_proxy_configuration(service, &value);
2815 return __connman_error_failed(msg, -err);
2817 proxy_configuration_changed(service);
2819 __connman_notifier_proxy_changed(service);
2821 service_save(service);
2822 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2823 g_str_equal(name, "IPv6.Configuration")) {
2825 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2826 enum connman_service_state state =
2827 CONNMAN_SERVICE_STATE_UNKNOWN;
2832 if (service->ipconfig_ipv4 == NULL &&
2833 service->ipconfig_ipv6 == NULL)
2834 return __connman_error_invalid_property(msg);
2836 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2837 ipv4 = service->ipconfig_ipv4;
2838 err = set_ipconfig(service, ipv4, &value,
2839 service->state_ipv4, &state);
2841 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2842 ipv6 = service->ipconfig_ipv6;
2843 err = set_ipconfig(service, ipv6, &value,
2844 service->state_ipv6, &state);
2848 if (is_connected_state(service, state) ||
2849 is_connecting_state(service, state))
2850 __connman_network_set_ipconfig(service->network,
2852 return __connman_error_failed(msg, -err);
2856 ipv4_configuration_changed(service);
2858 ipv6_configuration_changed(service);
2860 if (is_connecting(service) || is_connected(service))
2861 __connman_network_set_ipconfig(service->network,
2864 service_save(service);
2866 return __connman_error_invalid_property(msg);
2868 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2871 static void set_idle(struct connman_service *service)
2873 service->state = service->state_ipv4 = service->state_ipv6 =
2874 CONNMAN_SERVICE_STATE_IDLE;
2875 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2876 state_changed(service);
2879 static DBusMessage *clear_property(DBusConnection *conn,
2880 DBusMessage *msg, void *user_data)
2882 struct connman_service *service = user_data;
2885 DBG("service %p", service);
2887 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2890 if (g_str_equal(name, "Error") == TRUE) {
2893 g_get_current_time(&service->modified);
2894 service_save(service);
2895 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2896 if (service->immutable == TRUE || service->hidden == TRUE)
2897 return __connman_error_not_supported(msg);
2899 g_free(service->passphrase);
2900 service->passphrase = NULL;
2902 service_save(service);
2904 return __connman_error_invalid_property(msg);
2906 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2909 static connman_bool_t is_ignore(struct connman_service *service)
2911 if (service->autoconnect == FALSE)
2914 if (service->roaming == TRUE)
2917 if (service->ignore == TRUE)
2920 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
2926 void __connman_service_auto_connect(void)
2928 struct connman_service *service = NULL;
2929 GSequenceIter *iter;
2933 if (__connman_session_mode() == TRUE) {
2934 DBG("Session mode enabled: auto connect disabled");
2938 iter = g_sequence_get_begin_iter(service_list);
2940 while (g_sequence_iter_is_end(iter) == FALSE) {
2941 service = g_sequence_get(iter);
2943 if (service->pending != NULL)
2946 if (is_connecting(service) == TRUE)
2949 if (service->favorite == FALSE)
2952 if (is_connected(service) == TRUE)
2955 if (is_ignore(service) == FALSE && service->state ==
2956 CONNMAN_SERVICE_STATE_IDLE)
2961 iter = g_sequence_iter_next(iter);
2964 if (service != NULL) {
2965 service->userconnect = FALSE;
2966 __connman_service_connect(service);
2970 static void remove_timeout(struct connman_service *service)
2972 if (service->timeout > 0) {
2973 g_source_remove(service->timeout);
2974 service->timeout = 0;
2978 static void reply_pending(struct connman_service *service, int error)
2980 remove_timeout(service);
2982 if (service->pending != NULL) {
2986 reply = __connman_error_failed(service->pending,
2989 g_dbus_send_message(connection, reply);
2993 sender = dbus_message_get_interface(service->pending);
2995 DBG("sender %s", sender);
2997 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2998 g_dbus_send_reply(connection, service->pending,
2999 DBUS_TYPE_OBJECT_PATH, &service->path,
3002 g_dbus_send_reply(connection, service->pending,
3006 dbus_message_unref(service->pending);
3007 service->pending = NULL;
3011 static gboolean connect_timeout(gpointer user_data)
3013 struct connman_service *service = user_data;
3014 connman_bool_t autoconnect = FALSE;
3016 DBG("service %p", service);
3018 service->timeout = 0;
3020 if (service->network != NULL)
3021 __connman_network_disconnect(service->network);
3023 __connman_ipconfig_disable(service->ipconfig_ipv4);
3024 __connman_ipconfig_disable(service->ipconfig_ipv6);
3026 __connman_stats_service_unregister(service);
3028 if (service->pending != NULL) {
3031 reply = __connman_error_operation_timeout(service->pending);
3033 g_dbus_send_message(connection, reply);
3035 dbus_message_unref(service->pending);
3036 service->pending = NULL;
3040 __connman_service_ipconfig_indicate_state(service,
3041 CONNMAN_SERVICE_STATE_FAILURE,
3042 CONNMAN_IPCONFIG_TYPE_IPV4);
3043 __connman_service_ipconfig_indicate_state(service,
3044 CONNMAN_SERVICE_STATE_FAILURE,
3045 CONNMAN_IPCONFIG_TYPE_IPV6);
3047 if (autoconnect == TRUE && service->userconnect == FALSE)
3048 __connman_service_auto_connect();
3053 static void set_reconnect_state(struct connman_service *service,
3054 connman_bool_t reconnect)
3056 struct connman_device *device;
3058 if (service->network == NULL)
3061 device = connman_network_get_device(service->network);
3065 __connman_device_set_reconnect(device, reconnect);
3068 static connman_bool_t get_reconnect_state(struct connman_service *service)
3070 struct connman_device *device;
3072 if (service->network == NULL)
3075 device = connman_network_get_device(service->network);
3079 return __connman_device_get_reconnect(device);
3082 static DBusMessage *connect_service(DBusConnection *conn,
3083 DBusMessage *msg, void *user_data)
3085 struct connman_service *service = user_data;
3086 GSequenceIter *iter;
3089 DBG("service %p", service);
3091 if (service->pending != NULL)
3092 return __connman_error_in_progress(msg);
3094 iter = g_sequence_get_begin_iter(service_list);
3096 while (g_sequence_iter_is_end(iter) == FALSE) {
3097 struct connman_service *temp = g_sequence_get(iter);
3099 if (service->type == temp->type && is_connecting(temp) == TRUE)
3100 return __connman_error_in_progress(msg);
3102 iter = g_sequence_iter_next(iter);
3105 service->ignore = FALSE;
3107 service->userconnect = TRUE;
3109 service->pending = dbus_message_ref(msg);
3111 set_reconnect_state(service, FALSE);
3113 err = __connman_service_connect(service);
3115 if (service->pending == NULL)
3118 if (err != -EINPROGRESS) {
3119 dbus_message_unref(service->pending);
3120 service->pending = NULL;
3122 return __connman_error_failed(msg, -err);
3128 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3131 static DBusMessage *disconnect_service(DBusConnection *conn,
3132 DBusMessage *msg, void *user_data)
3134 struct connman_service *service = user_data;
3137 DBG("service %p", service);
3139 reply_pending(service, ECONNABORTED);
3141 service->ignore = TRUE;
3143 set_reconnect_state(service, FALSE);
3145 err = __connman_service_disconnect(service);
3147 if (err != -EINPROGRESS)
3148 return __connman_error_failed(msg, -err);
3153 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3156 static DBusMessage *remove_service(DBusConnection *conn,
3157 DBusMessage *msg, void *user_data)
3159 struct connman_service *service = user_data;
3161 DBG("service %p", service);
3163 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
3164 return __connman_error_not_supported(msg);
3166 if (service->immutable == TRUE || service->hidden == TRUE)
3167 return __connman_error_not_supported(msg);
3169 if (service->favorite == FALSE && service->state !=
3170 CONNMAN_SERVICE_STATE_FAILURE)
3171 return __connman_error_not_supported(msg);
3173 set_reconnect_state(service, FALSE);
3175 __connman_service_disconnect(service);
3177 g_free(service->passphrase);
3178 service->passphrase = NULL;
3182 __connman_service_set_favorite(service, FALSE);
3183 service_save(service);
3185 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3188 static gboolean check_suitable_state(enum connman_service_state a,
3189 enum connman_service_state b)
3192 * Special check so that "ready" service can be moved before
3195 if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
3196 b == CONNMAN_SERVICE_STATE_READY) ||
3197 (b == CONNMAN_SERVICE_STATE_ONLINE &&
3198 a == CONNMAN_SERVICE_STATE_READY))
3204 static void downgrade_state(struct connman_service *service)
3206 if (service == NULL)
3209 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
3210 service->state_ipv6);
3212 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
3213 __connman_service_ipconfig_indicate_state(service,
3214 CONNMAN_SERVICE_STATE_READY,
3215 CONNMAN_IPCONFIG_TYPE_IPV4);
3217 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
3218 __connman_service_ipconfig_indicate_state(service,
3219 CONNMAN_SERVICE_STATE_READY,
3220 CONNMAN_IPCONFIG_TYPE_IPV6);
3223 static void apply_relevant_default_downgrade(struct connman_service *service)
3225 struct connman_service *def_service;
3227 def_service = get_default();
3228 if (def_service == NULL)
3231 if (def_service == service &&
3232 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3233 def_service->state = CONNMAN_SERVICE_STATE_READY;
3236 static DBusMessage *move_service(DBusConnection *conn,
3237 DBusMessage *msg, void *user_data,
3240 struct connman_service *service = user_data;
3241 struct connman_service *target;
3243 GSequenceIter *src, *dst;
3244 enum connman_ipconfig_method target4, target6;
3245 enum connman_ipconfig_method service4, service6;
3247 DBG("service %p", service);
3249 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3252 if (service->favorite == FALSE)
3253 return __connman_error_not_supported(msg);
3255 target = find_service(path);
3256 if (target == NULL || target->favorite == FALSE || target == service ||
3257 target->type == CONNMAN_SERVICE_TYPE_VPN)
3258 return __connman_error_invalid_service(msg);
3260 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3261 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3262 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3263 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3265 DBG("target %s method %d/%d state %d/%d", target->identifier,
3267 target->state_ipv4, target->state_ipv6);
3269 DBG("service %s method %d/%d state %d/%d", service->identifier,
3271 service->state_ipv4, service->state_ipv6);
3274 * If method is OFF, then we do not need to check the corresponding
3277 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3278 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3279 if (check_suitable_state(target->state_ipv6,
3280 service->state_ipv6) == FALSE)
3281 return __connman_error_invalid_service(msg);
3285 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3286 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3287 if (check_suitable_state(target->state_ipv4,
3288 service->state_ipv4) == FALSE)
3289 return __connman_error_invalid_service(msg);
3293 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3294 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3295 if (check_suitable_state(target->state_ipv6,
3296 service->state_ipv6) == FALSE)
3297 return __connman_error_invalid_service(msg);
3301 if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3302 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3303 if (check_suitable_state(target->state_ipv4,
3304 service->state_ipv4) == FALSE)
3305 return __connman_error_invalid_service(msg);
3309 g_get_current_time(&service->modified);
3310 service_save(service);
3312 src = g_hash_table_lookup(service_hash, service->identifier);
3313 dst = g_hash_table_lookup(service_hash, target->identifier);
3316 * If the service which goes down is the default service and is
3317 * online, we downgrade directly its state to ready so:
3318 * the service which goes up, needs to recompute its state which
3319 * is triggered via downgrading it - if relevant - to state ready.
3321 if (before == TRUE) {
3322 apply_relevant_default_downgrade(target);
3323 g_sequence_move(src, dst);
3324 downgrade_state(service);
3326 apply_relevant_default_downgrade(service);
3327 g_sequence_move(dst, src);
3328 downgrade_state(target);
3331 services_changed(FALSE);
3333 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3336 static DBusMessage *move_before(DBusConnection *conn,
3337 DBusMessage *msg, void *user_data)
3339 return move_service(conn, msg, user_data, TRUE);
3342 static DBusMessage *move_after(DBusConnection *conn,
3343 DBusMessage *msg, void *user_data)
3345 return move_service(conn, msg, user_data, FALSE);
3348 static DBusMessage *reset_counters(DBusConnection *conn,
3349 DBusMessage *msg, void *user_data)
3351 struct connman_service *service = user_data;
3353 reset_stats(service);
3355 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3358 static GDBusMethodTable service_methods[] = {
3359 { "GetProperties", "", "a{sv}", get_properties },
3360 { "SetProperty", "sv", "", set_property },
3361 { "ClearProperty", "s", "", clear_property },
3362 { "Connect", "", "", connect_service,
3363 G_DBUS_METHOD_FLAG_ASYNC },
3364 { "Disconnect", "", "", disconnect_service },
3365 { "Remove", "", "", remove_service },
3366 { "MoveBefore", "o", "", move_before },
3367 { "MoveAfter", "o", "", move_after },
3368 { "ResetCounters", "", "", reset_counters },
3372 static GDBusSignalTable service_signals[] = {
3373 { "PropertyChanged", "sv" },
3377 static void service_free(gpointer user_data)
3379 struct connman_service *service = user_data;
3380 char *path = service->path;
3382 DBG("service %p", service);
3384 reply_pending(service, ENOENT);
3386 g_hash_table_remove(service_hash, service->identifier);
3388 __connman_notifier_service_remove(service);
3390 stats_stop(service);
3392 service->path = NULL;
3395 services_changed(FALSE);
3397 g_dbus_unregister_interface(connection, path,
3398 CONNMAN_SERVICE_INTERFACE);
3402 g_hash_table_destroy(service->counter_table);
3404 if (service->network != NULL) {
3405 __connman_network_disconnect(service->network);
3406 if (service->network_created == TRUE)
3407 connman_network_unref(service->network);
3410 if (service->provider != NULL)
3411 connman_provider_unref(service->provider);
3413 if (service->ipconfig_ipv4 != NULL) {
3414 connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3415 connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3416 connman_ipconfig_unref(service->ipconfig_ipv4);
3417 service->ipconfig_ipv4 = NULL;
3420 if (service->ipconfig_ipv6 != NULL) {
3421 connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3422 connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3423 connman_ipconfig_unref(service->ipconfig_ipv6);
3424 service->ipconfig_ipv6 = NULL;
3427 g_strfreev(service->nameservers);
3428 g_strfreev(service->nameservers_config);
3429 g_strfreev(service->nameservers_auto);
3430 g_strfreev(service->domains);
3431 g_strfreev(service->proxies);
3432 g_strfreev(service->excludes);
3434 g_free(service->domainname);
3435 g_free(service->pac);
3436 g_free(service->name);
3437 g_free(service->passphrase);
3438 g_free(service->agent_passphrase);
3439 g_free(service->identifier);
3440 g_free(service->eap);
3441 g_free(service->identity);
3442 g_free(service->agent_identity);
3443 g_free(service->ca_cert_file);
3444 g_free(service->client_cert_file);
3445 g_free(service->private_key_file);
3446 g_free(service->private_key_passphrase);
3447 g_free(service->phase2);
3449 if (service->stats.timer != NULL)
3450 g_timer_destroy(service->stats.timer);
3451 if (service->stats_roaming.timer != NULL)
3452 g_timer_destroy(service->stats_roaming.timer);
3458 * __connman_service_put:
3459 * @service: service structure
3461 * Release service if no longer needed
3463 void __connman_service_put(struct connman_service *service)
3465 GSequenceIter *iter;
3467 DBG("service %p", service);
3469 if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
3472 iter = g_hash_table_lookup(service_hash, service->identifier);
3474 reply_pending(service, ECONNABORTED);
3476 __connman_service_disconnect(service);
3478 g_sequence_remove(iter);
3480 service_free(service);
3484 static void stats_init(struct connman_service *service)
3487 service->stats.valid = FALSE;
3488 service->stats.enabled = FALSE;
3489 service->stats.timer = g_timer_new();
3492 service->stats_roaming.valid = FALSE;
3493 service->stats_roaming.enabled = FALSE;
3494 service->stats_roaming.timer = g_timer_new();
3497 static void service_initialize(struct connman_service *service)
3499 DBG("service %p", service);
3501 service->refcount = 1;
3502 service->session_usage_count = 0;
3504 service->network_created = FALSE;
3506 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3507 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3509 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3510 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3511 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3513 service->favorite = FALSE;
3514 service->immutable = FALSE;
3515 service->hidden = FALSE;
3517 service->ignore = FALSE;
3519 service->userconnect = FALSE;
3523 stats_init(service);
3525 service->provider = NULL;
3527 service->wps = FALSE;
3531 * connman_service_create:
3533 * Allocate a new service.
3535 * Returns: a newly-allocated #connman_service structure
3537 struct connman_service *connman_service_create(void)
3540 struct connman_stats_counter *counters;
3541 const char *counter;
3543 struct connman_service *service;
3545 service = g_try_new0(struct connman_service, 1);
3546 if (service == NULL)
3549 DBG("service %p", service);
3551 service->counter_table = g_hash_table_new_full(g_str_hash,
3552 g_str_equal, NULL, g_free);
3554 for (list = counter_list; list; list = list->next) {
3555 counter = list->data;
3557 counters = g_try_new0(struct connman_stats_counter, 1);
3558 if (counters == NULL) {
3559 g_hash_table_destroy(service->counter_table);
3564 counters->append_all = TRUE;
3566 g_hash_table_replace(service->counter_table, (gpointer)counter,
3570 service_initialize(service);
3576 * connman_service_ref:
3577 * @service: service structure
3579 * Increase reference counter of service
3581 struct connman_service *connman_service_ref(struct connman_service *service)
3585 __sync_fetch_and_add(&service->refcount, 1);
3591 * connman_service_unref:
3592 * @service: service structure
3594 * Decrease reference counter of service
3596 void connman_service_unref(struct connman_service *service)
3598 __connman_service_put(service);
3601 static gint service_compare(gconstpointer a, gconstpointer b,
3604 struct connman_service *service_a = (void *) a;
3605 struct connman_service *service_b = (void *) b;
3606 enum connman_service_state state_a, state_b;
3608 state_a = service_a->state;
3609 state_b = service_b->state;
3611 if (state_a != state_b) {
3612 gboolean a_connected = is_connected(service_a);
3613 gboolean b_connected = is_connected(service_b);
3615 if (a_connected == TRUE && b_connected == TRUE) {
3616 /* We prefer online over ready state */
3617 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
3620 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
3624 if (a_connected == TRUE)
3626 if (b_connected == TRUE)
3629 if (is_connecting(service_a) == TRUE)
3631 if (is_connecting(service_b) == TRUE)
3635 if (service_a->order > service_b->order)
3638 if (service_a->order < service_b->order)
3641 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3644 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3647 if (service_a->type != service_b->type) {
3648 switch (service_a->type) {
3649 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3650 case CONNMAN_SERVICE_TYPE_SYSTEM:
3651 case CONNMAN_SERVICE_TYPE_ETHERNET:
3652 case CONNMAN_SERVICE_TYPE_GPS:
3653 case CONNMAN_SERVICE_TYPE_VPN:
3654 case CONNMAN_SERVICE_TYPE_GADGET:
3656 case CONNMAN_SERVICE_TYPE_WIFI:
3658 case CONNMAN_SERVICE_TYPE_WIMAX:
3659 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3660 case CONNMAN_SERVICE_TYPE_CELLULAR:
3665 return (gint) service_b->strength - (gint) service_a->strength;
3669 * connman_service_get_type:
3670 * @service: service structure
3672 * Get the type of service
3674 enum connman_service_type connman_service_get_type(struct connman_service *service)
3676 if (service == NULL)
3677 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3679 return service->type;
3683 * connman_service_get_interface:
3684 * @service: service structure
3686 * Get network interface of service
3688 char *connman_service_get_interface(struct connman_service *service)
3692 if (service == NULL)
3695 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3696 if (service->ipconfig_ipv4)
3697 index = connman_ipconfig_get_index(
3698 service->ipconfig_ipv4);
3699 else if (service->ipconfig_ipv6)
3700 index = connman_ipconfig_get_index(
3701 service->ipconfig_ipv6);
3705 return connman_inet_ifname(index);
3708 if (service->network == NULL)
3711 index = connman_network_get_index(service->network);
3713 return connman_inet_ifname(index);
3717 * connman_service_get_network:
3718 * @service: service structure
3720 * Get the service network
3722 struct connman_network *
3723 __connman_service_get_network(struct connman_service *service)
3725 if (service == NULL)
3728 return service->network;
3731 struct connman_ipconfig *
3732 __connman_service_get_ip4config(struct connman_service *service)
3734 if (service == NULL)
3737 return service->ipconfig_ipv4;
3740 struct connman_ipconfig *
3741 __connman_service_get_ip6config(struct connman_service *service)
3743 if (service == NULL)
3746 return service->ipconfig_ipv6;
3749 struct connman_ipconfig *
3750 __connman_service_get_ipconfig(struct connman_service *service, int family)
3752 if (family == AF_INET)
3753 return __connman_service_get_ip4config(service);
3754 else if (family == AF_INET6)
3755 return __connman_service_get_ip6config(service);
3761 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3763 if (service == NULL)
3764 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3766 return service->security;
3769 const char *__connman_service_get_phase2(struct connman_service *service)
3771 if (service == NULL)
3774 return service->phase2;
3777 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3779 if (service == NULL)
3782 return service->wps;
3786 * __connman_service_set_favorite:
3787 * @service: service structure
3788 * @favorite: favorite value
3790 * Change the favorite setting of service
3792 int __connman_service_set_favorite(struct connman_service *service,
3793 connman_bool_t favorite)
3795 GSequenceIter *iter;
3797 if (service->hidden == TRUE)
3799 iter = g_hash_table_lookup(service_hash, service->identifier);
3803 if (service->favorite == favorite)
3806 service->favorite = favorite;
3807 service->order = __connman_service_get_order(service);
3809 favorite_changed(service);
3811 g_sequence_sort_changed(iter, service_compare, NULL);
3813 services_changed(FALSE);
3818 int __connman_service_set_immutable(struct connman_service *service,
3819 connman_bool_t immutable)
3821 if (service->hidden == TRUE)
3823 service->immutable = immutable;
3825 immutable_changed(service);
3830 void __connman_service_set_string(struct connman_service *service,
3831 const char *key, const char *value)
3833 if (service->hidden == TRUE)
3835 if (g_str_equal(key, "EAP") == TRUE) {
3836 g_free(service->eap);
3837 service->eap = g_strdup(value);
3838 } else if (g_str_equal(key, "Identity") == TRUE) {
3839 g_free(service->identity);
3840 service->identity = g_strdup(value);
3841 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3842 g_free(service->ca_cert_file);
3843 service->ca_cert_file = g_strdup(value);
3844 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3845 g_free(service->client_cert_file);
3846 service->client_cert_file = g_strdup(value);
3847 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3848 g_free(service->private_key_file);
3849 service->private_key_file = g_strdup(value);
3850 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3851 g_free(service->private_key_passphrase);
3852 service->private_key_passphrase = g_strdup(value);
3853 } else if (g_str_equal(key, "Phase2") == TRUE) {
3854 g_free(service->phase2);
3855 service->phase2 = g_strdup(value);
3856 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3857 g_free(service->passphrase);
3858 service->passphrase = g_strdup(value);
3862 static void service_complete(struct connman_service *service)
3864 reply_pending(service, EIO);
3866 if (service->userconnect == FALSE)
3867 __connman_service_auto_connect();
3869 g_get_current_time(&service->modified);
3870 service_save(service);
3873 static void report_error_cb(struct connman_service *service,
3874 gboolean retry, void *user_data)
3877 __connman_service_connect(service);
3879 service_complete(service);
3880 services_changed(FALSE);
3881 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3885 void __connman_service_add_passphrase(struct connman_service *service,
3886 const gchar *passphrase)
3888 switch (service->security) {
3889 case CONNMAN_SERVICE_SECURITY_WEP:
3890 case CONNMAN_SERVICE_SECURITY_PSK:
3891 __connman_service_set_passphrase(service, passphrase);
3893 case CONNMAN_SERVICE_SECURITY_8021X:
3894 __connman_service_set_agent_passphrase(service,
3897 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3898 case CONNMAN_SERVICE_SECURITY_NONE:
3899 case CONNMAN_SERVICE_SECURITY_WPA:
3900 case CONNMAN_SERVICE_SECURITY_RSN:
3901 DBG("service security '%s' not handled",
3902 security2string(service->security));
3908 static void request_input_cb (struct connman_service *service,
3909 connman_bool_t values_received,
3910 const char *name, int name_len,
3911 const char *identity, const char *passphrase,
3914 struct connman_device *device;
3916 DBG ("RequestInput return, %p", service);
3918 if (service->hidden == TRUE && name_len > 0 && name_len <= 32) {
3919 device = connman_network_get_device(service->network);
3920 __connman_device_request_hidden_scan(device,
3922 identity, passphrase);
3925 if (values_received == FALSE || service->hidden == TRUE) {
3926 service_complete(service);
3927 services_changed(FALSE);
3928 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3932 if (identity != NULL)
3933 __connman_service_set_agent_identity(service, identity);
3935 if (passphrase != NULL)
3936 __connman_service_add_passphrase(service, passphrase);
3938 __connman_service_connect(service);
3940 /* Never cache agent provided credentials */
3941 __connman_service_set_agent_identity(service, NULL);
3942 __connman_service_set_agent_passphrase(service, NULL);
3945 static void downgrade_connected_services(void)
3947 struct connman_service *up_service;
3948 GSequenceIter *iter;
3950 iter = g_sequence_get_begin_iter(service_list);
3951 while (g_sequence_iter_is_end(iter) == FALSE) {
3952 up_service = g_sequence_get(iter);
3954 if (is_connected(up_service) == FALSE) {
3955 iter = g_sequence_iter_next(iter);
3959 if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3962 downgrade_state(up_service);
3964 iter = g_sequence_iter_next(iter);
3968 static int service_indicate_state(struct connman_service *service)
3970 enum connman_service_state old_state, new_state;
3971 struct connman_service *def_service;
3972 GSequenceIter *iter;
3974 if (service == NULL)
3977 old_state = service->state;
3978 new_state = combine_state(service->state_ipv4, service->state_ipv6);
3980 DBG("service %p old %s - new %s/%s => %s",
3982 state2string(old_state),
3983 state2string(service->state_ipv4),
3984 state2string(service->state_ipv6),
3985 state2string(new_state));
3987 if (old_state == new_state)
3990 def_service = get_default();
3992 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
3993 if (def_service != NULL && def_service != service &&
3994 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3998 service->state = new_state;
3999 state_changed(service);
4001 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
4002 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
4003 reply_pending(service, ECONNABORTED);
4005 __connman_service_disconnect(service);
4008 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
4009 if (__connman_stats_service_register(service) == 0) {
4010 __connman_stats_get(service, FALSE,
4011 &service->stats.data);
4012 __connman_stats_get(service, TRUE,
4013 &service->stats_roaming.data);
4017 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4018 connman_timeserver_sync();
4021 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
4022 connman_bool_t reconnect;
4024 reconnect = get_reconnect_state(service);
4025 if (reconnect == TRUE)
4026 __connman_service_auto_connect();
4028 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4031 if (new_state == CONNMAN_SERVICE_STATE_READY) {
4032 enum connman_ipconfig_method method;
4034 set_reconnect_state(service, TRUE);
4036 __connman_service_set_favorite(service, TRUE);
4038 reply_pending(service, 0);
4040 service->userconnect = FALSE;
4042 g_get_current_time(&service->modified);
4043 service_save(service);
4045 update_nameservers(service);
4046 dns_changed(service);
4047 domain_changed(service);
4049 __connman_notifier_connect(service->type);
4051 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4052 connman_network_get_bool(service->network,
4053 "WiFi.UseWPS") == TRUE) {
4056 pass = connman_network_get_string(service->network,
4059 __connman_service_set_passphrase(service, pass);
4061 connman_network_set_bool(service->network,
4062 "WiFi.UseWPS", FALSE);
4067 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4068 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4069 __connman_ipconfig_disable_ipv6(
4070 service->ipconfig_ipv6);
4072 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4073 def_service = get_default();
4075 if (__connman_notifier_count_connected() == 0 &&
4076 def_service != NULL &&
4077 def_service->provider != NULL)
4078 __connman_provider_disconnect(def_service->provider);
4082 __connman_wispr_stop(service);
4084 __connman_wpad_stop(service);
4086 update_nameservers(service);
4087 dns_changed(service);
4088 domain_changed(service);
4090 __connman_notifier_disconnect(service->type);
4093 * Previous services which are connected and which states
4094 * are set to online should reset relevantly ipconfig_state
4095 * to ready so wispr/portal will be rerun on those
4097 downgrade_connected_services();
4100 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4101 if (service->userconnect == TRUE &&
4102 __connman_agent_report_error(service,
4103 error2string(service->error),
4104 report_error_cb, NULL) == -EIO)
4106 service_complete(service);
4108 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4110 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4112 iter = g_hash_table_lookup(service_hash, service->identifier);
4114 g_sequence_sort_changed(iter, service_compare, NULL);
4116 services_changed(FALSE);
4118 if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
4124 int __connman_service_indicate_error(struct connman_service *service,
4125 enum connman_service_error error)
4127 DBG("service %p error %d", service, error);
4129 if (service == NULL)
4132 service->error = error;
4134 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4135 __connman_service_set_passphrase(service, NULL);
4137 __connman_service_ipconfig_indicate_state(service,
4138 CONNMAN_SERVICE_STATE_FAILURE,
4139 CONNMAN_IPCONFIG_TYPE_IPV4);
4140 __connman_service_ipconfig_indicate_state(service,
4141 CONNMAN_SERVICE_STATE_FAILURE,
4142 CONNMAN_IPCONFIG_TYPE_IPV6);
4146 int __connman_service_clear_error(struct connman_service *service)
4148 DBG("service %p", service);
4150 if (service == NULL)
4153 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4156 service->state_ipv4 = service->state_ipv6 =
4157 CONNMAN_SERVICE_STATE_UNKNOWN;
4158 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
4160 if (service->favorite == TRUE)
4161 set_reconnect_state(service, TRUE);
4163 __connman_service_ipconfig_indicate_state(service,
4164 CONNMAN_SERVICE_STATE_IDLE,
4165 CONNMAN_IPCONFIG_TYPE_IPV6);
4168 * Toggling the IPv6 state to IDLE could trigger the auto connect
4169 * machinery and consequently the IPv4 state.
4171 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4172 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4175 return __connman_service_ipconfig_indicate_state(service,
4176 CONNMAN_SERVICE_STATE_IDLE,
4177 CONNMAN_IPCONFIG_TYPE_IPV4);
4180 int __connman_service_indicate_default(struct connman_service *service)
4182 DBG("service %p", service);
4189 enum connman_service_state __connman_service_ipconfig_get_state(
4190 struct connman_service *service,
4191 enum connman_ipconfig_type type)
4193 if (service == NULL)
4194 return CONNMAN_SERVICE_STATE_UNKNOWN;
4196 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4197 return service->state_ipv4;
4199 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4200 return service->state_ipv6;
4202 return CONNMAN_SERVICE_STATE_UNKNOWN;
4205 static void check_proxy_setup(struct connman_service *service)
4208 * We start WPAD if we haven't got a PAC URL from DHCP and
4209 * if our proxy manual configuration is either empty or set
4210 * to AUTO with an empty URL.
4213 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4216 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4217 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4218 service->pac != NULL))
4221 if (__connman_wpad_start(service) < 0) {
4222 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4223 __connman_notifier_proxy_changed(service);
4230 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
4234 * How many networks are connected at the same time. If more than 1,
4235 * then set the rp_filter setting properly (loose mode routing) so that network
4236 * connectivity works ok. This is only done for IPv4 networks as IPv6
4237 * does not have rp_filter knob.
4239 static int connected_networks_count;
4240 static int original_rp_filter;
4242 static void service_rp_filter(struct connman_service *service,
4245 enum connman_ipconfig_method method;
4247 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4250 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
4251 case CONNMAN_IPCONFIG_METHOD_OFF:
4252 case CONNMAN_IPCONFIG_METHOD_AUTO:
4254 case CONNMAN_IPCONFIG_METHOD_FIXED:
4255 case CONNMAN_IPCONFIG_METHOD_MANUAL:
4256 case CONNMAN_IPCONFIG_METHOD_DHCP:
4260 if (connected == TRUE) {
4261 if (connected_networks_count == 1) {
4263 filter_value = __connman_ipconfig_set_rp_filter();
4264 if (filter_value < 0)
4267 original_rp_filter = filter_value;
4269 connected_networks_count++;
4272 if (connected_networks_count == 2)
4273 __connman_ipconfig_unset_rp_filter(original_rp_filter);
4275 connected_networks_count--;
4276 if (connected_networks_count < 0)
4277 connected_networks_count = 0;
4280 DBG("%s %s ipconfig %p method %d count %d filter %d",
4281 connected ? "connected" : "disconnected", service->identifier,
4282 service->ipconfig_ipv4, method,
4283 connected_networks_count, original_rp_filter);
4286 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
4287 enum connman_service_state new_state,
4288 enum connman_ipconfig_type type)
4290 struct connman_ipconfig *ipconfig = NULL;
4291 enum connman_service_state old_state;
4294 if (service == NULL)
4297 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4298 old_state = service->state_ipv4;
4299 ipconfig = service->ipconfig_ipv4;
4300 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4301 old_state = service->state_ipv6;
4302 ipconfig = service->ipconfig_ipv6;
4305 if (ipconfig == NULL)
4309 if (old_state == new_state)
4312 DBG("service %p (%s) state %d (%s) type %d (%s)",
4313 service, service ? service->identifier : NULL,
4314 new_state, state2string(new_state),
4315 type, __connman_ipconfig_type2string(type));
4317 switch (new_state) {
4318 case CONNMAN_SERVICE_STATE_UNKNOWN:
4319 case CONNMAN_SERVICE_STATE_IDLE:
4320 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
4323 case CONNMAN_SERVICE_STATE_ASSOCIATION:
4325 case CONNMAN_SERVICE_STATE_CONFIGURATION:
4326 __connman_ipconfig_enable(ipconfig);
4328 case CONNMAN_SERVICE_STATE_READY:
4329 update_nameservers(service);
4331 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4332 check_proxy_setup(service);
4333 service_rp_filter(service, TRUE);
4335 __connman_wispr_start(service, type);
4337 case CONNMAN_SERVICE_STATE_ONLINE:
4339 case CONNMAN_SERVICE_STATE_DISCONNECT:
4340 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
4343 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4344 service_rp_filter(service, FALSE);
4347 case CONNMAN_SERVICE_STATE_FAILURE:
4351 /* We keep that state */
4352 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4353 service->state_ipv4 = new_state;
4354 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4355 service->state_ipv6 = new_state;
4357 ret = service_indicate_state(service);
4360 * If the ipconfig method is OFF, then we set the state to IDLE
4361 * so that it will not affect the combined state in the future.
4363 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4364 enum connman_ipconfig_method method;
4365 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4366 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4367 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4368 service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
4369 ret = service_indicate_state(service);
4372 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4373 enum connman_ipconfig_method method;
4374 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4375 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4376 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4377 service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4378 ret = service_indicate_state(service);
4385 static connman_bool_t prepare_network(struct connman_service *service)
4387 enum connman_network_type type;
4388 unsigned int ssid_len;
4390 type = connman_network_get_type(service->network);
4393 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4394 case CONNMAN_NETWORK_TYPE_VENDOR:
4396 case CONNMAN_NETWORK_TYPE_WIFI:
4397 if (connman_network_get_blob(service->network, "WiFi.SSID",
4401 if (service->passphrase != NULL)
4402 connman_network_set_string(service->network,
4403 "WiFi.Passphrase", service->passphrase);
4405 case CONNMAN_NETWORK_TYPE_ETHERNET:
4406 case CONNMAN_NETWORK_TYPE_WIMAX:
4407 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4408 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4409 case CONNMAN_NETWORK_TYPE_CELLULAR:
4416 static void prepare_8021x(struct connman_service *service)
4418 if (service->eap != NULL)
4419 connman_network_set_string(service->network, "WiFi.EAP",
4422 if (service->identity != NULL)
4423 connman_network_set_string(service->network, "WiFi.Identity",
4426 if (service->ca_cert_file != NULL)
4427 connman_network_set_string(service->network, "WiFi.CACertFile",
4428 service->ca_cert_file);
4430 if (service->client_cert_file != NULL)
4431 connman_network_set_string(service->network,
4432 "WiFi.ClientCertFile",
4433 service->client_cert_file);
4435 if (service->private_key_file != NULL)
4436 connman_network_set_string(service->network,
4437 "WiFi.PrivateKeyFile",
4438 service->private_key_file);
4440 if (service->private_key_passphrase != NULL)
4441 connman_network_set_string(service->network,
4442 "WiFi.PrivateKeyPassphrase",
4443 service->private_key_passphrase);
4445 if (service->phase2 != NULL)
4446 connman_network_set_string(service->network, "WiFi.Phase2",
4450 static int service_connect(struct connman_service *service)
4454 if (service->hidden == TRUE)
4457 switch (service->type) {
4458 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4459 case CONNMAN_SERVICE_TYPE_SYSTEM:
4460 case CONNMAN_SERVICE_TYPE_GPS:
4461 case CONNMAN_SERVICE_TYPE_GADGET:
4463 case CONNMAN_SERVICE_TYPE_ETHERNET:
4464 case CONNMAN_SERVICE_TYPE_WIMAX:
4465 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4466 case CONNMAN_SERVICE_TYPE_CELLULAR:
4467 case CONNMAN_SERVICE_TYPE_VPN:
4469 case CONNMAN_SERVICE_TYPE_WIFI:
4470 switch (service->security) {
4471 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4472 case CONNMAN_SERVICE_SECURITY_NONE:
4474 case CONNMAN_SERVICE_SECURITY_WEP:
4475 case CONNMAN_SERVICE_SECURITY_PSK:
4476 case CONNMAN_SERVICE_SECURITY_WPA:
4477 case CONNMAN_SERVICE_SECURITY_RSN:
4478 if (service->passphrase == NULL) {
4479 if (service->network == NULL)
4482 if (service->wps == FALSE ||
4483 connman_network_get_bool(
4485 "WiFi.UseWPS") == FALSE)
4489 case CONNMAN_SERVICE_SECURITY_8021X:
4490 if (service->eap == NULL)
4494 * never request credentials if using EAP-TLS
4495 * (EAP-TLS networks need to be fully provisioned)
4497 if (g_str_equal(service->eap, "tls") == TRUE)
4501 * Return -ENOKEY if either identity or passphrase is
4502 * missing. Agent provided credentials can be used as
4503 * fallback if needed.
4505 if ((service->identity == NULL &&
4506 service->agent_identity == NULL) ||
4507 (service->passphrase == NULL &&
4508 service->agent_passphrase == NULL))
4516 if (service->network != NULL) {
4517 if (prepare_network(service) == FALSE)
4520 switch (service->security) {
4521 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4522 case CONNMAN_SERVICE_SECURITY_NONE:
4523 case CONNMAN_SERVICE_SECURITY_WEP:
4524 case CONNMAN_SERVICE_SECURITY_PSK:
4525 case CONNMAN_SERVICE_SECURITY_WPA:
4526 case CONNMAN_SERVICE_SECURITY_RSN:
4528 case CONNMAN_SERVICE_SECURITY_8021X:
4529 prepare_8021x(service);
4533 if (__connman_stats_service_register(service) == 0) {
4534 __connman_stats_get(service, FALSE,
4535 &service->stats.data);
4536 __connman_stats_get(service, TRUE,
4537 &service->stats_roaming.data);
4540 if (service->ipconfig_ipv4)
4541 __connman_ipconfig_enable(service->ipconfig_ipv4);
4542 if (service->ipconfig_ipv6)
4543 __connman_ipconfig_enable(service->ipconfig_ipv6);
4545 err = __connman_network_connect(service->network);
4546 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4547 service->provider != NULL)
4548 err = __connman_provider_connect(service->provider);
4553 if (err != -EINPROGRESS) {
4554 __connman_ipconfig_disable(service->ipconfig_ipv4);
4555 __connman_ipconfig_disable(service->ipconfig_ipv6);
4556 __connman_stats_service_unregister(service);
4564 int __connman_service_connect(struct connman_service *service)
4568 DBG("service %p state %s", service, state2string(service->state));
4570 if (is_connected(service) == TRUE)
4573 if (is_connecting(service) == TRUE)
4576 switch (service->type) {
4577 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4578 case CONNMAN_SERVICE_TYPE_SYSTEM:
4579 case CONNMAN_SERVICE_TYPE_GPS:
4580 case CONNMAN_SERVICE_TYPE_GADGET:
4583 err = service_connect(service);
4589 if (err == -EINPROGRESS) {
4590 if (service->timeout == 0)
4591 service->timeout = g_timeout_add_seconds(
4592 CONNECT_TIMEOUT, connect_timeout, service);
4594 return -EINPROGRESS;
4597 __connman_service_ipconfig_indicate_state(service,
4598 CONNMAN_SERVICE_STATE_FAILURE,
4599 CONNMAN_IPCONFIG_TYPE_IPV4);
4600 __connman_service_ipconfig_indicate_state(service,
4601 CONNMAN_SERVICE_STATE_FAILURE,
4602 CONNMAN_IPCONFIG_TYPE_IPV6);
4604 if (service->network != NULL)
4605 __connman_network_disconnect(service->network);
4606 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4607 service->provider != NULL)
4608 __connman_provider_disconnect(service->provider);
4610 if (service->userconnect == TRUE) {
4611 if (err == -ENOKEY || err == -EPERM) {
4612 if (__connman_agent_request_passphrase_input(service,
4615 return -EINPROGRESS;
4617 reply_pending(service, err);
4623 int __connman_service_disconnect(struct connman_service *service)
4627 DBG("service %p", service);
4629 if (service->network != NULL) {
4630 err = __connman_network_disconnect(service->network);
4631 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4632 service->provider != NULL)
4633 err = __connman_provider_disconnect(service->provider);
4637 if (err < 0 && err != -EINPROGRESS)
4640 __connman_6to4_remove(service->ipconfig_ipv4);
4642 if (service->ipconfig_ipv4)
4643 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
4646 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
4649 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
4650 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
4652 __connman_ipconfig_disable(service->ipconfig_ipv4);
4653 __connman_ipconfig_disable(service->ipconfig_ipv6);
4655 __connman_stats_service_unregister(service);
4660 int __connman_service_disconnect_all(void)
4662 GSequenceIter *iter;
4663 GSList *services = NULL, *list;
4667 iter = g_sequence_get_begin_iter(service_list);
4669 while (g_sequence_iter_is_end(iter) == FALSE) {
4670 struct connman_service *service = g_sequence_get(iter);
4672 services = g_slist_prepend(services, service);
4674 iter = g_sequence_iter_next(iter);
4677 for (list = services; list != NULL; list = list->next) {
4678 struct connman_service *service = list->data;
4680 service->ignore = TRUE;
4682 set_reconnect_state(service, FALSE);
4684 __connman_service_disconnect(service);
4694 * lookup_by_identifier:
4695 * @identifier: service identifier
4697 * Look up a service by identifier (reference count will not be increased)
4699 static struct connman_service *lookup_by_identifier(const char *identifier)
4701 GSequenceIter *iter;
4703 iter = g_hash_table_lookup(service_hash, identifier);
4705 return g_sequence_get(iter);
4710 static void provision_changed(gpointer value, gpointer user_data)
4712 struct connman_service *service = value;
4713 char *path = user_data;
4715 __connman_config_provision_service_ident(service, path);
4718 void __connman_service_provision_changed(const char *ident)
4720 g_sequence_foreach(service_list, provision_changed, (void *)ident);
4724 * __connman_service_get:
4725 * @identifier: service identifier
4727 * Look up a service by identifier or create a new one if not found
4729 static struct connman_service *service_get(const char *identifier)
4731 struct connman_service *service;
4732 GSequenceIter *iter;
4734 iter = g_hash_table_lookup(service_hash, identifier);
4736 service = g_sequence_get(iter);
4737 if (service != NULL)
4738 connman_service_ref(service);
4742 service = connman_service_create();
4743 if (service == NULL)
4746 DBG("service %p", service);
4748 service->identifier = g_strdup(identifier);
4750 iter = g_sequence_insert_sorted(service_list, service,
4751 service_compare, NULL);
4753 g_hash_table_insert(service_hash, service->identifier, iter);
4758 static int service_register(struct connman_service *service)
4760 GSequenceIter *iter;
4762 DBG("service %p", service);
4764 if (service->path != NULL)
4767 service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
4768 service->identifier);
4770 DBG("path %s", service->path);
4772 __connman_config_provision_service(service);
4774 service_load(service);
4776 g_dbus_register_interface(connection, service->path,
4777 CONNMAN_SERVICE_INTERFACE,
4778 service_methods, service_signals,
4779 NULL, service, NULL);
4781 iter = g_hash_table_lookup(service_hash, service->identifier);
4783 g_sequence_sort_changed(iter, service_compare, NULL);
4785 services_changed(TRUE);
4790 static void service_up(struct connman_ipconfig *ipconfig)
4792 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4794 DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
4796 link_changed(service);
4798 service->stats.valid = FALSE;
4799 service->stats_roaming.valid = FALSE;
4802 static void service_down(struct connman_ipconfig *ipconfig)
4804 DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
4807 static void service_lower_up(struct connman_ipconfig *ipconfig)
4809 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4811 DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4813 stats_start(service);
4816 static void service_lower_down(struct connman_ipconfig *ipconfig)
4818 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4820 DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4822 stats_stop(service);
4823 service_save(service);
4826 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4828 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4829 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4830 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4832 DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
4834 type = __connman_ipconfig_get_config_type(ipconfig);
4835 method = __connman_ipconfig_get_method(ipconfig);
4837 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4840 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4841 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4842 __connman_service_ipconfig_indicate_state(service,
4843 CONNMAN_SERVICE_STATE_READY,
4844 CONNMAN_IPCONFIG_TYPE_IPV6);
4846 settings_changed(service, ipconfig);
4849 static void service_ip_release(struct connman_ipconfig *ipconfig)
4851 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4852 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4853 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4855 DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4857 type = __connman_ipconfig_get_config_type(ipconfig);
4858 method = __connman_ipconfig_get_method(ipconfig);
4860 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4863 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4864 method == CONNMAN_IPCONFIG_METHOD_OFF)
4865 __connman_service_ipconfig_indicate_state(service,
4866 CONNMAN_SERVICE_STATE_DISCONNECT,
4867 CONNMAN_IPCONFIG_TYPE_IPV6);
4869 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4870 method == CONNMAN_IPCONFIG_METHOD_OFF)
4871 __connman_service_ipconfig_indicate_state(service,
4872 CONNMAN_SERVICE_STATE_DISCONNECT,
4873 CONNMAN_IPCONFIG_TYPE_IPV4);
4875 settings_changed(service, ipconfig);
4878 static const struct connman_ipconfig_ops service_ops = {
4880 .down = service_down,
4881 .lower_up = service_lower_up,
4882 .lower_down = service_lower_down,
4883 .ip_bound = service_ip_bound,
4884 .ip_release = service_ip_release,
4887 static void setup_ip4config(struct connman_service *service, int index,
4888 enum connman_ipconfig_method method)
4890 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4891 CONNMAN_IPCONFIG_TYPE_IPV4);
4892 if (service->ipconfig_ipv4 == NULL)
4895 connman_ipconfig_set_method(service->ipconfig_ipv4, method);
4897 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4899 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4902 static void setup_ip6config(struct connman_service *service, int index)
4904 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4905 CONNMAN_IPCONFIG_TYPE_IPV6);
4906 if (service->ipconfig_ipv6 == NULL)
4909 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4911 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4914 void __connman_service_read_ip4config(struct connman_service *service)
4918 if (service->ipconfig_ipv4 == NULL)
4921 keyfile = connman_storage_load_service(service->identifier);
4922 if (keyfile == NULL)
4925 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4926 service->identifier, "IPv4.");
4928 g_key_file_free(keyfile);
4931 void __connman_service_create_ip4config(struct connman_service *service,
4934 DBG("ipv4 %p", service->ipconfig_ipv4);
4936 if (service->ipconfig_ipv4 != NULL)
4939 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4940 __connman_service_read_ip4config(service);
4943 void __connman_service_read_ip6config(struct connman_service *service)
4947 if (service->ipconfig_ipv6 == NULL)
4950 keyfile = connman_storage_load_service(service->identifier);
4951 if (keyfile == NULL)
4954 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4955 service->identifier, "IPv6.");
4957 g_key_file_free(keyfile);
4960 void __connman_service_create_ip6config(struct connman_service *service,
4963 DBG("ipv6 %p", service->ipconfig_ipv6);
4965 if (service->ipconfig_ipv6 != NULL)
4968 setup_ip6config(service, index);
4970 __connman_service_read_ip6config(service);
4974 * __connman_service_lookup_from_network:
4975 * @network: network structure
4977 * Look up a service by network (reference count will not be increased)
4979 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
4981 struct connman_service *service;
4982 const char *ident, *group;
4985 DBG("network %p", network);
4987 if (network == NULL)
4990 ident = __connman_network_get_ident(network);
4994 group = connman_network_get_group(network);
4998 name = g_strdup_printf("%s_%s_%s",
4999 __connman_network_get_type(network), ident, group);
5000 service = lookup_by_identifier(name);
5006 struct connman_service *__connman_service_lookup_from_index(int index)
5008 struct connman_service *service;
5009 GSequenceIter *iter;
5011 iter = g_sequence_get_begin_iter(service_list);
5013 while (g_sequence_iter_is_end(iter) == FALSE) {
5014 service = g_sequence_get(iter);
5016 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
5020 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
5024 iter = g_sequence_iter_next(iter);
5030 const char *__connman_service_get_ident(struct connman_service *service)
5032 return service->identifier;
5035 const char *__connman_service_get_path(struct connman_service *service)
5037 return service->path;
5040 unsigned int __connman_service_get_order(struct connman_service *service)
5042 GSequenceIter *iter;
5044 if (service == NULL)
5047 if (service->favorite == FALSE) {
5052 iter = g_hash_table_lookup(service_hash, service->identifier);
5054 if (g_sequence_iter_get_position(iter) == 0)
5056 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
5057 service->order = 10;
5063 return service->order;
5066 static enum connman_service_type convert_network_type(struct connman_network *network)
5068 enum connman_network_type type = connman_network_get_type(network);
5071 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5072 case CONNMAN_NETWORK_TYPE_VENDOR:
5074 case CONNMAN_NETWORK_TYPE_ETHERNET:
5075 return CONNMAN_SERVICE_TYPE_ETHERNET;
5076 case CONNMAN_NETWORK_TYPE_WIFI:
5077 return CONNMAN_SERVICE_TYPE_WIFI;
5078 case CONNMAN_NETWORK_TYPE_WIMAX:
5079 return CONNMAN_SERVICE_TYPE_WIMAX;
5080 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5081 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5082 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5083 case CONNMAN_NETWORK_TYPE_CELLULAR:
5084 return CONNMAN_SERVICE_TYPE_CELLULAR;
5087 return CONNMAN_SERVICE_TYPE_UNKNOWN;
5090 static enum connman_service_security convert_wifi_security(const char *security)
5092 if (security == NULL)
5093 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5094 else if (g_str_equal(security, "none") == TRUE)
5095 return CONNMAN_SERVICE_SECURITY_NONE;
5096 else if (g_str_equal(security, "wep") == TRUE)
5097 return CONNMAN_SERVICE_SECURITY_WEP;
5098 else if (g_str_equal(security, "psk") == TRUE)
5099 return CONNMAN_SERVICE_SECURITY_PSK;
5100 else if (g_str_equal(security, "ieee8021x") == TRUE)
5101 return CONNMAN_SERVICE_SECURITY_8021X;
5102 else if (g_str_equal(security, "wpa") == TRUE)
5103 return CONNMAN_SERVICE_SECURITY_WPA;
5104 else if (g_str_equal(security, "rsn") == TRUE)
5105 return CONNMAN_SERVICE_SECURITY_RSN;
5107 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5110 static void update_from_network(struct connman_service *service,
5111 struct connman_network *network)
5113 connman_uint8_t strength = service->strength;
5114 GSequenceIter *iter;
5117 DBG("service %p network %p", service, network);
5119 if (is_connected(service) == TRUE)
5122 if (is_connecting(service) == TRUE)
5125 str = connman_network_get_string(network, "Name");
5127 g_free(service->name);
5128 service->name = g_strdup(str);
5129 service->hidden = FALSE;
5131 g_free(service->name);
5132 service->name = NULL;
5133 service->hidden = TRUE;
5136 service->strength = connman_network_get_strength(network);
5137 service->roaming = connman_network_get_bool(network, "Roaming");
5139 if (service->strength == 0) {
5141 * Filter out 0-values; it's unclear what they mean
5142 * and they cause anomalous sorting of the priority list.
5144 service->strength = strength;
5147 str = connman_network_get_string(network, "WiFi.Security");
5148 service->security = convert_wifi_security(str);
5150 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5151 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5153 if (service->strength > strength && service->network != NULL) {
5154 service->network = network;
5156 strength_changed(service);
5159 if (service->network == NULL)
5160 service->network = network;
5162 iter = g_hash_table_lookup(service_hash, service->identifier);
5164 g_sequence_sort_changed(iter, service_compare, NULL);
5168 * __connman_service_create_from_network:
5169 * @network: network structure
5171 * Look up service by network and if not found, create one
5173 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5175 struct connman_service *service;
5176 struct connman_device *device;
5177 const char *ident, *group;
5181 DBG("network %p", network);
5183 if (network == NULL)
5186 ident = __connman_network_get_ident(network);
5190 group = connman_network_get_group(network);
5194 name = g_strdup_printf("%s_%s_%s",
5195 __connman_network_get_type(network), ident, group);
5196 service = service_get(name);
5199 if (service == NULL)
5202 if (__connman_network_get_weakness(network) == TRUE)
5205 if (service->path != NULL) {
5206 update_from_network(service, network);
5207 services_changed(TRUE);
5211 service->type = convert_network_type(network);
5213 switch (service->type) {
5214 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5215 case CONNMAN_SERVICE_TYPE_SYSTEM:
5216 case CONNMAN_SERVICE_TYPE_ETHERNET:
5217 case CONNMAN_SERVICE_TYPE_WIMAX:
5218 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5219 case CONNMAN_SERVICE_TYPE_GPS:
5220 case CONNMAN_SERVICE_TYPE_VPN:
5221 case CONNMAN_SERVICE_TYPE_GADGET:
5222 service->autoconnect = FALSE;
5224 case CONNMAN_SERVICE_TYPE_WIFI:
5225 case CONNMAN_SERVICE_TYPE_CELLULAR:
5226 service->autoconnect = TRUE;
5230 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5231 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5233 update_from_network(service, network);
5235 index = connman_network_get_index(network);
5237 if (service->ipconfig_ipv4 == NULL)
5238 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5240 if (service->ipconfig_ipv6 == NULL)
5241 setup_ip6config(service, index);
5243 service_register(service);
5245 if (service->favorite == TRUE) {
5246 device = connman_network_get_device(service->network);
5247 if (device && __connman_device_scanning(device) == FALSE)
5248 __connman_service_auto_connect();
5251 __connman_notifier_service_add(service, service->name);
5256 void __connman_service_update_from_network(struct connman_network *network)
5258 connman_bool_t need_sort = FALSE;
5259 struct connman_service *service;
5260 connman_uint8_t strength;
5261 connman_bool_t roaming;
5262 GSequenceIter *iter;
5264 connman_bool_t stats_enable;
5266 DBG("network %p", network);
5268 service = __connman_service_lookup_from_network(network);
5269 if (service == NULL)
5272 if (service->network == NULL)
5275 name = connman_network_get_string(service->network, "Name");
5276 if (g_strcmp0(service->name, name) != 0) {
5277 g_free(service->name);
5278 service->name = g_strdup(name);
5279 connman_dbus_property_changed_basic(service->path,
5280 CONNMAN_SERVICE_INTERFACE, "Name",
5281 DBUS_TYPE_STRING, &service->name);
5284 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5285 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5287 strength = connman_network_get_strength(service->network);
5288 if (strength == service->strength)
5291 service->strength = strength;
5294 strength_changed(service);
5297 roaming = connman_network_get_bool(service->network, "Roaming");
5298 if (roaming == service->roaming)
5301 stats_enable = stats_enabled(service);
5302 if (stats_enable == TRUE)
5303 stats_stop(service);
5305 service->roaming = roaming;
5308 if (stats_enable == TRUE)
5309 stats_start(service);
5311 roaming_changed(service);
5314 if (need_sort == TRUE) {
5315 iter = g_hash_table_lookup(service_hash, service->identifier);
5317 g_sequence_sort_changed(iter, service_compare, NULL);
5321 void __connman_service_remove_from_network(struct connman_network *network)
5323 struct connman_service *service;
5325 DBG("network %p", network);
5327 service = __connman_service_lookup_from_network(network);
5328 if (service == NULL)
5331 __connman_connection_gateway_remove(service,
5332 CONNMAN_IPCONFIG_TYPE_ALL);
5334 __connman_service_put(service);
5338 * __connman_service_create_from_provider:
5339 * @provider: provider structure
5341 * Look up service by provider and if not found, create one
5343 struct connman_service *
5344 __connman_service_create_from_provider(struct connman_provider *provider)
5346 struct connman_service *service;
5347 const char *ident, *str;
5349 int index = connman_provider_get_index(provider);
5351 DBG("provider %p", provider);
5353 ident = __connman_provider_get_ident(provider);
5357 name = g_strdup_printf("vpn_%s", ident);
5358 service = service_get(name);
5361 if (service == NULL)
5364 service->type = CONNMAN_SERVICE_TYPE_VPN;
5365 service->provider = connman_provider_ref(provider);
5366 service->autoconnect = FALSE;
5367 service->userconnect = TRUE;
5369 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5371 str = connman_provider_get_string(provider, "Name");
5373 g_free(service->name);
5374 service->name = g_strdup(str);
5375 service->hidden = FALSE;
5377 g_free(service->name);
5378 service->name = NULL;
5379 service->hidden = TRUE;
5382 service->strength = 0;
5384 if (service->ipconfig_ipv4 == NULL)
5385 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5387 if (service->ipconfig_ipv6 == NULL)
5388 setup_ip6config(service, index);
5390 service_register(service);
5392 __connman_notifier_service_add(service, service->name);
5397 int __connman_service_init(void)
5401 connection = connman_dbus_get_connection();
5403 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5406 service_list = g_sequence_new(service_free);
5411 void __connman_service_cleanup(void)
5417 list = service_list;
5418 service_list = NULL;
5419 g_sequence_free(list);
5421 g_hash_table_destroy(service_hash);
5422 service_hash = NULL;
5424 g_slist_free(counter_list);
5425 counter_list = NULL;
5427 dbus_connection_unref(connection);