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;
113 int online_check_count;
116 static void append_path(gpointer value, gpointer user_data)
118 struct connman_service *service = value;
119 DBusMessageIter *iter = user_data;
121 if (service->path == NULL)
124 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
128 void __connman_service_list(DBusMessageIter *iter, void *user_data)
130 if (service_list == NULL)
133 g_sequence_foreach(service_list, append_path, iter);
138 struct connman_service *service;
141 static void compare_path(gpointer value, gpointer user_data)
143 struct connman_service *service = value;
144 struct find_data *data = user_data;
146 if (data->service != NULL)
149 if (g_strcmp0(service->path, data->path) == 0)
150 data->service = service;
153 static struct connman_service *find_service(const char *path)
155 struct find_data data = { .path = path, .service = NULL };
157 DBG("path %s", path);
159 g_sequence_foreach(service_list, compare_path, &data);
164 const char *__connman_service_type2string(enum connman_service_type type)
167 case CONNMAN_SERVICE_TYPE_UNKNOWN:
169 case CONNMAN_SERVICE_TYPE_SYSTEM:
171 case CONNMAN_SERVICE_TYPE_ETHERNET:
173 case CONNMAN_SERVICE_TYPE_WIFI:
175 case CONNMAN_SERVICE_TYPE_WIMAX:
177 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
179 case CONNMAN_SERVICE_TYPE_CELLULAR:
181 case CONNMAN_SERVICE_TYPE_GPS:
183 case CONNMAN_SERVICE_TYPE_VPN:
185 case CONNMAN_SERVICE_TYPE_GADGET:
192 static const char *security2string(enum connman_service_security security)
195 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
197 case CONNMAN_SERVICE_SECURITY_NONE:
199 case CONNMAN_SERVICE_SECURITY_WEP:
201 case CONNMAN_SERVICE_SECURITY_PSK:
202 case CONNMAN_SERVICE_SECURITY_WPA:
203 case CONNMAN_SERVICE_SECURITY_RSN:
205 case CONNMAN_SERVICE_SECURITY_8021X:
212 static const char *state2string(enum connman_service_state state)
215 case CONNMAN_SERVICE_STATE_UNKNOWN:
217 case CONNMAN_SERVICE_STATE_IDLE:
219 case CONNMAN_SERVICE_STATE_ASSOCIATION:
220 return "association";
221 case CONNMAN_SERVICE_STATE_CONFIGURATION:
222 return "configuration";
223 case CONNMAN_SERVICE_STATE_READY:
225 case CONNMAN_SERVICE_STATE_ONLINE:
227 case CONNMAN_SERVICE_STATE_DISCONNECT:
229 case CONNMAN_SERVICE_STATE_FAILURE:
236 static const char *error2string(enum connman_service_error error)
239 case CONNMAN_SERVICE_ERROR_UNKNOWN:
241 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
242 return "out-of-range";
243 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
244 return "pin-missing";
245 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
246 return "dhcp-failed";
247 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
248 return "connect-failed";
249 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
250 return "login-failed";
251 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
252 return "auth-failed";
253 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
254 return "invalid-key";
260 static enum connman_service_error string2error(const char *error)
262 if (g_strcmp0(error, "dhcp-failed") == 0)
263 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
264 else if (g_strcmp0(error, "pin-missing") == 0)
265 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
266 else if (g_strcmp0(error, "invalid-key") == 0)
267 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
269 return CONNMAN_SERVICE_ERROR_UNKNOWN;
272 static const char *proxymethod2string(enum connman_service_proxy_method method)
275 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
277 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
279 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
281 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
288 static enum connman_service_proxy_method string2proxymethod(const char *method)
290 if (g_strcmp0(method, "direct") == 0)
291 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
292 else if (g_strcmp0(method, "auto") == 0)
293 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
294 else if (g_strcmp0(method, "manual") == 0)
295 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
297 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
300 static int service_load(struct connman_service *service)
303 GError *error = NULL;
306 connman_bool_t autoconnect;
307 unsigned int ssid_len;
310 DBG("service %p", service);
312 keyfile = connman_storage_load_service(service->identifier);
316 switch (service->type) {
317 case CONNMAN_SERVICE_TYPE_UNKNOWN:
318 case CONNMAN_SERVICE_TYPE_SYSTEM:
319 case CONNMAN_SERVICE_TYPE_ETHERNET:
320 case CONNMAN_SERVICE_TYPE_GPS:
321 case CONNMAN_SERVICE_TYPE_VPN:
322 case CONNMAN_SERVICE_TYPE_GADGET:
324 case CONNMAN_SERVICE_TYPE_WIFI:
325 if (service->name == NULL) {
328 name = g_key_file_get_string(keyfile,
329 service->identifier, "Name", NULL);
331 g_free(service->name);
332 service->name = name;
335 if (service->network != NULL)
336 connman_network_set_name(service->network,
340 if (service->network &&
341 connman_network_get_blob(service->network,
342 "WiFi.SSID", &ssid_len) == NULL) {
345 hex_ssid = g_key_file_get_string(keyfile,
349 if (hex_ssid != NULL) {
351 unsigned int i, j = 0, hex;
352 size_t hex_ssid_len = strlen(hex_ssid);
354 ssid = g_try_malloc0(hex_ssid_len / 2);
361 for (i = 0; i < hex_ssid_len; i += 2) {
362 sscanf(hex_ssid + i, "%02x", &hex);
366 connman_network_set_blob(service->network,
367 "WiFi.SSID", ssid, hex_ssid_len / 2);
374 case CONNMAN_SERVICE_TYPE_WIMAX:
375 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
376 case CONNMAN_SERVICE_TYPE_CELLULAR:
377 service->favorite = g_key_file_get_boolean(keyfile,
378 service->identifier, "Favorite", NULL);
380 autoconnect = g_key_file_get_boolean(keyfile,
381 service->identifier, "AutoConnect", &error);
383 service->autoconnect = autoconnect;
384 g_clear_error(&error);
386 str = g_key_file_get_string(keyfile,
387 service->identifier, "Failure", NULL);
389 if (service->favorite == FALSE)
390 service->state_ipv4 = service->state_ipv6 =
391 CONNMAN_SERVICE_STATE_FAILURE;
392 service->error = string2error(str);
398 str = g_key_file_get_string(keyfile,
399 service->identifier, "Modified", NULL);
401 g_time_val_from_iso8601(str, &service->modified);
405 str = g_key_file_get_string(keyfile,
406 service->identifier, "Passphrase", NULL);
408 g_free(service->passphrase);
409 service->passphrase = str;
412 if (service->ipconfig_ipv4 != NULL)
413 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
414 service->identifier, "IPv4.");
416 if (service->ipconfig_ipv6 != NULL)
417 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
418 service->identifier, "IPv6.");
420 service->nameservers_config = g_key_file_get_string_list(keyfile,
421 service->identifier, "Nameservers", &length, NULL);
422 if (service->nameservers_config != NULL && length == 0) {
423 g_strfreev(service->nameservers_config);
424 service->nameservers_config = NULL;
427 service->domains = g_key_file_get_string_list(keyfile,
428 service->identifier, "Domains", &length, NULL);
429 if (service->domains != NULL && length == 0) {
430 g_strfreev(service->domains);
431 service->domains = NULL;
434 str = g_key_file_get_string(keyfile,
435 service->identifier, "Proxy.Method", NULL);
437 service->proxy_config = string2proxymethod(str);
441 service->proxies = g_key_file_get_string_list(keyfile,
442 service->identifier, "Proxy.Servers", &length, NULL);
443 if (service->proxies != NULL && length == 0) {
444 g_strfreev(service->proxies);
445 service->proxies = NULL;
448 service->excludes = g_key_file_get_string_list(keyfile,
449 service->identifier, "Proxy.Excludes", &length, NULL);
450 if (service->excludes != NULL && length == 0) {
451 g_strfreev(service->excludes);
452 service->excludes = NULL;
455 str = g_key_file_get_string(keyfile,
456 service->identifier, "Proxy.URL", NULL);
458 g_free(service->pac);
463 g_key_file_free(keyfile);
468 static int service_save(struct connman_service *service)
473 const char *cst_str = NULL;
476 DBG("service %p", service);
478 keyfile = __connman_storage_open_service(service->identifier);
482 if (service->name != NULL)
483 g_key_file_set_string(keyfile, service->identifier,
484 "Name", service->name);
486 switch (service->type) {
487 case CONNMAN_SERVICE_TYPE_UNKNOWN:
488 case CONNMAN_SERVICE_TYPE_SYSTEM:
489 case CONNMAN_SERVICE_TYPE_ETHERNET:
490 case CONNMAN_SERVICE_TYPE_GPS:
491 case CONNMAN_SERVICE_TYPE_VPN:
492 case CONNMAN_SERVICE_TYPE_GADGET:
494 case CONNMAN_SERVICE_TYPE_WIFI:
495 if (service->network) {
496 const unsigned char *ssid;
497 unsigned int ssid_len = 0;
499 ssid = connman_network_get_blob(service->network,
500 "WiFi.SSID", &ssid_len);
502 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
503 char *identifier = service->identifier;
507 str = g_string_sized_new(ssid_len * 2);
513 for (i = 0; i < ssid_len; i++)
514 g_string_append_printf(str,
517 g_key_file_set_string(keyfile, identifier,
520 g_string_free(str, TRUE);
523 freq = connman_network_get_frequency(service->network);
524 g_key_file_set_integer(keyfile, service->identifier,
529 case CONNMAN_SERVICE_TYPE_WIMAX:
530 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
531 case CONNMAN_SERVICE_TYPE_CELLULAR:
532 g_key_file_set_boolean(keyfile, service->identifier,
533 "Favorite", service->favorite);
535 if (service->favorite == TRUE)
536 g_key_file_set_boolean(keyfile, service->identifier,
537 "AutoConnect", service->autoconnect);
539 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
540 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
541 const char *failure = error2string(service->error);
543 g_key_file_set_string(keyfile,
547 g_key_file_remove_key(keyfile, service->identifier,
553 str = g_time_val_to_iso8601(&service->modified);
555 g_key_file_set_string(keyfile, service->identifier,
560 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
561 g_key_file_set_string(keyfile, service->identifier,
562 "Passphrase", service->passphrase);
564 g_key_file_remove_key(keyfile, service->identifier,
567 if (service->ipconfig_ipv4 != NULL)
568 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
569 service->identifier, "IPv4.");
571 if (service->ipconfig_ipv6 != NULL)
572 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
573 service->identifier, "IPv6.");
575 if (service->nameservers_config != NULL) {
576 guint len = g_strv_length(service->nameservers_config);
578 g_key_file_set_string_list(keyfile, service->identifier,
580 (const gchar **) service->nameservers_config, len);
582 g_key_file_remove_key(keyfile, service->identifier,
583 "Nameservers", NULL);
585 if (service->domains != NULL) {
586 guint len = g_strv_length(service->domains);
588 g_key_file_set_string_list(keyfile, service->identifier,
590 (const gchar **) service->domains, len);
592 g_key_file_remove_key(keyfile, service->identifier,
595 cst_str = proxymethod2string(service->proxy_config);
597 g_key_file_set_string(keyfile, service->identifier,
598 "Proxy.Method", cst_str);
600 if (service->proxies != NULL) {
601 guint len = g_strv_length(service->proxies);
603 g_key_file_set_string_list(keyfile, service->identifier,
605 (const gchar **) service->proxies, len);
607 g_key_file_remove_key(keyfile, service->identifier,
608 "Proxy.Servers", NULL);
610 if (service->excludes != NULL) {
611 guint len = g_strv_length(service->excludes);
613 g_key_file_set_string_list(keyfile, service->identifier,
615 (const gchar **) service->excludes, len);
617 g_key_file_remove_key(keyfile, service->identifier,
618 "Proxy.Excludes", NULL);
620 if (service->pac != NULL && strlen(service->pac) > 0)
621 g_key_file_set_string(keyfile, service->identifier,
622 "Proxy.URL", service->pac);
624 g_key_file_remove_key(keyfile, service->identifier,
628 __connman_storage_save_service(keyfile, service->identifier);
630 g_key_file_free(keyfile);
635 static guint changed_timeout = 0;
637 static gboolean notify_services_changed(gpointer user_data)
641 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
642 CONNMAN_MANAGER_INTERFACE, "Services",
643 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
649 static void services_changed(gboolean delayed)
653 if (changed_timeout > 0) {
654 g_source_remove(changed_timeout);
658 if (__connman_connection_update_gateway() == TRUE) {
659 notify_services_changed(NULL);
663 if (delayed == FALSE) {
664 notify_services_changed(NULL);
668 changed_timeout = g_timeout_add_seconds(1, notify_services_changed,
672 static enum connman_service_state combine_state(
673 enum connman_service_state state_a,
674 enum connman_service_state state_b)
676 enum connman_service_state result;
678 if (state_a == state_b) {
683 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
688 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
693 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
698 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
703 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
708 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
713 if (state_a == CONNMAN_SERVICE_STATE_READY) {
718 if (state_b == CONNMAN_SERVICE_STATE_READY) {
723 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
728 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
733 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
738 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
743 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
748 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
753 result = CONNMAN_SERVICE_STATE_FAILURE;
759 static connman_bool_t is_connecting_state(struct connman_service *service,
760 enum connman_service_state state)
763 case CONNMAN_SERVICE_STATE_UNKNOWN:
764 case CONNMAN_SERVICE_STATE_IDLE:
765 case CONNMAN_SERVICE_STATE_FAILURE:
766 if (service->network != NULL)
767 return connman_network_get_connecting(service->network);
768 case CONNMAN_SERVICE_STATE_DISCONNECT:
769 case CONNMAN_SERVICE_STATE_READY:
770 case CONNMAN_SERVICE_STATE_ONLINE:
772 case CONNMAN_SERVICE_STATE_ASSOCIATION:
773 case CONNMAN_SERVICE_STATE_CONFIGURATION:
780 static connman_bool_t is_connected_state(const struct connman_service *service,
781 enum connman_service_state state)
784 case CONNMAN_SERVICE_STATE_UNKNOWN:
785 case CONNMAN_SERVICE_STATE_IDLE:
786 case CONNMAN_SERVICE_STATE_ASSOCIATION:
787 case CONNMAN_SERVICE_STATE_CONFIGURATION:
788 case CONNMAN_SERVICE_STATE_DISCONNECT:
789 case CONNMAN_SERVICE_STATE_FAILURE:
791 case CONNMAN_SERVICE_STATE_READY:
792 case CONNMAN_SERVICE_STATE_ONLINE:
799 static connman_bool_t is_connecting(struct connman_service *service)
801 return is_connecting_state(service, service->state);
804 static connman_bool_t is_connected(struct connman_service *service)
806 return is_connected_state(service, service->state);
809 static void update_nameservers(struct connman_service *service)
813 if (service->ipconfig_ipv4)
814 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
815 else if (service->ipconfig_ipv6)
816 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
823 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
824 case CONNMAN_SERVICE_STATE_UNKNOWN:
825 case CONNMAN_SERVICE_STATE_IDLE:
826 case CONNMAN_SERVICE_STATE_ASSOCIATION:
827 case CONNMAN_SERVICE_STATE_CONFIGURATION:
829 case CONNMAN_SERVICE_STATE_FAILURE:
830 case CONNMAN_SERVICE_STATE_DISCONNECT:
831 connman_resolver_remove_all(ifname);
833 case CONNMAN_SERVICE_STATE_READY:
834 case CONNMAN_SERVICE_STATE_ONLINE:
838 if (service->nameservers_config != NULL) {
841 for (i = 0; service->nameservers_config[i] != NULL; i++) {
842 connman_resolver_append(ifname, NULL,
843 service->nameservers_config[i]);
845 } else if (service->nameservers != NULL) {
848 for (i = 0; service->nameservers[i] != NULL; i++) {
849 connman_resolver_append(ifname, NULL,
850 service->nameservers[i]);
854 if (service->domains != NULL) {
857 for (i = 0; service->domains[i]; i++)
858 connman_resolver_append(ifname, service->domains[i],
860 } else if (service->domainname != NULL)
861 connman_resolver_append(ifname, service->domainname, NULL);
863 connman_resolver_flush();
867 * The is_auto variable is set to true when IPv6 autoconf nameservers are
868 * inserted to resolver via netlink message (see rtnl.c:rtnl_newnduseropt()
869 * for details) and not through service.c
871 int __connman_service_nameserver_append(struct connman_service *service,
872 const char *nameserver, gboolean is_auto)
877 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
879 if (nameserver == NULL)
883 nameservers = service->nameservers_auto;
885 nameservers = service->nameservers;
887 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
888 if (g_strcmp0(nameservers[i], nameserver) == 0)
891 if (nameservers != NULL) {
892 len = g_strv_length(nameservers);
893 nameservers = g_try_renew(char *, nameservers, len + 2);
896 nameservers = g_try_new0(char *, len + 2);
899 if (nameservers == NULL)
902 nameservers[len] = g_strdup(nameserver);
903 if (nameservers[len] == NULL)
906 nameservers[len + 1] = NULL;
908 if (is_auto == TRUE) {
909 service->nameservers_auto = nameservers;
911 service->nameservers = nameservers;
912 update_nameservers(service);
918 int __connman_service_nameserver_remove(struct connman_service *service,
919 const char *nameserver, gboolean is_auto)
921 char **servers, **nameservers;
922 gboolean found = FALSE;
925 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
927 if (nameserver == NULL)
931 nameservers = service->nameservers_auto;
933 nameservers = service->nameservers;
935 if (nameservers == NULL)
938 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
939 if (g_strcmp0(nameservers[i], nameserver) == 0) {
947 len = g_strv_length(nameservers);
950 g_strfreev(nameservers);
952 service->nameservers_auto = NULL;
954 service->nameservers = NULL;
959 servers = g_try_new0(char *, len);
963 for (i = 0, j = 0; i < len; i++) {
964 if (g_strcmp0(nameservers[i], nameserver) != 0) {
965 servers[j] = g_strdup(nameservers[i]);
966 if (servers[j] == NULL)
971 servers[len - 1] = NULL;
973 g_strfreev(nameservers);
974 nameservers = servers;
976 if (is_auto == TRUE) {
977 service->nameservers_auto = nameservers;
979 service->nameservers = nameservers;
980 update_nameservers(service);
986 void __connman_service_nameserver_clear(struct connman_service *service)
988 g_strfreev(service->nameservers);
989 service->nameservers = NULL;
991 update_nameservers(service);
994 static void nameserver_add_routes(int index, char **nameservers,
998 struct addrinfo hints;
999 struct addrinfo *addr;
1001 for (i = 0; nameservers[i] != NULL; i++) {
1002 memset(&hints, 0, sizeof(struct addrinfo));
1003 hints.ai_flags = AI_NUMERICHOST;
1006 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1007 if (ret == EAI_NONAME)
1008 family = AF_INET; /* use the IPv4 as a default */
1012 family = addr->ai_family;
1014 if (family == AF_INET) {
1015 if (connman_inet_compare_subnet(index,
1016 nameservers[i]) != TRUE)
1017 connman_inet_add_host_route(index,
1018 nameservers[i], gw);
1019 } else if (family == AF_INET6)
1020 connman_inet_add_ipv6_host_route(index,
1021 nameservers[i], gw);
1027 static void nameserver_del_routes(int index, char **nameservers)
1030 struct addrinfo hints;
1031 struct addrinfo *addr;
1033 for (i = 0; nameservers[i] != NULL; i++) {
1034 memset(&hints, 0, sizeof(struct addrinfo));
1035 hints.ai_flags = AI_NUMERICHOST;
1038 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1039 if (ret == EAI_NONAME)
1040 family = AF_INET; /* use the IPv4 as a default */
1044 family = addr->ai_family;
1046 if (family == AF_INET)
1047 connman_inet_del_host_route(index, nameservers[i]);
1048 else if (family == AF_INET6)
1049 connman_inet_del_ipv6_host_route(index,
1056 void __connman_service_nameserver_add_routes(struct connman_service *service,
1061 if (service == NULL)
1064 if (service->network != NULL)
1065 index = connman_network_get_index(service->network);
1066 else if (service->provider != NULL)
1067 index = connman_provider_get_index(service->provider);
1069 if (service->nameservers_config != NULL) {
1071 * Configured nameserver takes preference over the
1072 * discoverd nameserver gathered from DHCP, VPN, etc.
1074 nameserver_add_routes(index, service->nameservers_config, gw);
1075 } else if (service->nameservers != NULL) {
1077 * We add nameservers host routes for nameservers that
1078 * are not on our subnet. For those who are, the subnet
1079 * route will be installed by the time the dns proxy code
1080 * tries to reach them. The subnet route is installed
1081 * when setting the interface IP address.
1083 nameserver_add_routes(index, service->nameservers, gw);
1087 void __connman_service_nameserver_del_routes(struct connman_service *service)
1091 if (service == NULL)
1094 if (service->network != NULL)
1095 index = connman_network_get_index(service->network);
1096 else if (service->provider != NULL)
1097 index = connman_provider_get_index(service->provider);
1099 if (service->nameservers_config != NULL)
1100 nameserver_del_routes(index, service->nameservers_config);
1101 else if (service->nameservers != NULL)
1102 nameserver_del_routes(index, service->nameservers);
1105 static struct connman_stats *stats_get(struct connman_service *service)
1107 if (service->roaming == TRUE)
1108 return &service->stats_roaming;
1110 return &service->stats;
1113 static connman_bool_t stats_enabled(struct connman_service *service)
1115 struct connman_stats *stats = stats_get(service);
1117 return stats->enabled;
1120 static void stats_start(struct connman_service *service)
1122 struct connman_stats *stats = stats_get(service);
1124 DBG("service %p", service);
1126 if (stats->timer == NULL)
1129 stats->enabled = TRUE;
1130 stats->data_last.time = stats->data.time;
1132 g_timer_start(stats->timer);
1135 static void stats_stop(struct connman_service *service)
1137 struct connman_stats *stats = stats_get(service);
1138 unsigned int seconds;
1140 DBG("service %p", service);
1142 if (stats->timer == NULL)
1145 if (stats->enabled == FALSE)
1148 g_timer_stop(stats->timer);
1150 seconds = g_timer_elapsed(stats->timer, NULL);
1151 stats->data.time = stats->data_last.time + seconds;
1153 stats->enabled = FALSE;
1156 static void reset_stats(struct connman_service *service)
1158 DBG("service %p", service);
1161 service->stats.valid = FALSE;
1163 service->stats.data.rx_packets = 0;
1164 service->stats.data.tx_packets = 0;
1165 service->stats.data.rx_bytes = 0;
1166 service->stats.data.tx_bytes = 0;
1167 service->stats.data.rx_errors = 0;
1168 service->stats.data.tx_errors = 0;
1169 service->stats.data.rx_dropped = 0;
1170 service->stats.data.tx_dropped = 0;
1171 service->stats.data.time = 0;
1172 service->stats.data_last.time = 0;
1174 g_timer_reset(service->stats.timer);
1177 service->stats_roaming.valid = FALSE;
1179 service->stats_roaming.data.rx_packets = 0;
1180 service->stats_roaming.data.tx_packets = 0;
1181 service->stats_roaming.data.rx_bytes = 0;
1182 service->stats_roaming.data.tx_bytes = 0;
1183 service->stats_roaming.data.rx_errors = 0;
1184 service->stats_roaming.data.tx_errors = 0;
1185 service->stats_roaming.data.rx_dropped = 0;
1186 service->stats_roaming.data.tx_dropped = 0;
1187 service->stats_roaming.data.time = 0;
1188 service->stats_roaming.data_last.time = 0;
1190 g_timer_reset(service->stats_roaming.timer);
1193 static struct connman_service *get_default(void)
1195 struct connman_service *service;
1196 GSequenceIter *iter;
1198 iter = g_sequence_get_begin_iter(service_list);
1200 if (g_sequence_iter_is_end(iter) == TRUE)
1203 service = g_sequence_get(iter);
1205 if (is_connected(service) == FALSE)
1211 static void default_changed(void)
1213 struct connman_service *service = get_default();
1215 __connman_notifier_default_changed(service);
1218 static void state_changed(struct connman_service *service)
1222 __connman_notifier_service_state_changed(service, service->state);
1224 str = state2string(service->state);
1228 connman_dbus_property_changed_basic(service->path,
1229 CONNMAN_SERVICE_INTERFACE, "State",
1230 DBUS_TYPE_STRING, &str);
1233 static void strength_changed(struct connman_service *service)
1235 if (service->strength == 0)
1238 connman_dbus_property_changed_basic(service->path,
1239 CONNMAN_SERVICE_INTERFACE, "Strength",
1240 DBUS_TYPE_BYTE, &service->strength);
1243 static void favorite_changed(struct connman_service *service)
1245 if (service->path == NULL)
1248 connman_dbus_property_changed_basic(service->path,
1249 CONNMAN_SERVICE_INTERFACE, "Favorite",
1250 DBUS_TYPE_BOOLEAN, &service->favorite);
1253 static void immutable_changed(struct connman_service *service)
1255 if (service->path == NULL)
1258 connman_dbus_property_changed_basic(service->path,
1259 CONNMAN_SERVICE_INTERFACE, "Immutable",
1260 DBUS_TYPE_BOOLEAN, &service->immutable);
1263 static void roaming_changed(struct connman_service *service)
1265 if (service->path == NULL)
1268 connman_dbus_property_changed_basic(service->path,
1269 CONNMAN_SERVICE_INTERFACE, "Roaming",
1270 DBUS_TYPE_BOOLEAN, &service->roaming);
1273 static void autoconnect_changed(struct connman_service *service)
1275 if (service->path == NULL)
1278 connman_dbus_property_changed_basic(service->path,
1279 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
1280 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1283 static void append_security(DBusMessageIter *iter, void *user_data)
1285 struct connman_service *service = user_data;
1288 str = security2string(service->security);
1290 dbus_message_iter_append_basic(iter,
1291 DBUS_TYPE_STRING, &str);
1294 * Some access points incorrectly advertise WPS even when they
1295 * are configured as open or no security, so filter
1298 if (service->wps == TRUE) {
1299 switch (service->security) {
1300 case CONNMAN_SERVICE_SECURITY_PSK:
1301 case CONNMAN_SERVICE_SECURITY_WPA:
1302 case CONNMAN_SERVICE_SECURITY_RSN:
1304 dbus_message_iter_append_basic(iter,
1305 DBUS_TYPE_STRING, &str);
1307 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1308 case CONNMAN_SERVICE_SECURITY_NONE:
1309 case CONNMAN_SERVICE_SECURITY_WEP:
1310 case CONNMAN_SERVICE_SECURITY_8021X:
1316 static void append_ethernet(DBusMessageIter *iter, void *user_data)
1318 struct connman_service *service = user_data;
1320 if (service->ipconfig_ipv4 != NULL)
1321 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
1323 else if (service->ipconfig_ipv6 != NULL)
1324 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
1328 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1330 struct connman_service *service = user_data;
1332 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1333 state2string(service->state_ipv4));
1335 if (is_connected_state(service, service->state_ipv4) == FALSE)
1338 if (service->ipconfig_ipv4 != NULL)
1339 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1342 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1344 struct connman_service *service = user_data;
1346 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1347 state2string(service->state_ipv6));
1349 if (is_connected_state(service, service->state_ipv6) == FALSE)
1352 if (service->ipconfig_ipv6 != NULL)
1353 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1354 service->ipconfig_ipv4);
1357 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1359 struct connman_service *service = user_data;
1361 if (service->ipconfig_ipv4 != NULL)
1362 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1366 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1368 struct connman_service *service = user_data;
1370 if (service->ipconfig_ipv6 != NULL)
1371 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1375 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1380 servers = *nameservers;
1382 for (i = 0; servers[i] != NULL; i++) {
1383 dbus_message_iter_append_basic(iter,
1384 DBUS_TYPE_STRING, &servers[i]);
1388 static void append_dns(DBusMessageIter *iter, void *user_data)
1390 struct connman_service *service = user_data;
1392 if (is_connected(service) == FALSE)
1395 if (service->nameservers_config != NULL) {
1396 append_nameserver(iter, &service->nameservers_config);
1399 if (service->nameservers != NULL)
1400 append_nameserver(iter, &service->nameservers);
1402 if (service->nameservers_auto != NULL)
1403 append_nameserver(iter, &service->nameservers_auto);
1407 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1409 struct connman_service *service = user_data;
1412 if (service->nameservers_config == NULL)
1415 for (i = 0; service->nameservers_config[i]; i++) {
1416 dbus_message_iter_append_basic(iter,
1418 &service->nameservers_config[i]);
1422 static void append_domain(DBusMessageIter *iter, void *user_data)
1424 struct connman_service *service = user_data;
1426 if (is_connected(service) == FALSE &&
1427 is_connecting(service) == FALSE)
1430 if (service->domainname == NULL)
1433 dbus_message_iter_append_basic(iter,
1434 DBUS_TYPE_STRING, &service->domainname);
1437 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1439 struct connman_service *service = user_data;
1442 if (service->domains == NULL)
1445 for (i = 0; service->domains[i]; i++)
1446 dbus_message_iter_append_basic(iter,
1447 DBUS_TYPE_STRING, &service->domains[i]);
1450 static void append_proxies(DBusMessageIter *iter, void *user_data)
1452 struct connman_service *service = user_data;
1455 if (service->proxies == NULL)
1458 for (i = 0; service->proxies[i]; i++)
1459 dbus_message_iter_append_basic(iter,
1460 DBUS_TYPE_STRING, &service->proxies[i]);
1463 static void append_excludes(DBusMessageIter *iter, void *user_data)
1465 struct connman_service *service = user_data;
1468 if (service->excludes == NULL)
1471 for (i = 0; service->excludes[i]; i++)
1472 dbus_message_iter_append_basic(iter,
1473 DBUS_TYPE_STRING, &service->excludes[i]);
1476 static void append_proxy(DBusMessageIter *iter, void *user_data)
1478 struct connman_service *service = user_data;
1479 enum connman_service_proxy_method proxy;
1480 const char *pac = NULL;
1481 const char *method = proxymethod2string(
1482 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1486 if (is_connected(service) == FALSE)
1489 proxy = connman_service_get_proxy_method(service);
1492 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1494 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1496 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1497 connman_dbus_dict_append_array(iter, "Servers",
1498 DBUS_TYPE_STRING, append_proxies,
1501 connman_dbus_dict_append_array(iter, "Excludes",
1502 DBUS_TYPE_STRING, append_excludes,
1505 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1506 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1507 if (service->ipconfig_ipv4 != NULL)
1508 pac = __connman_ipconfig_get_proxy_autoconfig(
1509 service->ipconfig_ipv4);
1510 else if (service->ipconfig_ipv6 != NULL)
1511 pac = __connman_ipconfig_get_proxy_autoconfig(
1512 service->ipconfig_ipv6);
1514 if (service->pac == NULL && pac == NULL)
1517 if (service->pac != NULL)
1520 connman_dbus_dict_append_basic(iter, "URL",
1521 DBUS_TYPE_STRING, &pac);
1525 method = proxymethod2string(proxy);
1528 connman_dbus_dict_append_basic(iter, "Method",
1529 DBUS_TYPE_STRING, &method);
1532 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1534 struct connman_service *service = user_data;
1537 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1540 switch (service->proxy_config) {
1541 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1543 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1545 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1546 if (service->proxies != NULL)
1547 connman_dbus_dict_append_array(iter, "Servers",
1549 append_proxies, service);
1551 if (service->excludes != NULL)
1552 connman_dbus_dict_append_array(iter, "Excludes",
1554 append_excludes, service);
1556 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1557 if (service->pac != NULL)
1558 connman_dbus_dict_append_basic(iter, "URL",
1559 DBUS_TYPE_STRING, &service->pac);
1563 method = proxymethod2string(service->proxy_config);
1565 connman_dbus_dict_append_basic(iter, "Method",
1566 DBUS_TYPE_STRING, &method);
1569 static void append_provider(DBusMessageIter *iter, void *user_data)
1571 struct connman_service *service = user_data;
1573 DBG("%p %p", service, service->provider);
1575 if (is_connected(service) == FALSE)
1578 if (service->provider != NULL)
1579 __connman_provider_append_properties(service->provider, iter);
1583 static void settings_changed(struct connman_service *service,
1584 struct connman_ipconfig *ipconfig)
1586 connman_dbus_property_changed_dict(service->path,
1587 CONNMAN_SERVICE_INTERFACE, "IPv4",
1588 append_ipv4, service);
1590 connman_dbus_property_changed_dict(service->path,
1591 CONNMAN_SERVICE_INTERFACE, "IPv6",
1592 append_ipv6, service);
1594 __connman_notifier_ipconfig_changed(service, ipconfig);
1597 static void ipv4_configuration_changed(struct connman_service *service)
1599 connman_dbus_property_changed_dict(service->path,
1600 CONNMAN_SERVICE_INTERFACE,
1601 "IPv4.Configuration",
1606 static void ipv6_configuration_changed(struct connman_service *service)
1608 connman_dbus_property_changed_dict(service->path,
1609 CONNMAN_SERVICE_INTERFACE,
1610 "IPv6.Configuration",
1615 static void dns_changed(struct connman_service *service)
1617 connman_dbus_property_changed_array(service->path,
1618 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1619 DBUS_TYPE_STRING, append_dns, service);
1622 static void dns_configuration_changed(struct connman_service *service)
1624 connman_dbus_property_changed_array(service->path,
1625 CONNMAN_SERVICE_INTERFACE,
1626 "Nameservers.Configuration",
1627 DBUS_TYPE_STRING, append_dnsconfig, service);
1629 dns_changed(service);
1632 static void domain_changed(struct connman_service *service)
1634 connman_dbus_property_changed_array(service->path,
1635 CONNMAN_SERVICE_INTERFACE, "Domains",
1636 DBUS_TYPE_STRING, append_domain, service);
1639 static void domain_configuration_changed(struct connman_service *service)
1641 connman_dbus_property_changed_array(service->path,
1642 CONNMAN_SERVICE_INTERFACE,
1643 "Domains.Configuration",
1644 DBUS_TYPE_STRING, append_domainconfig, service);
1647 static void proxy_changed(struct connman_service *service)
1649 connman_dbus_property_changed_dict(service->path,
1650 CONNMAN_SERVICE_INTERFACE, "Proxy",
1651 append_proxy, service);
1654 static void proxy_configuration_changed(struct connman_service *service)
1656 connman_dbus_property_changed_dict(service->path,
1657 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1658 append_proxyconfig, service);
1660 proxy_changed(service);
1663 static void link_changed(struct connman_service *service)
1665 connman_dbus_property_changed_dict(service->path,
1666 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1667 append_ethernet, service);
1670 static void stats_append_counters(DBusMessageIter *dict,
1671 struct connman_stats_data *stats,
1672 struct connman_stats_data *counters,
1673 connman_bool_t append_all)
1675 if (counters->rx_packets != stats->rx_packets || append_all) {
1676 counters->rx_packets = stats->rx_packets;
1677 connman_dbus_dict_append_basic(dict, "RX.Packets",
1678 DBUS_TYPE_UINT32, &stats->rx_packets);
1681 if (counters->tx_packets != stats->tx_packets || append_all) {
1682 counters->tx_packets = stats->tx_packets;
1683 connman_dbus_dict_append_basic(dict, "TX.Packets",
1684 DBUS_TYPE_UINT32, &stats->tx_packets);
1687 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1688 counters->rx_bytes = stats->rx_bytes;
1689 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1690 DBUS_TYPE_UINT32, &stats->rx_bytes);
1693 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1694 counters->tx_bytes = stats->tx_bytes;
1695 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1696 DBUS_TYPE_UINT32, &stats->tx_bytes);
1699 if (counters->rx_errors != stats->rx_errors || append_all) {
1700 counters->rx_errors = stats->rx_errors;
1701 connman_dbus_dict_append_basic(dict, "RX.Errors",
1702 DBUS_TYPE_UINT32, &stats->rx_errors);
1705 if (counters->tx_errors != stats->tx_errors || append_all) {
1706 counters->tx_errors = stats->tx_errors;
1707 connman_dbus_dict_append_basic(dict, "TX.Errors",
1708 DBUS_TYPE_UINT32, &stats->tx_errors);
1711 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1712 counters->rx_dropped = stats->rx_dropped;
1713 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1714 DBUS_TYPE_UINT32, &stats->rx_dropped);
1717 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1718 counters->tx_dropped = stats->tx_dropped;
1719 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1720 DBUS_TYPE_UINT32, &stats->tx_dropped);
1723 if (counters->time != stats->time || append_all) {
1724 counters->time = stats->time;
1725 connman_dbus_dict_append_basic(dict, "Time",
1726 DBUS_TYPE_UINT32, &stats->time);
1730 static void stats_append(struct connman_service *service,
1731 const char *counter,
1732 struct connman_stats_counter *counters,
1733 connman_bool_t append_all)
1735 DBusMessageIter array, dict;
1738 DBG("service %p counter %s", service, counter);
1740 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1744 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1745 &service->path, DBUS_TYPE_INVALID);
1747 dbus_message_iter_init_append(msg, &array);
1750 connman_dbus_dict_open(&array, &dict);
1752 stats_append_counters(&dict, &service->stats.data,
1753 &counters->stats.data, append_all);
1755 connman_dbus_dict_close(&array, &dict);
1757 /* roaming counter */
1758 connman_dbus_dict_open(&array, &dict);
1760 stats_append_counters(&dict, &service->stats_roaming.data,
1761 &counters->stats_roaming.data, append_all);
1763 connman_dbus_dict_close(&array, &dict);
1765 __connman_counter_send_usage(counter, msg);
1768 static void stats_update(struct connman_service *service,
1769 unsigned int rx_packets, unsigned int tx_packets,
1770 unsigned int rx_bytes, unsigned int tx_bytes,
1771 unsigned int rx_errors, unsigned int tx_errors,
1772 unsigned int rx_dropped, unsigned int tx_dropped)
1774 struct connman_stats *stats = stats_get(service);
1775 struct connman_stats_data *data_last = &stats->data_last;
1776 struct connman_stats_data *data = &stats->data;
1777 unsigned int seconds;
1779 DBG("service %p", service);
1781 if (stats->valid == TRUE) {
1783 rx_packets - data_last->rx_packets;
1785 tx_packets - data_last->tx_packets;
1787 rx_bytes - data_last->rx_bytes;
1789 tx_bytes - data_last->tx_bytes;
1791 rx_errors - data_last->rx_errors;
1793 tx_errors - data_last->tx_errors;
1795 rx_dropped - data_last->rx_dropped;
1797 tx_dropped - data_last->tx_dropped;
1799 stats->valid = TRUE;
1802 data_last->rx_packets = rx_packets;
1803 data_last->tx_packets = tx_packets;
1804 data_last->rx_bytes = rx_bytes;
1805 data_last->tx_bytes = tx_bytes;
1806 data_last->rx_errors = rx_errors;
1807 data_last->tx_errors = tx_errors;
1808 data_last->rx_dropped = rx_dropped;
1809 data_last->tx_dropped = tx_dropped;
1811 seconds = g_timer_elapsed(stats->timer, NULL);
1812 stats->data.time = stats->data_last.time + seconds;
1815 void __connman_service_notify(struct connman_service *service,
1816 unsigned int rx_packets, unsigned int tx_packets,
1817 unsigned int rx_bytes, unsigned int tx_bytes,
1818 unsigned int rx_errors, unsigned int tx_errors,
1819 unsigned int rx_dropped, unsigned int tx_dropped)
1821 GHashTableIter iter;
1822 gpointer key, value;
1823 const char *counter;
1824 struct connman_stats_counter *counters;
1825 struct connman_stats_data *data;
1828 if (service == NULL)
1831 if (is_connected(service) == FALSE)
1834 stats_update(service,
1835 rx_packets, tx_packets,
1837 rx_errors, tx_errors,
1838 rx_dropped, tx_dropped);
1840 data = &stats_get(service)->data;
1841 err = __connman_stats_update(service, service->roaming, data);
1843 connman_error("Failed to store statistics for %s",
1844 service->identifier);
1846 g_hash_table_iter_init(&iter, service->counter_table);
1847 while (g_hash_table_iter_next(&iter, &key, &value)) {
1851 stats_append(service, counter, counters, counters->append_all);
1852 counters->append_all = FALSE;
1856 int __connman_service_counter_register(const char *counter)
1858 struct connman_service *service;
1859 GSequenceIter *iter;
1860 struct connman_stats_counter *counters;
1862 DBG("counter %s", counter);
1864 counter_list = g_slist_append(counter_list, (gpointer)counter);
1866 iter = g_sequence_get_begin_iter(service_list);
1868 while (g_sequence_iter_is_end(iter) == FALSE) {
1869 service = g_sequence_get(iter);
1871 counters = g_try_new0(struct connman_stats_counter, 1);
1872 if (counters == NULL)
1875 counters->append_all = TRUE;
1877 g_hash_table_replace(service->counter_table, (gpointer)counter,
1880 iter = g_sequence_iter_next(iter);
1886 void __connman_service_counter_unregister(const char *counter)
1888 struct connman_service *service;
1889 GSequenceIter *iter;
1891 DBG("counter %s", counter);
1893 iter = g_sequence_get_begin_iter(service_list);
1895 while (g_sequence_iter_is_end(iter) == FALSE) {
1896 service = g_sequence_get(iter);
1898 g_hash_table_remove(service->counter_table, counter);
1900 iter = g_sequence_iter_next(iter);
1903 counter_list = g_slist_remove(counter_list, counter);
1906 GSequence *__connman_service_get_list(struct connman_session *session,
1907 service_match_cb service_match,
1908 create_service_entry_cb create_service_entry,
1909 GDestroyNotify destroy_service_entry)
1912 GSequenceIter *iter;
1913 struct connman_service *service;
1914 struct service_entry *entry;
1916 list = g_sequence_new(destroy_service_entry);
1920 iter = g_sequence_get_begin_iter(service_list);
1922 while (g_sequence_iter_is_end(iter) == FALSE) {
1923 service = g_sequence_get(iter);
1925 if (service_match(session, service) == TRUE) {
1926 entry = create_service_entry(service, service->name,
1931 g_sequence_append(list, entry);
1934 iter = g_sequence_iter_next(iter);
1940 void __connman_service_session_inc(struct connman_service *service)
1942 DBG("service %p ref count %d", service,
1943 service->session_usage_count + 1);
1945 __sync_fetch_and_add(&service->session_usage_count, 1);
1948 connman_bool_t __connman_service_session_dec(struct connman_service *service)
1950 DBG("service %p ref count %d", service,
1951 service->session_usage_count - 1);
1953 if (__sync_fetch_and_sub(&service->session_usage_count, 1) != 1)
1959 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1960 struct connman_service *service)
1964 str = __connman_service_type2string(service->type);
1966 connman_dbus_dict_append_basic(dict, "Type",
1967 DBUS_TYPE_STRING, &str);
1969 connman_dbus_dict_append_array(dict, "Security",
1970 DBUS_TYPE_STRING, append_security, service);
1972 str = state2string(service->state);
1974 connman_dbus_dict_append_basic(dict, "State",
1975 DBUS_TYPE_STRING, &str);
1977 str = error2string(service->error);
1979 connman_dbus_dict_append_basic(dict, "Error",
1980 DBUS_TYPE_STRING, &str);
1982 if (service->strength > 0)
1983 connman_dbus_dict_append_basic(dict, "Strength",
1984 DBUS_TYPE_BYTE, &service->strength);
1986 connman_dbus_dict_append_basic(dict, "Favorite",
1987 DBUS_TYPE_BOOLEAN, &service->favorite);
1989 connman_dbus_dict_append_basic(dict, "Immutable",
1990 DBUS_TYPE_BOOLEAN, &service->immutable);
1992 if (service->favorite == TRUE)
1993 connman_dbus_dict_append_basic(dict, "AutoConnect",
1994 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1996 connman_dbus_dict_append_basic(dict, "AutoConnect",
1997 DBUS_TYPE_BOOLEAN, &service->favorite);
1999 if (service->name != NULL)
2000 connman_dbus_dict_append_basic(dict, "Name",
2001 DBUS_TYPE_STRING, &service->name);
2003 switch (service->type) {
2004 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2005 case CONNMAN_SERVICE_TYPE_SYSTEM:
2006 case CONNMAN_SERVICE_TYPE_GPS:
2007 case CONNMAN_SERVICE_TYPE_VPN:
2008 case CONNMAN_SERVICE_TYPE_GADGET:
2010 case CONNMAN_SERVICE_TYPE_CELLULAR:
2011 connman_dbus_dict_append_basic(dict, "Roaming",
2012 DBUS_TYPE_BOOLEAN, &service->roaming);
2014 connman_dbus_dict_append_dict(dict, "Ethernet",
2015 append_ethernet, service);
2017 case CONNMAN_SERVICE_TYPE_WIFI:
2018 case CONNMAN_SERVICE_TYPE_ETHERNET:
2019 case CONNMAN_SERVICE_TYPE_WIMAX:
2020 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2021 connman_dbus_dict_append_dict(dict, "Ethernet",
2022 append_ethernet, service);
2026 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
2028 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
2029 append_ipv4config, service);
2031 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
2033 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
2034 append_ipv6config, service);
2036 connman_dbus_dict_append_array(dict, "Nameservers",
2037 DBUS_TYPE_STRING, append_dns, service);
2039 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
2040 DBUS_TYPE_STRING, append_dnsconfig, service);
2042 connman_dbus_dict_append_array(dict, "Domains",
2043 DBUS_TYPE_STRING, append_domain, service);
2045 connman_dbus_dict_append_array(dict, "Domains.Configuration",
2046 DBUS_TYPE_STRING, append_domainconfig, service);
2048 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
2050 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
2051 append_proxyconfig, service);
2053 connman_dbus_dict_append_dict(dict, "Provider",
2054 append_provider, service);
2057 static void append_struct(gpointer value, gpointer user_data)
2059 struct connman_service *service = value;
2060 DBusMessageIter *iter = user_data;
2061 DBusMessageIter entry, dict;
2063 if (service->path == NULL)
2066 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
2068 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2071 connman_dbus_dict_open(&entry, &dict);
2072 append_properties(&dict, TRUE, service);
2073 connman_dbus_dict_close(&entry, &dict);
2075 dbus_message_iter_close_container(iter, &entry);
2078 void __connman_service_list_struct(DBusMessageIter *iter)
2080 g_sequence_foreach(service_list, append_struct, iter);
2083 connman_bool_t __connman_service_is_hidden(struct connman_service *service)
2085 return service->hidden;
2088 int __connman_service_get_index(struct connman_service *service)
2090 if (service == NULL)
2093 if (service->network != NULL)
2094 return connman_network_get_index(service->network);
2095 else if (service->provider != NULL)
2096 return connman_provider_get_index(service->provider);
2101 void __connman_service_set_domainname(struct connman_service *service,
2102 const char *domainname)
2104 if (service == NULL || service->hidden == TRUE)
2107 g_free(service->domainname);
2108 service->domainname = g_strdup(domainname);
2110 domain_changed(service);
2113 const char *connman_service_get_domainname(struct connman_service *service)
2115 if (service == NULL)
2118 if (service->domains != NULL)
2119 return service->domains[0];
2121 return service->domainname;
2124 char **connman_service_get_nameservers(struct connman_service *service)
2126 if (service == NULL)
2129 if (service->nameservers_config != NULL)
2130 return g_strdupv(service->nameservers_config);
2131 else if (service->nameservers != NULL ||
2132 service->nameservers_auto != NULL) {
2133 int len = 0, len_auto = 0, i;
2136 if (service->nameservers != NULL)
2137 len = g_strv_length(service->nameservers);
2138 if (service->nameservers_auto != NULL)
2139 len_auto = g_strv_length(service->nameservers_auto);
2141 nameservers = g_try_new0(char *, len + len_auto + 1);
2142 if (nameservers == NULL)
2145 for (i = 0; i < len; i++)
2146 nameservers[i] = g_strdup(service->nameservers[i]);
2148 for (i = 0; i < len_auto; i++)
2149 nameservers[i + len] =
2150 g_strdup(service->nameservers_auto[i]);
2158 void connman_service_set_proxy_method(struct connman_service *service,
2159 enum connman_service_proxy_method method)
2161 if (service == NULL || service->hidden == TRUE)
2164 service->proxy = method;
2166 proxy_changed(service);
2168 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
2169 __connman_notifier_proxy_changed(service);
2172 enum connman_service_proxy_method connman_service_get_proxy_method(
2173 struct connman_service *service)
2175 if (service == NULL)
2176 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2178 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
2179 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
2180 service->pac == NULL)
2181 return service->proxy;
2183 return service->proxy_config;
2186 return service->proxy;
2189 char **connman_service_get_proxy_servers(struct connman_service *service)
2191 return g_strdupv(service->proxies);
2194 char **connman_service_get_proxy_excludes(struct connman_service *service)
2196 return g_strdupv(service->excludes);
2199 const char *connman_service_get_proxy_url(struct connman_service *service)
2201 if (service == NULL)
2204 return service->pac;
2207 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
2210 if (service == NULL || service->hidden == TRUE)
2213 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
2215 if (service->ipconfig_ipv4) {
2216 if (__connman_ipconfig_set_proxy_autoconfig(
2217 service->ipconfig_ipv4, url) < 0)
2219 } else if (service->ipconfig_ipv6) {
2220 if (__connman_ipconfig_set_proxy_autoconfig(
2221 service->ipconfig_ipv6, url) < 0)
2226 proxy_changed(service);
2228 __connman_notifier_proxy_changed(service);
2231 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
2233 if (service == NULL)
2236 if (service->ipconfig_ipv4)
2237 return __connman_ipconfig_get_proxy_autoconfig(
2238 service->ipconfig_ipv4);
2239 else if (service->ipconfig_ipv6)
2240 return __connman_ipconfig_get_proxy_autoconfig(
2241 service->ipconfig_ipv6);
2245 static void update_timeservers(struct connman_service *service)
2249 if (service->timeservers == NULL)
2252 switch (service->state) {
2253 case CONNMAN_SERVICE_STATE_UNKNOWN:
2254 case CONNMAN_SERVICE_STATE_IDLE:
2255 case CONNMAN_SERVICE_STATE_ASSOCIATION:
2256 case CONNMAN_SERVICE_STATE_CONFIGURATION:
2258 case CONNMAN_SERVICE_STATE_FAILURE:
2259 case CONNMAN_SERVICE_STATE_DISCONNECT:
2260 for (i = 0; service->timeservers[i] != NULL; i++)
2261 connman_timeserver_remove(service->timeservers[i]);
2263 case CONNMAN_SERVICE_STATE_READY:
2264 case CONNMAN_SERVICE_STATE_ONLINE:
2268 for (i = 0; service->timeservers[i] != NULL; i++)
2269 connman_timeserver_append(service->timeservers[i]);
2272 int __connman_service_timeserver_append(struct connman_service *service,
2273 const char *timeserver)
2277 DBG("service %p timeserver %s", service, timeserver);
2279 if (timeserver == NULL)
2282 if (service->timeservers != NULL) {
2285 for (i = 0; service->timeservers[i] != NULL; i++)
2286 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2289 len = g_strv_length(service->timeservers);
2290 service->timeservers = g_try_renew(char *, service->timeservers,
2294 service->timeservers = g_try_new0(char *, len + 2);
2297 if (service->timeservers == NULL)
2300 service->timeservers[len] = g_strdup(timeserver);
2301 service->timeservers[len + 1] = NULL;
2303 update_timeservers(service);
2308 int __connman_service_timeserver_remove(struct connman_service *service,
2309 const char *timeserver)
2312 int len, i, j, found = 0;
2314 DBG("service %p timeserver %s", service, timeserver);
2316 if (timeserver == NULL)
2319 if (service->timeservers == NULL)
2322 for (i = 0; service->timeservers != NULL &&
2323 service->timeservers[i] != NULL; i++)
2324 if (g_strcmp0(service->timeservers[i], timeserver) == 0) {
2332 len = g_strv_length(service->timeservers);
2335 g_strfreev(service->timeservers);
2336 service->timeservers = NULL;
2341 servers = g_try_new0(char *, len);
2342 if (servers == NULL)
2345 for (i = 0, j = 0; i < len; i++) {
2346 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2347 servers[j] = g_strdup(service->timeservers[i]);
2348 if (servers[j] == NULL)
2353 servers[len - 1] = NULL;
2355 g_strfreev(service->timeservers);
2356 service->timeservers = servers;
2358 update_timeservers(service);
2363 void __connman_service_set_pac(struct connman_service *service,
2366 if (service->hidden == TRUE)
2368 g_free(service->pac);
2369 service->pac = g_strdup(pac);
2371 proxy_changed(service);
2374 void __connman_service_set_identity(struct connman_service *service,
2375 const char *identity)
2377 if (service->immutable || service->hidden == TRUE)
2380 g_free(service->identity);
2381 service->identity = g_strdup(identity);
2383 if (service->network != NULL)
2384 connman_network_set_string(service->network,
2389 void __connman_service_set_agent_identity(struct connman_service *service,
2390 const char *agent_identity)
2392 if (service->hidden == TRUE)
2394 g_free(service->agent_identity);
2395 service->agent_identity = g_strdup(agent_identity);
2397 if (service->network != NULL)
2398 connman_network_set_string(service->network,
2399 "WiFi.AgentIdentity",
2400 service->agent_identity);
2403 void __connman_service_set_passphrase(struct connman_service *service,
2404 const char* passphrase)
2406 if (service->immutable == TRUE || service->hidden == TRUE)
2409 g_free(service->passphrase);
2410 service->passphrase = g_strdup(passphrase);
2412 if (service->network != NULL)
2413 connman_network_set_string(service->network,
2415 service->passphrase);
2417 service_save(service);
2420 void __connman_service_set_agent_passphrase(struct connman_service *service,
2421 const char *agent_passphrase)
2423 if (service->hidden == TRUE)
2425 g_free(service->agent_passphrase);
2426 service->agent_passphrase = g_strdup(agent_passphrase);
2428 if (service->network != NULL)
2429 connman_network_set_string(service->network,
2430 "WiFi.AgentPassphrase",
2431 service->agent_passphrase);
2434 static DBusMessage *get_properties(DBusConnection *conn,
2435 DBusMessage *msg, void *user_data)
2437 struct connman_service *service = user_data;
2439 DBusMessageIter array, dict;
2441 DBG("service %p", service);
2443 reply = dbus_message_new_method_return(msg);
2447 dbus_message_iter_init_append(reply, &array);
2449 connman_dbus_dict_open(&array, &dict);
2450 append_properties(&dict, FALSE, service);
2451 connman_dbus_dict_close(&array, &dict);
2456 static int update_proxy_configuration(struct connman_service *service,
2457 DBusMessageIter *array)
2459 DBusMessageIter dict;
2460 enum connman_service_proxy_method method;
2461 GString *servers_str = NULL;
2462 GString *excludes_str = NULL;
2463 const char *url = NULL;
2465 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2467 dbus_message_iter_recurse(array, &dict);
2469 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2470 DBusMessageIter entry, variant;
2474 dbus_message_iter_recurse(&dict, &entry);
2476 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2479 dbus_message_iter_get_basic(&entry, &key);
2480 dbus_message_iter_next(&entry);
2482 if (dbus_message_iter_get_arg_type(&entry) !=
2486 dbus_message_iter_recurse(&entry, &variant);
2487 type = dbus_message_iter_get_arg_type(&variant);
2489 if (g_str_equal(key, "Method") == TRUE) {
2492 if (type != DBUS_TYPE_STRING)
2495 dbus_message_iter_get_basic(&variant, &val);
2496 method = string2proxymethod(val);
2497 } else if (g_str_equal(key, "URL") == TRUE) {
2498 if (type != DBUS_TYPE_STRING)
2501 dbus_message_iter_get_basic(&variant, &url);
2502 } else if (g_str_equal(key, "Servers") == TRUE) {
2503 DBusMessageIter str_array;
2505 if (type != DBUS_TYPE_ARRAY)
2508 servers_str = g_string_new(NULL);
2509 if (servers_str == NULL)
2512 dbus_message_iter_recurse(&variant, &str_array);
2514 while (dbus_message_iter_get_arg_type(&str_array) ==
2518 dbus_message_iter_get_basic(&str_array, &val);
2520 if (servers_str->len > 0)
2521 g_string_append_printf(servers_str,
2524 g_string_append(servers_str, val);
2526 dbus_message_iter_next(&str_array);
2528 } else if (g_str_equal(key, "Excludes") == TRUE) {
2529 DBusMessageIter str_array;
2531 if (type != DBUS_TYPE_ARRAY)
2534 excludes_str = g_string_new(NULL);
2535 if (excludes_str == NULL)
2538 dbus_message_iter_recurse(&variant, &str_array);
2540 while (dbus_message_iter_get_arg_type(&str_array) ==
2544 dbus_message_iter_get_basic(&str_array, &val);
2546 if (excludes_str->len > 0)
2547 g_string_append_printf(excludes_str,
2550 g_string_append(excludes_str, val);
2552 dbus_message_iter_next(&str_array);
2556 dbus_message_iter_next(&dict);
2560 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2562 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2563 if (servers_str == NULL && service->proxies == NULL)
2566 if (servers_str != NULL) {
2567 g_strfreev(service->proxies);
2569 if (servers_str->len > 0)
2570 service->proxies = g_strsplit_set(
2571 servers_str->str, " ", 0);
2573 service->proxies = NULL;
2576 if (excludes_str != NULL) {
2577 g_strfreev(service->excludes);
2579 if (excludes_str->len > 0)
2580 service->excludes = g_strsplit_set(
2581 excludes_str->str, " ", 0);
2583 service->excludes = NULL;
2586 if (service->proxies == NULL)
2587 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2590 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2591 g_free(service->pac);
2593 if (url != NULL && strlen(url) > 0)
2594 service->pac = g_strdup(url);
2596 service->pac = NULL;
2598 /* if we are connected:
2599 - if service->pac == NULL
2600 - if __connman_ipconfig_get_proxy_autoconfig(
2601 service->ipconfig) == NULL
2602 --> We should start WPAD */
2605 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2609 if (servers_str != NULL)
2610 g_string_free(servers_str, TRUE);
2612 if (excludes_str != NULL)
2613 g_string_free(excludes_str, TRUE);
2615 service->proxy_config = method;
2620 if (servers_str != NULL)
2621 g_string_free(servers_str, TRUE);
2623 if (excludes_str != NULL)
2624 g_string_free(excludes_str, TRUE);
2629 static int set_ipconfig(struct connman_service *service,
2630 struct connman_ipconfig *ipconfig,
2631 DBusMessageIter *array,
2632 enum connman_service_state state,
2633 enum connman_service_state *new_state)
2635 enum connman_ipconfig_method old_method;
2636 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2637 enum connman_ipconfig_type type;
2640 if (ipconfig == NULL)
2643 old_method = __connman_ipconfig_get_method(ipconfig);
2645 if (is_connecting_state(service, state) ||
2646 is_connected_state(service, state))
2647 __connman_network_clear_ipconfig(service->network, ipconfig);
2649 err = __connman_ipconfig_set_config(ipconfig, array);
2650 method = __connman_ipconfig_get_method(ipconfig);
2651 type = __connman_ipconfig_get_config_type(ipconfig);
2653 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2654 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2655 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2656 *new_state = service->state_ipv4 =
2657 CONNMAN_SERVICE_STATE_CONFIGURATION;
2658 __connman_ipconfig_enable(ipconfig);
2661 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2662 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2663 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2664 *new_state = service->state_ipv6;
2665 __connman_ipconfig_enable(ipconfig);
2669 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2670 type, method, state2string(*new_state));
2675 static DBusMessage *set_property(DBusConnection *conn,
2676 DBusMessage *msg, void *user_data)
2678 struct connman_service *service = user_data;
2679 DBusMessageIter iter, value;
2683 DBG("service %p", service);
2685 if (dbus_message_iter_init(msg, &iter) == FALSE)
2686 return __connman_error_invalid_arguments(msg);
2688 dbus_message_iter_get_basic(&iter, &name);
2689 dbus_message_iter_next(&iter);
2690 dbus_message_iter_recurse(&iter, &value);
2692 type = dbus_message_iter_get_arg_type(&value);
2694 if (g_str_equal(name, "AutoConnect") == TRUE) {
2695 connman_bool_t autoconnect;
2697 if (type != DBUS_TYPE_BOOLEAN)
2698 return __connman_error_invalid_arguments(msg);
2700 if (service->favorite == FALSE)
2701 return __connman_error_invalid_service(msg);
2703 dbus_message_iter_get_basic(&value, &autoconnect);
2705 if (service->autoconnect == autoconnect)
2706 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2708 service->autoconnect = autoconnect;
2710 autoconnect_changed(service);
2712 service_save(service);
2713 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2714 DBusMessageIter entry;
2719 if (type != DBUS_TYPE_ARRAY)
2720 return __connman_error_invalid_arguments(msg);
2722 str = g_string_new(NULL);
2724 return __connman_error_invalid_arguments(msg);
2726 index = connman_network_get_index(service->network);
2727 gw = __connman_ipconfig_get_gateway_from_index(index);
2729 if (gw && strlen(gw))
2730 __connman_service_nameserver_del_routes(service);
2732 dbus_message_iter_recurse(&value, &entry);
2734 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2736 dbus_message_iter_get_basic(&entry, &val);
2737 dbus_message_iter_next(&entry);
2739 g_string_append_printf(str, " %s", val);
2741 g_string_append(str, val);
2744 g_strfreev(service->nameservers_config);
2747 service->nameservers_config =
2748 g_strsplit_set(str->str, " ", 0);
2750 service->nameservers_config = NULL;
2753 g_string_free(str, TRUE);
2755 if (gw && strlen(gw))
2756 __connman_service_nameserver_add_routes(service, gw);
2758 update_nameservers(service);
2759 dns_configuration_changed(service);
2761 service_save(service);
2762 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2763 DBusMessageIter entry;
2766 if (type != DBUS_TYPE_ARRAY)
2767 return __connman_error_invalid_arguments(msg);
2769 str = g_string_new(NULL);
2771 return __connman_error_invalid_arguments(msg);
2773 dbus_message_iter_recurse(&value, &entry);
2775 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2777 dbus_message_iter_get_basic(&entry, &val);
2778 dbus_message_iter_next(&entry);
2780 g_string_append_printf(str, " %s", val);
2782 g_string_append(str, val);
2785 g_strfreev(service->domains);
2788 service->domains = g_strsplit_set(str->str, " ", 0);
2790 service->domains = NULL;
2792 g_string_free(str, TRUE);
2794 update_nameservers(service);
2795 domain_configuration_changed(service);
2797 service_save(service);
2798 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2801 if (type != DBUS_TYPE_ARRAY)
2802 return __connman_error_invalid_arguments(msg);
2804 err = update_proxy_configuration(service, &value);
2807 return __connman_error_failed(msg, -err);
2809 proxy_configuration_changed(service);
2811 __connman_notifier_proxy_changed(service);
2813 service_save(service);
2814 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2815 g_str_equal(name, "IPv6.Configuration")) {
2817 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2818 enum connman_service_state state =
2819 CONNMAN_SERVICE_STATE_UNKNOWN;
2824 if (service->ipconfig_ipv4 == NULL &&
2825 service->ipconfig_ipv6 == NULL)
2826 return __connman_error_invalid_property(msg);
2828 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2829 ipv4 = service->ipconfig_ipv4;
2830 err = set_ipconfig(service, ipv4, &value,
2831 service->state_ipv4, &state);
2833 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2834 ipv6 = service->ipconfig_ipv6;
2835 err = set_ipconfig(service, ipv6, &value,
2836 service->state_ipv6, &state);
2840 if (is_connected_state(service, state) ||
2841 is_connecting_state(service, state))
2842 __connman_network_set_ipconfig(service->network,
2844 return __connman_error_failed(msg, -err);
2848 ipv4_configuration_changed(service);
2850 ipv6_configuration_changed(service);
2852 if (is_connecting(service) || is_connected(service))
2853 __connman_network_set_ipconfig(service->network,
2856 service_save(service);
2858 return __connman_error_invalid_property(msg);
2860 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2863 static void set_idle(struct connman_service *service)
2865 service->state = service->state_ipv4 = service->state_ipv6 =
2866 CONNMAN_SERVICE_STATE_IDLE;
2867 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2868 state_changed(service);
2871 static DBusMessage *clear_property(DBusConnection *conn,
2872 DBusMessage *msg, void *user_data)
2874 struct connman_service *service = user_data;
2877 DBG("service %p", service);
2879 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2882 if (g_str_equal(name, "Error") == TRUE) {
2885 g_get_current_time(&service->modified);
2886 service_save(service);
2888 return __connman_error_invalid_property(msg);
2890 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2893 static connman_bool_t is_ignore(struct connman_service *service)
2895 if (service->autoconnect == FALSE)
2898 if (service->roaming == TRUE)
2901 if (service->ignore == TRUE)
2904 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
2910 void __connman_service_auto_connect(void)
2912 struct connman_service *service = NULL;
2913 GSequenceIter *iter;
2917 if (__connman_session_mode() == TRUE) {
2918 DBG("Session mode enabled: auto connect disabled");
2922 iter = g_sequence_get_begin_iter(service_list);
2924 while (g_sequence_iter_is_end(iter) == FALSE) {
2925 service = g_sequence_get(iter);
2927 if (service->pending != NULL)
2930 if (is_connecting(service) == TRUE)
2933 if (service->favorite == FALSE)
2936 if (is_connected(service) == TRUE)
2939 if (is_ignore(service) == FALSE && service->state ==
2940 CONNMAN_SERVICE_STATE_IDLE)
2945 iter = g_sequence_iter_next(iter);
2948 if (service != NULL) {
2949 service->userconnect = FALSE;
2950 __connman_service_connect(service);
2954 static void remove_timeout(struct connman_service *service)
2956 if (service->timeout > 0) {
2957 g_source_remove(service->timeout);
2958 service->timeout = 0;
2962 static void reply_pending(struct connman_service *service, int error)
2964 remove_timeout(service);
2966 if (service->pending != NULL) {
2970 reply = __connman_error_failed(service->pending,
2973 g_dbus_send_message(connection, reply);
2977 sender = dbus_message_get_interface(service->pending);
2979 DBG("sender %s", sender);
2981 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2982 g_dbus_send_reply(connection, service->pending,
2983 DBUS_TYPE_OBJECT_PATH, &service->path,
2986 g_dbus_send_reply(connection, service->pending,
2990 dbus_message_unref(service->pending);
2991 service->pending = NULL;
2995 static gboolean connect_timeout(gpointer user_data)
2997 struct connman_service *service = user_data;
2998 connman_bool_t autoconnect = FALSE;
3000 DBG("service %p", service);
3002 service->timeout = 0;
3004 if (service->network != NULL)
3005 __connman_network_disconnect(service->network);
3007 __connman_ipconfig_disable(service->ipconfig_ipv4);
3008 __connman_ipconfig_disable(service->ipconfig_ipv6);
3010 __connman_stats_service_unregister(service);
3012 if (service->pending != NULL) {
3015 reply = __connman_error_operation_timeout(service->pending);
3017 g_dbus_send_message(connection, reply);
3019 dbus_message_unref(service->pending);
3020 service->pending = NULL;
3024 __connman_service_ipconfig_indicate_state(service,
3025 CONNMAN_SERVICE_STATE_FAILURE,
3026 CONNMAN_IPCONFIG_TYPE_IPV4);
3027 __connman_service_ipconfig_indicate_state(service,
3028 CONNMAN_SERVICE_STATE_FAILURE,
3029 CONNMAN_IPCONFIG_TYPE_IPV6);
3031 if (autoconnect == TRUE && service->userconnect == FALSE)
3032 __connman_service_auto_connect();
3037 static void set_reconnect_state(struct connman_service *service,
3038 connman_bool_t reconnect)
3040 struct connman_device *device;
3042 if (service->network == NULL)
3045 device = connman_network_get_device(service->network);
3049 __connman_device_set_reconnect(device, reconnect);
3052 static connman_bool_t get_reconnect_state(struct connman_service *service)
3054 struct connman_device *device;
3056 if (service->network == NULL)
3059 device = connman_network_get_device(service->network);
3063 return __connman_device_get_reconnect(device);
3066 static DBusMessage *connect_service(DBusConnection *conn,
3067 DBusMessage *msg, void *user_data)
3069 struct connman_service *service = user_data;
3070 GSequenceIter *iter;
3073 DBG("service %p", service);
3075 if (service->pending != NULL)
3076 return __connman_error_in_progress(msg);
3078 iter = g_sequence_get_begin_iter(service_list);
3080 while (g_sequence_iter_is_end(iter) == FALSE) {
3081 struct connman_service *temp = g_sequence_get(iter);
3083 if (service->type == temp->type && is_connecting(temp) == TRUE)
3084 return __connman_error_in_progress(msg);
3086 iter = g_sequence_iter_next(iter);
3089 service->ignore = FALSE;
3091 service->userconnect = TRUE;
3093 service->pending = dbus_message_ref(msg);
3095 set_reconnect_state(service, FALSE);
3097 err = __connman_service_connect(service);
3099 if (service->pending == NULL)
3102 if (err != -EINPROGRESS) {
3103 dbus_message_unref(service->pending);
3104 service->pending = NULL;
3106 return __connman_error_failed(msg, -err);
3112 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3115 static DBusMessage *disconnect_service(DBusConnection *conn,
3116 DBusMessage *msg, void *user_data)
3118 struct connman_service *service = user_data;
3121 DBG("service %p", service);
3123 reply_pending(service, ECONNABORTED);
3125 service->ignore = TRUE;
3127 set_reconnect_state(service, FALSE);
3129 err = __connman_service_disconnect(service);
3131 if (err != -EINPROGRESS)
3132 return __connman_error_failed(msg, -err);
3137 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3140 static DBusMessage *remove_service(DBusConnection *conn,
3141 DBusMessage *msg, void *user_data)
3143 struct connman_service *service = user_data;
3145 DBG("service %p", service);
3147 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
3148 return __connman_error_not_supported(msg);
3150 if (service->immutable == TRUE || service->hidden == TRUE)
3151 return __connman_error_not_supported(msg);
3153 if (service->favorite == FALSE && service->state !=
3154 CONNMAN_SERVICE_STATE_FAILURE)
3155 return __connman_error_not_supported(msg);
3157 set_reconnect_state(service, FALSE);
3159 __connman_service_disconnect(service);
3161 g_free(service->passphrase);
3162 service->passphrase = NULL;
3166 __connman_service_set_favorite(service, FALSE);
3167 service_save(service);
3169 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3172 static gboolean check_suitable_state(enum connman_service_state a,
3173 enum connman_service_state b)
3176 * Special check so that "ready" service can be moved before
3179 if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
3180 b == CONNMAN_SERVICE_STATE_READY) ||
3181 (b == CONNMAN_SERVICE_STATE_ONLINE &&
3182 a == CONNMAN_SERVICE_STATE_READY))
3188 static void downgrade_state(struct connman_service *service)
3190 if (service == NULL)
3193 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
3194 service->state_ipv6);
3196 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
3197 __connman_service_ipconfig_indicate_state(service,
3198 CONNMAN_SERVICE_STATE_READY,
3199 CONNMAN_IPCONFIG_TYPE_IPV4);
3201 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
3202 __connman_service_ipconfig_indicate_state(service,
3203 CONNMAN_SERVICE_STATE_READY,
3204 CONNMAN_IPCONFIG_TYPE_IPV6);
3207 static void apply_relevant_default_downgrade(struct connman_service *service)
3209 struct connman_service *def_service;
3211 def_service = get_default();
3212 if (def_service == NULL)
3215 if (def_service == service &&
3216 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3217 def_service->state = CONNMAN_SERVICE_STATE_READY;
3220 static DBusMessage *move_service(DBusConnection *conn,
3221 DBusMessage *msg, void *user_data,
3224 struct connman_service *service = user_data;
3225 struct connman_service *target;
3227 GSequenceIter *src, *dst;
3228 enum connman_ipconfig_method target4, target6;
3229 enum connman_ipconfig_method service4, service6;
3231 DBG("service %p", service);
3233 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3236 if (service->favorite == FALSE)
3237 return __connman_error_not_supported(msg);
3239 target = find_service(path);
3240 if (target == NULL || target->favorite == FALSE || target == service ||
3241 target->type == CONNMAN_SERVICE_TYPE_VPN)
3242 return __connman_error_invalid_service(msg);
3244 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3245 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3246 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3247 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3249 DBG("target %s method %d/%d state %d/%d", target->identifier,
3251 target->state_ipv4, target->state_ipv6);
3253 DBG("service %s method %d/%d state %d/%d", service->identifier,
3255 service->state_ipv4, service->state_ipv6);
3258 * If method is OFF, then we do not need to check the corresponding
3261 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3262 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3263 if (check_suitable_state(target->state_ipv6,
3264 service->state_ipv6) == FALSE)
3265 return __connman_error_invalid_service(msg);
3269 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3270 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3271 if (check_suitable_state(target->state_ipv4,
3272 service->state_ipv4) == FALSE)
3273 return __connman_error_invalid_service(msg);
3277 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3278 if (target6 != 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 (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3286 if (target4 != 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 g_get_current_time(&service->modified);
3294 service_save(service);
3296 src = g_hash_table_lookup(service_hash, service->identifier);
3297 dst = g_hash_table_lookup(service_hash, target->identifier);
3300 * If the service which goes down is the default service and is
3301 * online, we downgrade directly its state to ready so:
3302 * the service which goes up, needs to recompute its state which
3303 * is triggered via downgrading it - if relevant - to state ready.
3305 if (before == TRUE) {
3306 apply_relevant_default_downgrade(target);
3307 g_sequence_move(src, dst);
3308 downgrade_state(service);
3310 apply_relevant_default_downgrade(service);
3311 g_sequence_move(dst, src);
3312 downgrade_state(target);
3315 services_changed(FALSE);
3317 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3320 static DBusMessage *move_before(DBusConnection *conn,
3321 DBusMessage *msg, void *user_data)
3323 return move_service(conn, msg, user_data, TRUE);
3326 static DBusMessage *move_after(DBusConnection *conn,
3327 DBusMessage *msg, void *user_data)
3329 return move_service(conn, msg, user_data, FALSE);
3332 static DBusMessage *reset_counters(DBusConnection *conn,
3333 DBusMessage *msg, void *user_data)
3335 struct connman_service *service = user_data;
3337 reset_stats(service);
3339 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3342 static GDBusMethodTable service_methods[] = {
3343 { "GetProperties", "", "a{sv}", get_properties },
3344 { "SetProperty", "sv", "", set_property },
3345 { "ClearProperty", "s", "", clear_property },
3346 { "Connect", "", "", connect_service,
3347 G_DBUS_METHOD_FLAG_ASYNC },
3348 { "Disconnect", "", "", disconnect_service },
3349 { "Remove", "", "", remove_service },
3350 { "MoveBefore", "o", "", move_before },
3351 { "MoveAfter", "o", "", move_after },
3352 { "ResetCounters", "", "", reset_counters },
3356 static GDBusSignalTable service_signals[] = {
3357 { "PropertyChanged", "sv" },
3361 static void service_free(gpointer user_data)
3363 struct connman_service *service = user_data;
3364 char *path = service->path;
3366 DBG("service %p", service);
3368 reply_pending(service, ENOENT);
3370 g_hash_table_remove(service_hash, service->identifier);
3372 __connman_notifier_service_remove(service);
3374 stats_stop(service);
3376 service->path = NULL;
3379 services_changed(FALSE);
3381 g_dbus_unregister_interface(connection, path,
3382 CONNMAN_SERVICE_INTERFACE);
3386 g_hash_table_destroy(service->counter_table);
3388 if (service->network != NULL) {
3389 __connman_network_disconnect(service->network);
3390 if (service->network_created == TRUE)
3391 connman_network_unref(service->network);
3394 if (service->provider != NULL)
3395 connman_provider_unref(service->provider);
3397 if (service->ipconfig_ipv4 != NULL) {
3398 connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3399 connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3400 connman_ipconfig_unref(service->ipconfig_ipv4);
3401 service->ipconfig_ipv4 = NULL;
3404 if (service->ipconfig_ipv6 != NULL) {
3405 connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3406 connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3407 connman_ipconfig_unref(service->ipconfig_ipv6);
3408 service->ipconfig_ipv6 = NULL;
3411 g_strfreev(service->nameservers);
3412 g_strfreev(service->nameservers_config);
3413 g_strfreev(service->nameservers_auto);
3414 g_strfreev(service->domains);
3415 g_strfreev(service->proxies);
3416 g_strfreev(service->excludes);
3418 g_free(service->domainname);
3419 g_free(service->pac);
3420 g_free(service->name);
3421 g_free(service->passphrase);
3422 g_free(service->agent_passphrase);
3423 g_free(service->identifier);
3424 g_free(service->eap);
3425 g_free(service->identity);
3426 g_free(service->agent_identity);
3427 g_free(service->ca_cert_file);
3428 g_free(service->client_cert_file);
3429 g_free(service->private_key_file);
3430 g_free(service->private_key_passphrase);
3431 g_free(service->phase2);
3433 if (service->stats.timer != NULL)
3434 g_timer_destroy(service->stats.timer);
3435 if (service->stats_roaming.timer != NULL)
3436 g_timer_destroy(service->stats_roaming.timer);
3442 * __connman_service_put:
3443 * @service: service structure
3445 * Release service if no longer needed
3447 void __connman_service_put(struct connman_service *service)
3449 GSequenceIter *iter;
3451 DBG("service %p", service);
3453 if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
3456 iter = g_hash_table_lookup(service_hash, service->identifier);
3458 reply_pending(service, ECONNABORTED);
3460 __connman_service_disconnect(service);
3462 g_sequence_remove(iter);
3464 service_free(service);
3468 static void stats_init(struct connman_service *service)
3471 service->stats.valid = FALSE;
3472 service->stats.enabled = FALSE;
3473 service->stats.timer = g_timer_new();
3476 service->stats_roaming.valid = FALSE;
3477 service->stats_roaming.enabled = FALSE;
3478 service->stats_roaming.timer = g_timer_new();
3481 static void service_initialize(struct connman_service *service)
3483 DBG("service %p", service);
3485 service->refcount = 1;
3486 service->session_usage_count = 0;
3488 service->network_created = FALSE;
3490 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3491 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3493 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3494 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3495 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3497 service->favorite = FALSE;
3498 service->immutable = FALSE;
3499 service->hidden = FALSE;
3501 service->ignore = FALSE;
3503 service->userconnect = FALSE;
3507 stats_init(service);
3509 service->provider = NULL;
3511 service->wps = FALSE;
3515 * connman_service_create:
3517 * Allocate a new service.
3519 * Returns: a newly-allocated #connman_service structure
3521 struct connman_service *connman_service_create(void)
3524 struct connman_stats_counter *counters;
3525 const char *counter;
3527 struct connman_service *service;
3529 service = g_try_new0(struct connman_service, 1);
3530 if (service == NULL)
3533 DBG("service %p", service);
3535 service->counter_table = g_hash_table_new_full(g_str_hash,
3536 g_str_equal, NULL, g_free);
3538 for (list = counter_list; list; list = list->next) {
3539 counter = list->data;
3541 counters = g_try_new0(struct connman_stats_counter, 1);
3542 if (counters == NULL) {
3543 g_hash_table_destroy(service->counter_table);
3548 counters->append_all = TRUE;
3550 g_hash_table_replace(service->counter_table, (gpointer)counter,
3554 service_initialize(service);
3560 * connman_service_ref:
3561 * @service: service structure
3563 * Increase reference counter of service
3565 struct connman_service *connman_service_ref(struct connman_service *service)
3569 __sync_fetch_and_add(&service->refcount, 1);
3575 * connman_service_unref:
3576 * @service: service structure
3578 * Decrease reference counter of service
3580 void connman_service_unref(struct connman_service *service)
3582 __connman_service_put(service);
3585 static gint service_compare(gconstpointer a, gconstpointer b,
3588 struct connman_service *service_a = (void *) a;
3589 struct connman_service *service_b = (void *) b;
3590 enum connman_service_state state_a, state_b;
3592 state_a = service_a->state;
3593 state_b = service_b->state;
3595 if (state_a != state_b) {
3596 gboolean a_connected = is_connected(service_a);
3597 gboolean b_connected = is_connected(service_b);
3599 if (a_connected == TRUE && b_connected == TRUE) {
3600 /* We prefer online over ready state */
3601 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
3604 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
3608 if (a_connected == TRUE)
3610 if (b_connected == TRUE)
3613 if (is_connecting(service_a) == TRUE)
3615 if (is_connecting(service_b) == TRUE)
3619 if (service_a->order > service_b->order)
3622 if (service_a->order < service_b->order)
3625 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3628 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3631 if (service_a->type != service_b->type) {
3632 switch (service_a->type) {
3633 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3634 case CONNMAN_SERVICE_TYPE_SYSTEM:
3635 case CONNMAN_SERVICE_TYPE_ETHERNET:
3636 case CONNMAN_SERVICE_TYPE_GPS:
3637 case CONNMAN_SERVICE_TYPE_VPN:
3638 case CONNMAN_SERVICE_TYPE_GADGET:
3640 case CONNMAN_SERVICE_TYPE_WIFI:
3642 case CONNMAN_SERVICE_TYPE_WIMAX:
3643 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3644 case CONNMAN_SERVICE_TYPE_CELLULAR:
3649 return (gint) service_b->strength - (gint) service_a->strength;
3653 * connman_service_get_type:
3654 * @service: service structure
3656 * Get the type of service
3658 enum connman_service_type connman_service_get_type(struct connman_service *service)
3660 if (service == NULL)
3661 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3663 return service->type;
3667 * connman_service_get_interface:
3668 * @service: service structure
3670 * Get network interface of service
3672 char *connman_service_get_interface(struct connman_service *service)
3676 if (service == NULL)
3679 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3680 if (service->ipconfig_ipv4)
3681 index = connman_ipconfig_get_index(
3682 service->ipconfig_ipv4);
3683 else if (service->ipconfig_ipv6)
3684 index = connman_ipconfig_get_index(
3685 service->ipconfig_ipv6);
3689 return connman_inet_ifname(index);
3692 if (service->network == NULL)
3695 index = connman_network_get_index(service->network);
3697 return connman_inet_ifname(index);
3701 * connman_service_get_network:
3702 * @service: service structure
3704 * Get the service network
3706 struct connman_network *
3707 __connman_service_get_network(struct connman_service *service)
3709 if (service == NULL)
3712 return service->network;
3715 struct connman_ipconfig *
3716 __connman_service_get_ip4config(struct connman_service *service)
3718 if (service == NULL)
3721 return service->ipconfig_ipv4;
3724 struct connman_ipconfig *
3725 __connman_service_get_ip6config(struct connman_service *service)
3727 if (service == NULL)
3730 return service->ipconfig_ipv6;
3733 struct connman_ipconfig *
3734 __connman_service_get_ipconfig(struct connman_service *service, int family)
3736 if (family == AF_INET)
3737 return __connman_service_get_ip4config(service);
3738 else if (family == AF_INET6)
3739 return __connman_service_get_ip6config(service);
3745 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3747 if (service == NULL)
3748 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3750 return service->security;
3753 const char *__connman_service_get_phase2(struct connman_service *service)
3755 if (service == NULL)
3758 return service->phase2;
3761 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3763 if (service == NULL)
3766 return service->wps;
3770 * __connman_service_set_favorite:
3771 * @service: service structure
3772 * @favorite: favorite value
3774 * Change the favorite setting of service
3776 int __connman_service_set_favorite(struct connman_service *service,
3777 connman_bool_t favorite)
3779 GSequenceIter *iter;
3781 if (service->hidden == TRUE)
3783 iter = g_hash_table_lookup(service_hash, service->identifier);
3787 if (service->favorite == favorite)
3790 service->favorite = favorite;
3791 service->order = __connman_service_get_order(service);
3793 favorite_changed(service);
3795 g_sequence_sort_changed(iter, service_compare, NULL);
3797 services_changed(FALSE);
3802 int __connman_service_set_immutable(struct connman_service *service,
3803 connman_bool_t immutable)
3805 if (service->hidden == TRUE)
3807 service->immutable = immutable;
3809 immutable_changed(service);
3814 void __connman_service_set_string(struct connman_service *service,
3815 const char *key, const char *value)
3817 if (service->hidden == TRUE)
3819 if (g_str_equal(key, "EAP") == TRUE) {
3820 g_free(service->eap);
3821 service->eap = g_strdup(value);
3822 } else if (g_str_equal(key, "Identity") == TRUE) {
3823 g_free(service->identity);
3824 service->identity = g_strdup(value);
3825 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3826 g_free(service->ca_cert_file);
3827 service->ca_cert_file = g_strdup(value);
3828 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3829 g_free(service->client_cert_file);
3830 service->client_cert_file = g_strdup(value);
3831 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3832 g_free(service->private_key_file);
3833 service->private_key_file = g_strdup(value);
3834 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3835 g_free(service->private_key_passphrase);
3836 service->private_key_passphrase = g_strdup(value);
3837 } else if (g_str_equal(key, "Phase2") == TRUE) {
3838 g_free(service->phase2);
3839 service->phase2 = g_strdup(value);
3840 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3841 g_free(service->passphrase);
3842 service->passphrase = g_strdup(value);
3846 static void service_complete(struct connman_service *service)
3848 reply_pending(service, EIO);
3850 if (service->userconnect == FALSE)
3851 __connman_service_auto_connect();
3853 g_get_current_time(&service->modified);
3854 service_save(service);
3857 static void report_error_cb(struct connman_service *service,
3858 gboolean retry, void *user_data)
3861 __connman_service_connect(service);
3863 service_complete(service);
3864 services_changed(FALSE);
3865 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3869 void __connman_service_add_passphrase(struct connman_service *service,
3870 const gchar *passphrase)
3872 switch (service->security) {
3873 case CONNMAN_SERVICE_SECURITY_WEP:
3874 case CONNMAN_SERVICE_SECURITY_PSK:
3875 __connman_service_set_passphrase(service, passphrase);
3877 case CONNMAN_SERVICE_SECURITY_8021X:
3878 __connman_service_set_agent_passphrase(service,
3881 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3882 case CONNMAN_SERVICE_SECURITY_NONE:
3883 case CONNMAN_SERVICE_SECURITY_WPA:
3884 case CONNMAN_SERVICE_SECURITY_RSN:
3885 DBG("service security '%s' (%d) not handled",
3886 security2string(service->security),
3893 static void request_input_cb (struct connman_service *service,
3894 connman_bool_t values_received,
3895 const char *name, int name_len,
3896 const char *identity, const char *passphrase,
3899 struct connman_device *device;
3901 DBG ("RequestInput return, %p", service);
3903 if (service->hidden == TRUE && name_len > 0 && name_len <= 32) {
3904 device = connman_network_get_device(service->network);
3905 __connman_device_request_hidden_scan(device,
3907 identity, passphrase);
3910 if (values_received == FALSE || service->hidden == TRUE) {
3911 service_complete(service);
3912 services_changed(FALSE);
3913 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3917 if (identity != NULL)
3918 __connman_service_set_agent_identity(service, identity);
3920 if (passphrase != NULL)
3921 __connman_service_add_passphrase(service, passphrase);
3923 __connman_service_connect(service);
3925 /* Never cache agent provided credentials */
3926 __connman_service_set_agent_identity(service, NULL);
3927 __connman_service_set_agent_passphrase(service, NULL);
3930 static void downgrade_connected_services(void)
3932 struct connman_service *up_service;
3933 GSequenceIter *iter;
3935 iter = g_sequence_get_begin_iter(service_list);
3936 while (g_sequence_iter_is_end(iter) == FALSE) {
3937 up_service = g_sequence_get(iter);
3939 if (is_connected(up_service) == FALSE) {
3940 iter = g_sequence_iter_next(iter);
3944 if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3947 downgrade_state(up_service);
3949 iter = g_sequence_iter_next(iter);
3953 static int service_indicate_state(struct connman_service *service)
3955 enum connman_service_state old_state, new_state;
3956 struct connman_service *def_service;
3957 GSequenceIter *iter;
3959 if (service == NULL)
3962 old_state = service->state;
3963 new_state = combine_state(service->state_ipv4, service->state_ipv6);
3965 DBG("service %p old %s - new %s/%s => %s",
3967 state2string(old_state),
3968 state2string(service->state_ipv4),
3969 state2string(service->state_ipv6),
3970 state2string(new_state));
3972 if (old_state == new_state)
3975 def_service = get_default();
3977 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
3978 if (def_service != NULL && def_service != service &&
3979 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3983 service->state = new_state;
3984 state_changed(service);
3986 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
3987 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3988 reply_pending(service, ECONNABORTED);
3990 __connman_service_disconnect(service);
3993 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3994 if (__connman_stats_service_register(service) == 0) {
3995 __connman_stats_get(service, FALSE,
3996 &service->stats.data);
3997 __connman_stats_get(service, TRUE,
3998 &service->stats_roaming.data);
4002 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4003 connman_timeserver_sync();
4006 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
4007 connman_bool_t reconnect;
4009 reconnect = get_reconnect_state(service);
4010 if (reconnect == TRUE)
4011 __connman_service_auto_connect();
4013 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4016 if (new_state == CONNMAN_SERVICE_STATE_READY) {
4017 enum connman_ipconfig_method method;
4019 set_reconnect_state(service, TRUE);
4021 __connman_service_set_favorite(service, TRUE);
4023 reply_pending(service, 0);
4025 service->userconnect = FALSE;
4027 g_get_current_time(&service->modified);
4028 service_save(service);
4030 update_nameservers(service);
4031 dns_changed(service);
4032 domain_changed(service);
4034 __connman_notifier_connect(service->type);
4036 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4037 connman_network_get_bool(service->network,
4038 "WiFi.UseWPS") == TRUE) {
4041 pass = connman_network_get_string(service->network,
4044 __connman_service_set_passphrase(service, pass);
4046 connman_network_set_bool(service->network,
4047 "WiFi.UseWPS", FALSE);
4052 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4053 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4054 __connman_ipconfig_disable_ipv6(
4055 service->ipconfig_ipv6);
4057 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4058 def_service = get_default();
4060 if (__connman_notifier_count_connected() == 0 &&
4061 def_service != NULL &&
4062 def_service->provider != NULL)
4063 __connman_provider_disconnect(def_service->provider);
4067 __connman_wispr_stop(service);
4069 __connman_wpad_stop(service);
4071 update_nameservers(service);
4072 dns_changed(service);
4073 domain_changed(service);
4075 __connman_notifier_disconnect(service->type);
4078 * Previous services which are connected and which states
4079 * are set to online should reset relevantly ipconfig_state
4080 * to ready so wispr/portal will be rerun on those
4082 downgrade_connected_services();
4085 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4086 if (service->userconnect == TRUE &&
4087 __connman_agent_report_error(service,
4088 error2string(service->error),
4089 report_error_cb, NULL) == -EIO)
4091 service_complete(service);
4093 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4095 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4097 iter = g_hash_table_lookup(service_hash, service->identifier);
4099 g_sequence_sort_changed(iter, service_compare, NULL);
4101 services_changed(FALSE);
4103 if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
4109 int __connman_service_indicate_error(struct connman_service *service,
4110 enum connman_service_error error)
4112 DBG("service %p error %d", service, error);
4114 if (service == NULL)
4117 service->error = error;
4119 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4120 __connman_service_set_passphrase(service, NULL);
4122 __connman_service_ipconfig_indicate_state(service,
4123 CONNMAN_SERVICE_STATE_FAILURE,
4124 CONNMAN_IPCONFIG_TYPE_IPV4);
4125 __connman_service_ipconfig_indicate_state(service,
4126 CONNMAN_SERVICE_STATE_FAILURE,
4127 CONNMAN_IPCONFIG_TYPE_IPV6);
4131 int __connman_service_clear_error(struct connman_service *service)
4133 DBG("service %p", service);
4135 if (service == NULL)
4138 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4141 service->state_ipv4 = service->state_ipv6 =
4142 CONNMAN_SERVICE_STATE_UNKNOWN;
4143 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
4145 if (service->favorite == TRUE)
4146 set_reconnect_state(service, TRUE);
4148 __connman_service_ipconfig_indicate_state(service,
4149 CONNMAN_SERVICE_STATE_IDLE,
4150 CONNMAN_IPCONFIG_TYPE_IPV6);
4153 * Toggling the IPv6 state to IDLE could trigger the auto connect
4154 * machinery and consequently the IPv4 state.
4156 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4157 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4160 return __connman_service_ipconfig_indicate_state(service,
4161 CONNMAN_SERVICE_STATE_IDLE,
4162 CONNMAN_IPCONFIG_TYPE_IPV4);
4165 int __connman_service_indicate_default(struct connman_service *service)
4167 DBG("service %p", service);
4174 enum connman_service_state __connman_service_ipconfig_get_state(
4175 struct connman_service *service,
4176 enum connman_ipconfig_type type)
4178 if (service == NULL)
4179 return CONNMAN_SERVICE_STATE_UNKNOWN;
4181 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4182 return service->state_ipv4;
4184 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4185 return service->state_ipv6;
4187 return CONNMAN_SERVICE_STATE_UNKNOWN;
4190 static void check_proxy_setup(struct connman_service *service)
4193 * We start WPAD if we haven't got a PAC URL from DHCP and
4194 * if our proxy manual configuration is either empty or set
4195 * to AUTO with an empty URL.
4198 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4201 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4202 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4203 service->pac != NULL))
4206 if (__connman_wpad_start(service) < 0) {
4207 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4208 __connman_notifier_proxy_changed(service);
4215 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
4219 * How many networks are connected at the same time. If more than 1,
4220 * then set the rp_filter setting properly (loose mode routing) so that network
4221 * connectivity works ok. This is only done for IPv4 networks as IPv6
4222 * does not have rp_filter knob.
4224 static int connected_networks_count;
4225 static int original_rp_filter;
4227 static void service_rp_filter(struct connman_service *service,
4230 enum connman_ipconfig_method method;
4232 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4235 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
4236 case CONNMAN_IPCONFIG_METHOD_OFF:
4237 case CONNMAN_IPCONFIG_METHOD_AUTO:
4239 case CONNMAN_IPCONFIG_METHOD_FIXED:
4240 case CONNMAN_IPCONFIG_METHOD_MANUAL:
4241 case CONNMAN_IPCONFIG_METHOD_DHCP:
4245 if (connected == TRUE) {
4246 if (connected_networks_count == 1) {
4248 filter_value = __connman_ipconfig_set_rp_filter();
4249 if (filter_value < 0)
4252 original_rp_filter = filter_value;
4254 connected_networks_count++;
4257 if (connected_networks_count == 2)
4258 __connman_ipconfig_unset_rp_filter(original_rp_filter);
4260 connected_networks_count--;
4261 if (connected_networks_count < 0)
4262 connected_networks_count = 0;
4265 DBG("%s %s ipconfig %p method %d count %d filter %d",
4266 connected ? "connected" : "disconnected", service->identifier,
4267 service->ipconfig_ipv4, method,
4268 connected_networks_count, original_rp_filter);
4271 static gboolean redo_wispr(gpointer user_data)
4273 struct connman_service *service = user_data;
4277 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV6);
4282 int __connman_service_online_check_failed(struct connman_service *service,
4283 enum connman_ipconfig_type type)
4285 DBG("service %p type %d count %d", service, type,
4286 service->online_check_count);
4288 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4289 /* currently we only retry IPv6 stuff */
4292 if (service->online_check_count != 1)
4295 service->online_check_count = 0;
4298 * We set the timeout to 1 sec so that we have a chance to get
4299 * necessary IPv6 router advertisement messages that might have
4302 g_timeout_add_seconds(1, redo_wispr, service);
4307 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
4308 enum connman_service_state new_state,
4309 enum connman_ipconfig_type type)
4311 struct connman_ipconfig *ipconfig = NULL;
4312 enum connman_service_state old_state;
4315 if (service == NULL)
4318 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4319 old_state = service->state_ipv4;
4320 ipconfig = service->ipconfig_ipv4;
4321 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4322 old_state = service->state_ipv6;
4323 ipconfig = service->ipconfig_ipv6;
4326 if (ipconfig == NULL)
4330 if (old_state == new_state)
4333 DBG("service %p (%s) state %d (%s) type %d (%s)",
4334 service, service ? service->identifier : NULL,
4335 new_state, state2string(new_state),
4336 type, __connman_ipconfig_type2string(type));
4338 switch (new_state) {
4339 case CONNMAN_SERVICE_STATE_UNKNOWN:
4340 case CONNMAN_SERVICE_STATE_IDLE:
4341 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
4344 case CONNMAN_SERVICE_STATE_ASSOCIATION:
4346 case CONNMAN_SERVICE_STATE_CONFIGURATION:
4347 __connman_ipconfig_enable(ipconfig);
4349 case CONNMAN_SERVICE_STATE_READY:
4350 update_nameservers(service);
4352 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4353 check_proxy_setup(service);
4354 service_rp_filter(service, TRUE);
4356 service->online_check_count = 1;
4357 __connman_wispr_start(service, type);
4360 case CONNMAN_SERVICE_STATE_ONLINE:
4362 case CONNMAN_SERVICE_STATE_DISCONNECT:
4363 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
4366 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4367 service_rp_filter(service, FALSE);
4370 case CONNMAN_SERVICE_STATE_FAILURE:
4374 /* We keep that state */
4375 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4376 service->state_ipv4 = new_state;
4377 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4378 service->state_ipv6 = new_state;
4380 ret = service_indicate_state(service);
4383 * If the ipconfig method is OFF, then we set the state to IDLE
4384 * so that it will not affect the combined state in the future.
4386 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4387 enum connman_ipconfig_method method;
4388 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4389 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4390 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4391 service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
4392 ret = service_indicate_state(service);
4395 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4396 enum connman_ipconfig_method method;
4397 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4398 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4399 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4400 service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4401 ret = service_indicate_state(service);
4408 static connman_bool_t prepare_network(struct connman_service *service)
4410 enum connman_network_type type;
4411 unsigned int ssid_len;
4413 type = connman_network_get_type(service->network);
4416 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4417 case CONNMAN_NETWORK_TYPE_VENDOR:
4419 case CONNMAN_NETWORK_TYPE_WIFI:
4420 if (connman_network_get_blob(service->network, "WiFi.SSID",
4424 if (service->passphrase != NULL)
4425 connman_network_set_string(service->network,
4426 "WiFi.Passphrase", service->passphrase);
4428 case CONNMAN_NETWORK_TYPE_ETHERNET:
4429 case CONNMAN_NETWORK_TYPE_WIMAX:
4430 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4431 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4432 case CONNMAN_NETWORK_TYPE_CELLULAR:
4439 static void prepare_8021x(struct connman_service *service)
4441 if (service->eap != NULL)
4442 connman_network_set_string(service->network, "WiFi.EAP",
4445 if (service->identity != NULL)
4446 connman_network_set_string(service->network, "WiFi.Identity",
4449 if (service->ca_cert_file != NULL)
4450 connman_network_set_string(service->network, "WiFi.CACertFile",
4451 service->ca_cert_file);
4453 if (service->client_cert_file != NULL)
4454 connman_network_set_string(service->network,
4455 "WiFi.ClientCertFile",
4456 service->client_cert_file);
4458 if (service->private_key_file != NULL)
4459 connman_network_set_string(service->network,
4460 "WiFi.PrivateKeyFile",
4461 service->private_key_file);
4463 if (service->private_key_passphrase != NULL)
4464 connman_network_set_string(service->network,
4465 "WiFi.PrivateKeyPassphrase",
4466 service->private_key_passphrase);
4468 if (service->phase2 != NULL)
4469 connman_network_set_string(service->network, "WiFi.Phase2",
4473 static int service_connect(struct connman_service *service)
4477 if (service->hidden == TRUE)
4480 switch (service->type) {
4481 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4482 case CONNMAN_SERVICE_TYPE_SYSTEM:
4483 case CONNMAN_SERVICE_TYPE_GPS:
4484 case CONNMAN_SERVICE_TYPE_GADGET:
4486 case CONNMAN_SERVICE_TYPE_ETHERNET:
4487 case CONNMAN_SERVICE_TYPE_WIMAX:
4488 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4489 case CONNMAN_SERVICE_TYPE_CELLULAR:
4490 case CONNMAN_SERVICE_TYPE_VPN:
4492 case CONNMAN_SERVICE_TYPE_WIFI:
4493 switch (service->security) {
4494 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4495 case CONNMAN_SERVICE_SECURITY_NONE:
4497 case CONNMAN_SERVICE_SECURITY_WEP:
4498 case CONNMAN_SERVICE_SECURITY_PSK:
4499 case CONNMAN_SERVICE_SECURITY_WPA:
4500 case CONNMAN_SERVICE_SECURITY_RSN:
4501 if (service->passphrase == NULL) {
4502 if (service->network == NULL)
4505 if (service->wps == FALSE ||
4506 connman_network_get_bool(
4508 "WiFi.UseWPS") == FALSE)
4512 case CONNMAN_SERVICE_SECURITY_8021X:
4513 if (service->eap == NULL)
4517 * never request credentials if using EAP-TLS
4518 * (EAP-TLS networks need to be fully provisioned)
4520 if (g_str_equal(service->eap, "tls") == TRUE)
4524 * Return -ENOKEY if either identity or passphrase is
4525 * missing. Agent provided credentials can be used as
4526 * fallback if needed.
4528 if ((service->identity == NULL &&
4529 service->agent_identity == NULL) ||
4530 (service->passphrase == NULL &&
4531 service->agent_passphrase == NULL))
4539 if (service->network != NULL) {
4540 if (prepare_network(service) == FALSE)
4543 switch (service->security) {
4544 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4545 case CONNMAN_SERVICE_SECURITY_NONE:
4546 case CONNMAN_SERVICE_SECURITY_WEP:
4547 case CONNMAN_SERVICE_SECURITY_PSK:
4548 case CONNMAN_SERVICE_SECURITY_WPA:
4549 case CONNMAN_SERVICE_SECURITY_RSN:
4551 case CONNMAN_SERVICE_SECURITY_8021X:
4552 prepare_8021x(service);
4556 if (__connman_stats_service_register(service) == 0) {
4557 __connman_stats_get(service, FALSE,
4558 &service->stats.data);
4559 __connman_stats_get(service, TRUE,
4560 &service->stats_roaming.data);
4563 if (service->ipconfig_ipv4)
4564 __connman_ipconfig_enable(service->ipconfig_ipv4);
4565 if (service->ipconfig_ipv6)
4566 __connman_ipconfig_enable(service->ipconfig_ipv6);
4568 err = __connman_network_connect(service->network);
4569 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4570 service->provider != NULL)
4571 err = __connman_provider_connect(service->provider);
4576 if (err != -EINPROGRESS) {
4577 __connman_ipconfig_disable(service->ipconfig_ipv4);
4578 __connman_ipconfig_disable(service->ipconfig_ipv6);
4579 __connman_stats_service_unregister(service);
4587 int __connman_service_connect(struct connman_service *service)
4591 DBG("service %p state %s", service, state2string(service->state));
4593 if (is_connected(service) == TRUE)
4596 if (is_connecting(service) == TRUE)
4599 switch (service->type) {
4600 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4601 case CONNMAN_SERVICE_TYPE_SYSTEM:
4602 case CONNMAN_SERVICE_TYPE_GPS:
4603 case CONNMAN_SERVICE_TYPE_GADGET:
4606 err = service_connect(service);
4612 if (err == -EINPROGRESS) {
4613 if (service->timeout == 0)
4614 service->timeout = g_timeout_add_seconds(
4615 CONNECT_TIMEOUT, connect_timeout, service);
4617 return -EINPROGRESS;
4620 __connman_service_ipconfig_indicate_state(service,
4621 CONNMAN_SERVICE_STATE_FAILURE,
4622 CONNMAN_IPCONFIG_TYPE_IPV4);
4623 __connman_service_ipconfig_indicate_state(service,
4624 CONNMAN_SERVICE_STATE_FAILURE,
4625 CONNMAN_IPCONFIG_TYPE_IPV6);
4627 if (service->network != NULL)
4628 __connman_network_disconnect(service->network);
4629 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4630 service->provider != NULL)
4631 __connman_provider_disconnect(service->provider);
4633 if (service->userconnect == TRUE) {
4634 if (err == -ENOKEY || err == -EPERM) {
4635 if (__connman_agent_request_passphrase_input(service,
4638 return -EINPROGRESS;
4640 reply_pending(service, err);
4646 int __connman_service_disconnect(struct connman_service *service)
4650 DBG("service %p", service);
4652 if (service->network != NULL) {
4653 err = __connman_network_disconnect(service->network);
4654 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4655 service->provider != NULL)
4656 err = __connman_provider_disconnect(service->provider);
4660 if (err < 0 && err != -EINPROGRESS)
4663 __connman_6to4_remove(service->ipconfig_ipv4);
4665 if (service->ipconfig_ipv4)
4666 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
4669 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
4672 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
4673 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
4675 __connman_ipconfig_disable(service->ipconfig_ipv4);
4676 __connman_ipconfig_disable(service->ipconfig_ipv6);
4678 __connman_stats_service_unregister(service);
4683 int __connman_service_disconnect_all(void)
4685 GSequenceIter *iter;
4686 GSList *services = NULL, *list;
4690 iter = g_sequence_get_begin_iter(service_list);
4692 while (g_sequence_iter_is_end(iter) == FALSE) {
4693 struct connman_service *service = g_sequence_get(iter);
4695 services = g_slist_prepend(services, service);
4697 iter = g_sequence_iter_next(iter);
4700 for (list = services; list != NULL; list = list->next) {
4701 struct connman_service *service = list->data;
4703 service->ignore = TRUE;
4705 set_reconnect_state(service, FALSE);
4707 __connman_service_disconnect(service);
4717 * lookup_by_identifier:
4718 * @identifier: service identifier
4720 * Look up a service by identifier (reference count will not be increased)
4722 static struct connman_service *lookup_by_identifier(const char *identifier)
4724 GSequenceIter *iter;
4726 iter = g_hash_table_lookup(service_hash, identifier);
4728 return g_sequence_get(iter);
4733 static void provision_changed(gpointer value, gpointer user_data)
4735 struct connman_service *service = value;
4736 char *path = user_data;
4738 __connman_config_provision_service_ident(service, path);
4741 void __connman_service_provision_changed(const char *ident)
4743 g_sequence_foreach(service_list, provision_changed, (void *)ident);
4747 * __connman_service_get:
4748 * @identifier: service identifier
4750 * Look up a service by identifier or create a new one if not found
4752 static struct connman_service *service_get(const char *identifier)
4754 struct connman_service *service;
4755 GSequenceIter *iter;
4757 iter = g_hash_table_lookup(service_hash, identifier);
4759 service = g_sequence_get(iter);
4760 if (service != NULL)
4761 connman_service_ref(service);
4765 service = connman_service_create();
4766 if (service == NULL)
4769 DBG("service %p", service);
4771 service->identifier = g_strdup(identifier);
4773 iter = g_sequence_insert_sorted(service_list, service,
4774 service_compare, NULL);
4776 g_hash_table_insert(service_hash, service->identifier, iter);
4781 static int service_register(struct connman_service *service)
4783 GSequenceIter *iter;
4785 DBG("service %p", service);
4787 if (service->path != NULL)
4790 service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
4791 service->identifier);
4793 DBG("path %s", service->path);
4795 __connman_config_provision_service(service);
4797 service_load(service);
4799 g_dbus_register_interface(connection, service->path,
4800 CONNMAN_SERVICE_INTERFACE,
4801 service_methods, service_signals,
4802 NULL, service, NULL);
4804 iter = g_hash_table_lookup(service_hash, service->identifier);
4806 g_sequence_sort_changed(iter, service_compare, NULL);
4808 services_changed(TRUE);
4813 static void service_up(struct connman_ipconfig *ipconfig)
4815 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4817 DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
4819 link_changed(service);
4821 service->stats.valid = FALSE;
4822 service->stats_roaming.valid = FALSE;
4825 static void service_down(struct connman_ipconfig *ipconfig)
4827 DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
4830 static void service_lower_up(struct connman_ipconfig *ipconfig)
4832 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4834 DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
4836 stats_start(service);
4839 static void service_lower_down(struct connman_ipconfig *ipconfig)
4841 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4843 DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
4845 stats_stop(service);
4846 service_save(service);
4849 static void service_ip_bound(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 bound", 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_AUTO)
4865 __connman_service_ipconfig_indicate_state(service,
4866 CONNMAN_SERVICE_STATE_READY,
4867 CONNMAN_IPCONFIG_TYPE_IPV6);
4869 settings_changed(service, ipconfig);
4872 static void service_ip_release(struct connman_ipconfig *ipconfig)
4874 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4875 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4876 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4878 DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
4880 type = __connman_ipconfig_get_config_type(ipconfig);
4881 method = __connman_ipconfig_get_method(ipconfig);
4883 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4886 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4887 method == CONNMAN_IPCONFIG_METHOD_OFF)
4888 __connman_service_ipconfig_indicate_state(service,
4889 CONNMAN_SERVICE_STATE_DISCONNECT,
4890 CONNMAN_IPCONFIG_TYPE_IPV6);
4892 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4893 method == CONNMAN_IPCONFIG_METHOD_OFF)
4894 __connman_service_ipconfig_indicate_state(service,
4895 CONNMAN_SERVICE_STATE_DISCONNECT,
4896 CONNMAN_IPCONFIG_TYPE_IPV4);
4898 settings_changed(service, ipconfig);
4901 static const struct connman_ipconfig_ops service_ops = {
4903 .down = service_down,
4904 .lower_up = service_lower_up,
4905 .lower_down = service_lower_down,
4906 .ip_bound = service_ip_bound,
4907 .ip_release = service_ip_release,
4910 static void setup_ip4config(struct connman_service *service, int index,
4911 enum connman_ipconfig_method method)
4913 service->ipconfig_ipv4 = connman_ipconfig_create(index,
4914 CONNMAN_IPCONFIG_TYPE_IPV4);
4915 if (service->ipconfig_ipv4 == NULL)
4918 connman_ipconfig_set_method(service->ipconfig_ipv4, method);
4920 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
4922 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
4925 static void setup_ip6config(struct connman_service *service, int index)
4927 service->ipconfig_ipv6 = connman_ipconfig_create(index,
4928 CONNMAN_IPCONFIG_TYPE_IPV6);
4929 if (service->ipconfig_ipv6 == NULL)
4932 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
4934 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
4937 void __connman_service_read_ip4config(struct connman_service *service)
4941 if (service->ipconfig_ipv4 == NULL)
4944 keyfile = connman_storage_load_service(service->identifier);
4945 if (keyfile == NULL)
4948 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
4949 service->identifier, "IPv4.");
4951 g_key_file_free(keyfile);
4954 void __connman_service_create_ip4config(struct connman_service *service,
4957 DBG("ipv4 %p", service->ipconfig_ipv4);
4959 if (service->ipconfig_ipv4 != NULL)
4962 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
4963 __connman_service_read_ip4config(service);
4966 void __connman_service_read_ip6config(struct connman_service *service)
4970 if (service->ipconfig_ipv6 == NULL)
4973 keyfile = connman_storage_load_service(service->identifier);
4974 if (keyfile == NULL)
4977 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
4978 service->identifier, "IPv6.");
4980 g_key_file_free(keyfile);
4983 void __connman_service_create_ip6config(struct connman_service *service,
4986 DBG("ipv6 %p", service->ipconfig_ipv6);
4988 if (service->ipconfig_ipv6 != NULL)
4991 setup_ip6config(service, index);
4993 __connman_service_read_ip6config(service);
4997 * __connman_service_lookup_from_network:
4998 * @network: network structure
5000 * Look up a service by network (reference count will not be increased)
5002 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
5004 struct connman_service *service;
5005 const char *ident, *group;
5008 DBG("network %p", network);
5010 if (network == NULL)
5013 ident = __connman_network_get_ident(network);
5017 group = connman_network_get_group(network);
5021 name = g_strdup_printf("%s_%s_%s",
5022 __connman_network_get_type(network), ident, group);
5023 service = lookup_by_identifier(name);
5029 struct connman_service *__connman_service_lookup_from_index(int index)
5031 struct connman_service *service;
5032 GSequenceIter *iter;
5034 iter = g_sequence_get_begin_iter(service_list);
5036 while (g_sequence_iter_is_end(iter) == FALSE) {
5037 service = g_sequence_get(iter);
5039 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
5043 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
5047 iter = g_sequence_iter_next(iter);
5053 const char *__connman_service_get_ident(struct connman_service *service)
5055 return service->identifier;
5058 const char *__connman_service_get_path(struct connman_service *service)
5060 return service->path;
5063 unsigned int __connman_service_get_order(struct connman_service *service)
5065 GSequenceIter *iter;
5067 if (service == NULL)
5070 if (service->favorite == FALSE) {
5075 iter = g_hash_table_lookup(service_hash, service->identifier);
5077 if (g_sequence_iter_get_position(iter) == 0)
5079 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
5080 service->order = 10;
5086 return service->order;
5089 static enum connman_service_type convert_network_type(struct connman_network *network)
5091 enum connman_network_type type = connman_network_get_type(network);
5094 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5095 case CONNMAN_NETWORK_TYPE_VENDOR:
5097 case CONNMAN_NETWORK_TYPE_ETHERNET:
5098 return CONNMAN_SERVICE_TYPE_ETHERNET;
5099 case CONNMAN_NETWORK_TYPE_WIFI:
5100 return CONNMAN_SERVICE_TYPE_WIFI;
5101 case CONNMAN_NETWORK_TYPE_WIMAX:
5102 return CONNMAN_SERVICE_TYPE_WIMAX;
5103 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5104 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5105 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5106 case CONNMAN_NETWORK_TYPE_CELLULAR:
5107 return CONNMAN_SERVICE_TYPE_CELLULAR;
5110 return CONNMAN_SERVICE_TYPE_UNKNOWN;
5113 static enum connman_service_security convert_wifi_security(const char *security)
5115 if (security == NULL)
5116 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5117 else if (g_str_equal(security, "none") == TRUE)
5118 return CONNMAN_SERVICE_SECURITY_NONE;
5119 else if (g_str_equal(security, "wep") == TRUE)
5120 return CONNMAN_SERVICE_SECURITY_WEP;
5121 else if (g_str_equal(security, "psk") == TRUE)
5122 return CONNMAN_SERVICE_SECURITY_PSK;
5123 else if (g_str_equal(security, "ieee8021x") == TRUE)
5124 return CONNMAN_SERVICE_SECURITY_8021X;
5125 else if (g_str_equal(security, "wpa") == TRUE)
5126 return CONNMAN_SERVICE_SECURITY_WPA;
5127 else if (g_str_equal(security, "rsn") == TRUE)
5128 return CONNMAN_SERVICE_SECURITY_RSN;
5130 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5133 static void update_from_network(struct connman_service *service,
5134 struct connman_network *network)
5136 connman_uint8_t strength = service->strength;
5137 GSequenceIter *iter;
5140 DBG("service %p network %p", service, network);
5142 if (is_connected(service) == TRUE)
5145 if (is_connecting(service) == TRUE)
5148 str = connman_network_get_string(network, "Name");
5150 g_free(service->name);
5151 service->name = g_strdup(str);
5152 service->hidden = FALSE;
5154 g_free(service->name);
5155 service->name = NULL;
5156 service->hidden = TRUE;
5159 service->strength = connman_network_get_strength(network);
5160 service->roaming = connman_network_get_bool(network, "Roaming");
5162 if (service->strength == 0) {
5164 * Filter out 0-values; it's unclear what they mean
5165 * and they cause anomalous sorting of the priority list.
5167 service->strength = strength;
5170 str = connman_network_get_string(network, "WiFi.Security");
5171 service->security = convert_wifi_security(str);
5173 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5174 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5176 if (service->strength > strength && service->network != NULL) {
5177 service->network = network;
5179 strength_changed(service);
5182 if (service->network == NULL)
5183 service->network = network;
5185 iter = g_hash_table_lookup(service_hash, service->identifier);
5187 g_sequence_sort_changed(iter, service_compare, NULL);
5191 * __connman_service_create_from_network:
5192 * @network: network structure
5194 * Look up service by network and if not found, create one
5196 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5198 struct connman_service *service;
5199 struct connman_device *device;
5200 const char *ident, *group;
5204 DBG("network %p", network);
5206 if (network == NULL)
5209 ident = __connman_network_get_ident(network);
5213 group = connman_network_get_group(network);
5217 name = g_strdup_printf("%s_%s_%s",
5218 __connman_network_get_type(network), ident, group);
5219 service = service_get(name);
5222 if (service == NULL)
5225 if (__connman_network_get_weakness(network) == TRUE)
5228 if (service->path != NULL) {
5229 update_from_network(service, network);
5230 services_changed(TRUE);
5234 service->type = convert_network_type(network);
5236 switch (service->type) {
5237 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5238 case CONNMAN_SERVICE_TYPE_SYSTEM:
5239 case CONNMAN_SERVICE_TYPE_ETHERNET:
5240 case CONNMAN_SERVICE_TYPE_WIMAX:
5241 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5242 case CONNMAN_SERVICE_TYPE_GPS:
5243 case CONNMAN_SERVICE_TYPE_VPN:
5244 case CONNMAN_SERVICE_TYPE_GADGET:
5245 service->autoconnect = FALSE;
5247 case CONNMAN_SERVICE_TYPE_WIFI:
5248 case CONNMAN_SERVICE_TYPE_CELLULAR:
5249 service->autoconnect = TRUE;
5253 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5254 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5256 update_from_network(service, network);
5258 index = connman_network_get_index(network);
5260 if (service->ipconfig_ipv4 == NULL)
5261 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5263 if (service->ipconfig_ipv6 == NULL)
5264 setup_ip6config(service, index);
5266 service_register(service);
5268 if (service->favorite == TRUE) {
5269 device = connman_network_get_device(service->network);
5270 if (device && __connman_device_scanning(device) == FALSE)
5271 __connman_service_auto_connect();
5274 __connman_notifier_service_add(service, service->name);
5279 void __connman_service_update_from_network(struct connman_network *network)
5281 connman_bool_t need_sort = FALSE;
5282 struct connman_service *service;
5283 connman_uint8_t strength;
5284 connman_bool_t roaming;
5285 GSequenceIter *iter;
5287 connman_bool_t stats_enable;
5289 DBG("network %p", network);
5291 service = __connman_service_lookup_from_network(network);
5292 if (service == NULL)
5295 if (service->network == NULL)
5298 name = connman_network_get_string(service->network, "Name");
5299 if (g_strcmp0(service->name, name) != 0) {
5300 g_free(service->name);
5301 service->name = g_strdup(name);
5302 connman_dbus_property_changed_basic(service->path,
5303 CONNMAN_SERVICE_INTERFACE, "Name",
5304 DBUS_TYPE_STRING, &service->name);
5307 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5308 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5310 strength = connman_network_get_strength(service->network);
5311 if (strength == service->strength)
5314 service->strength = strength;
5317 strength_changed(service);
5320 roaming = connman_network_get_bool(service->network, "Roaming");
5321 if (roaming == service->roaming)
5324 stats_enable = stats_enabled(service);
5325 if (stats_enable == TRUE)
5326 stats_stop(service);
5328 service->roaming = roaming;
5331 if (stats_enable == TRUE)
5332 stats_start(service);
5334 roaming_changed(service);
5337 if (need_sort == TRUE) {
5338 iter = g_hash_table_lookup(service_hash, service->identifier);
5340 g_sequence_sort_changed(iter, service_compare, NULL);
5344 void __connman_service_remove_from_network(struct connman_network *network)
5346 struct connman_service *service;
5348 DBG("network %p", network);
5350 service = __connman_service_lookup_from_network(network);
5351 if (service == NULL)
5354 __connman_connection_gateway_remove(service,
5355 CONNMAN_IPCONFIG_TYPE_ALL);
5357 __connman_service_put(service);
5361 * __connman_service_create_from_provider:
5362 * @provider: provider structure
5364 * Look up service by provider and if not found, create one
5366 struct connman_service *
5367 __connman_service_create_from_provider(struct connman_provider *provider)
5369 struct connman_service *service;
5370 const char *ident, *str;
5372 int index = connman_provider_get_index(provider);
5374 DBG("provider %p", provider);
5376 ident = __connman_provider_get_ident(provider);
5380 name = g_strdup_printf("vpn_%s", ident);
5381 service = service_get(name);
5384 if (service == NULL)
5387 service->type = CONNMAN_SERVICE_TYPE_VPN;
5388 service->provider = connman_provider_ref(provider);
5389 service->autoconnect = FALSE;
5390 service->userconnect = TRUE;
5392 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5394 str = connman_provider_get_string(provider, "Name");
5396 g_free(service->name);
5397 service->name = g_strdup(str);
5398 service->hidden = FALSE;
5400 g_free(service->name);
5401 service->name = NULL;
5402 service->hidden = TRUE;
5405 service->strength = 0;
5407 if (service->ipconfig_ipv4 == NULL)
5408 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5410 if (service->ipconfig_ipv6 == NULL)
5411 setup_ip6config(service, index);
5413 service_register(service);
5415 __connman_notifier_service_add(service, service->name);
5420 int __connman_service_init(void)
5424 connection = connman_dbus_get_connection();
5426 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5429 service_list = g_sequence_new(service_free);
5434 void __connman_service_cleanup(void)
5440 list = service_list;
5441 service_list = NULL;
5442 g_sequence_free(list);
5444 g_hash_table_destroy(service_hash);
5445 service_hash = NULL;
5447 g_slist_free(counter_list);
5448 counter_list = NULL;
5450 dbus_connection_unref(connection);