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
34 #define CONNECT_TIMEOUT 120
36 static DBusConnection *connection = NULL;
38 static GSequence *service_list = NULL;
39 static GHashTable *service_hash = NULL;
40 static GSList *counter_list = NULL;
42 struct connman_stats {
44 connman_bool_t enabled;
45 struct connman_stats_data data_last;
46 struct connman_stats_data data;
50 struct connman_stats_counter {
51 connman_bool_t append_all;
52 struct connman_stats stats;
53 struct connman_stats stats_roaming;
56 struct connman_service {
58 gint session_usage_count;
61 enum connman_service_type type;
62 enum connman_service_security security;
63 enum connman_service_state state;
64 enum connman_service_state state_ipv4;
65 enum connman_service_state state_ipv6;
66 enum connman_service_error error;
67 connman_uint8_t strength;
68 connman_bool_t favorite;
69 connman_bool_t immutable;
70 connman_bool_t hidden;
71 connman_bool_t ignore;
72 connman_bool_t autoconnect;
73 connman_bool_t userconnect;
78 char *agent_passphrase;
79 connman_bool_t roaming;
80 connman_bool_t login_required;
81 connman_bool_t network_created;
82 struct connman_ipconfig *ipconfig_ipv4;
83 struct connman_ipconfig *ipconfig_ipv6;
84 struct connman_network *network;
85 struct connman_provider *provider;
87 char **nameservers_config;
91 /* 802.1x settings from the config files */
96 char *client_cert_file;
97 char *private_key_file;
98 char *private_key_passphrase;
100 DBusMessage *pending;
102 struct connman_location *location;
103 struct connman_stats stats;
104 struct connman_stats stats_roaming;
105 GHashTable *counter_table;
106 enum connman_service_proxy_method proxy;
107 enum connman_service_proxy_method proxy_config;
114 static void append_path(gpointer value, gpointer user_data)
116 struct connman_service *service = value;
117 DBusMessageIter *iter = user_data;
119 if (service->path == NULL || service->hidden == TRUE)
122 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
126 void __connman_service_list(DBusMessageIter *iter, void *user_data)
128 if (service_list == NULL)
131 g_sequence_foreach(service_list, append_path, iter);
136 struct connman_service *service;
139 static void compare_path(gpointer value, gpointer user_data)
141 struct connman_service *service = value;
142 struct find_data *data = user_data;
144 if (data->service != NULL)
147 if (g_strcmp0(service->path, data->path) == 0)
148 data->service = service;
151 static struct connman_service *find_service(const char *path)
153 struct find_data data = { .path = path, .service = NULL };
155 DBG("path %s", path);
157 g_sequence_foreach(service_list, compare_path, &data);
162 const char *__connman_service_type2string(enum connman_service_type type)
165 case CONNMAN_SERVICE_TYPE_UNKNOWN:
167 case CONNMAN_SERVICE_TYPE_SYSTEM:
169 case CONNMAN_SERVICE_TYPE_ETHERNET:
171 case CONNMAN_SERVICE_TYPE_WIFI:
173 case CONNMAN_SERVICE_TYPE_WIMAX:
175 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
177 case CONNMAN_SERVICE_TYPE_CELLULAR:
179 case CONNMAN_SERVICE_TYPE_GPS:
181 case CONNMAN_SERVICE_TYPE_VPN:
183 case CONNMAN_SERVICE_TYPE_GADGET:
190 static const char *security2string(enum connman_service_security security)
193 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
195 case CONNMAN_SERVICE_SECURITY_NONE:
197 case CONNMAN_SERVICE_SECURITY_WEP:
199 case CONNMAN_SERVICE_SECURITY_PSK:
201 case CONNMAN_SERVICE_SECURITY_8021X:
203 case CONNMAN_SERVICE_SECURITY_WPA:
205 case CONNMAN_SERVICE_SECURITY_RSN:
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)
302 const char *ident = "default";
304 GError *error = NULL;
305 gchar *pathname, *data = NULL;
308 connman_bool_t autoconnect;
309 unsigned int ssid_len;
312 DBG("service %p", service);
317 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
318 if (pathname == NULL)
321 keyfile = g_key_file_new();
323 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
330 if (g_key_file_load_from_data(keyfile, data, length,
338 switch (service->type) {
339 case CONNMAN_SERVICE_TYPE_UNKNOWN:
340 case CONNMAN_SERVICE_TYPE_SYSTEM:
341 case CONNMAN_SERVICE_TYPE_ETHERNET:
342 case CONNMAN_SERVICE_TYPE_GPS:
343 case CONNMAN_SERVICE_TYPE_VPN:
344 case CONNMAN_SERVICE_TYPE_GADGET:
346 case CONNMAN_SERVICE_TYPE_WIFI:
347 if (service->name == NULL) {
350 name = g_key_file_get_string(keyfile,
351 service->identifier, "Name", NULL);
353 g_free(service->name);
354 service->name = name;
357 if (service->network != NULL)
358 connman_network_set_name(service->network,
362 if (service->network &&
363 connman_network_get_blob(service->network,
364 "WiFi.SSID", &ssid_len) == NULL) {
367 hex_ssid = g_key_file_get_string(keyfile,
371 if (hex_ssid != NULL) {
373 unsigned int i, j = 0, hex;
374 size_t hex_ssid_len = strlen(hex_ssid);
376 ssid = g_try_malloc0(hex_ssid_len / 2);
383 for (i = 0; i < hex_ssid_len; i += 2) {
384 sscanf(hex_ssid + i, "%02x", &hex);
388 connman_network_set_blob(service->network,
389 "WiFi.SSID", ssid, hex_ssid_len / 2);
396 case CONNMAN_SERVICE_TYPE_WIMAX:
397 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
398 case CONNMAN_SERVICE_TYPE_CELLULAR:
399 service->favorite = g_key_file_get_boolean(keyfile,
400 service->identifier, "Favorite", NULL);
402 autoconnect = g_key_file_get_boolean(keyfile,
403 service->identifier, "AutoConnect", &error);
405 service->autoconnect = autoconnect;
406 g_clear_error(&error);
408 str = g_key_file_get_string(keyfile,
409 service->identifier, "Failure", NULL);
411 if (service->favorite == FALSE)
412 service->state_ipv4 = service->state_ipv6 =
413 CONNMAN_SERVICE_STATE_FAILURE;
414 service->error = string2error(str);
419 str = g_key_file_get_string(keyfile,
420 service->identifier, "Modified", NULL);
422 g_time_val_from_iso8601(str, &service->modified);
426 str = g_key_file_get_string(keyfile,
427 service->identifier, "Passphrase", NULL);
429 g_free(service->passphrase);
430 service->passphrase = str;
433 if (service->ipconfig_ipv4 != NULL)
434 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
435 service->identifier, "IPv4.");
437 if (service->ipconfig_ipv6 != NULL)
438 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
439 service->identifier, "IPv6.");
441 service->nameservers_config = g_key_file_get_string_list(keyfile,
442 service->identifier, "Nameservers", &length, NULL);
443 if (service->nameservers_config != NULL && length == 0) {
444 g_strfreev(service->nameservers_config);
445 service->nameservers_config = NULL;
448 service->domains = g_key_file_get_string_list(keyfile,
449 service->identifier, "Domains", &length, NULL);
450 if (service->domains != NULL && length == 0) {
451 g_strfreev(service->domains);
452 service->domains = NULL;
455 str = g_key_file_get_string(keyfile,
456 service->identifier, "Proxy.Method", NULL);
458 service->proxy_config = string2proxymethod(str);
462 service->proxies = g_key_file_get_string_list(keyfile,
463 service->identifier, "Proxy.Servers", &length, NULL);
464 if (service->proxies != NULL && length == 0) {
465 g_strfreev(service->proxies);
466 service->proxies = NULL;
469 service->excludes = g_key_file_get_string_list(keyfile,
470 service->identifier, "Proxy.Excludes", &length, NULL);
471 if (service->excludes != NULL && length == 0) {
472 g_strfreev(service->excludes);
473 service->excludes = NULL;
476 str = g_key_file_get_string(keyfile,
477 service->identifier, "Proxy.URL", NULL);
479 g_free(service->pac);
484 g_key_file_free(keyfile);
489 static int service_save(struct connman_service *service)
491 const char *ident = "default";
493 gchar *pathname, *data = NULL;
496 const char *cst_str = NULL;
499 DBG("service %p", service);
504 pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident);
505 if (pathname == NULL)
508 keyfile = g_key_file_new();
510 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
514 if (g_key_file_load_from_data(keyfile, data, length,
522 if (service->name != NULL)
523 g_key_file_set_string(keyfile, service->identifier,
524 "Name", service->name);
526 switch (service->type) {
527 case CONNMAN_SERVICE_TYPE_UNKNOWN:
528 case CONNMAN_SERVICE_TYPE_SYSTEM:
529 case CONNMAN_SERVICE_TYPE_ETHERNET:
530 case CONNMAN_SERVICE_TYPE_GPS:
531 case CONNMAN_SERVICE_TYPE_VPN:
532 case CONNMAN_SERVICE_TYPE_GADGET:
534 case CONNMAN_SERVICE_TYPE_WIFI:
535 if (service->network) {
536 const unsigned char *ssid;
537 unsigned int ssid_len = 0;
539 ssid = connman_network_get_blob(service->network,
540 "WiFi.SSID", &ssid_len);
542 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
543 char *identifier = service->identifier;
547 str = g_string_sized_new(ssid_len * 2);
553 for (i = 0; i < ssid_len; i++)
554 g_string_append_printf(str,
557 g_key_file_set_string(keyfile, identifier,
560 g_string_free(str, TRUE);
565 case CONNMAN_SERVICE_TYPE_WIMAX:
566 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
567 case CONNMAN_SERVICE_TYPE_CELLULAR:
568 g_key_file_set_boolean(keyfile, service->identifier,
569 "Favorite", service->favorite);
571 if (service->favorite == TRUE)
572 g_key_file_set_boolean(keyfile, service->identifier,
573 "AutoConnect", service->autoconnect);
575 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
576 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
577 const char *failure = error2string(service->error);
579 g_key_file_set_string(keyfile,
583 g_key_file_remove_key(keyfile, service->identifier,
589 str = g_time_val_to_iso8601(&service->modified);
591 g_key_file_set_string(keyfile, service->identifier,
596 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
597 g_key_file_set_string(keyfile, service->identifier,
598 "Passphrase", service->passphrase);
600 g_key_file_remove_key(keyfile, service->identifier,
603 if (service->ipconfig_ipv4 != NULL)
604 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
605 service->identifier, "IPv4.");
607 if (service->ipconfig_ipv6 != NULL)
608 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
609 service->identifier, "IPv6.");
611 if (service->nameservers_config != NULL) {
612 guint len = g_strv_length(service->nameservers_config);
614 g_key_file_set_string_list(keyfile, service->identifier,
616 (const gchar **) service->nameservers_config, len);
618 g_key_file_remove_key(keyfile, service->identifier,
619 "Nameservers", NULL);
621 if (service->domains != NULL) {
622 guint len = g_strv_length(service->domains);
624 g_key_file_set_string_list(keyfile, service->identifier,
626 (const gchar **) service->domains, len);
628 g_key_file_remove_key(keyfile, service->identifier,
631 cst_str = proxymethod2string(service->proxy_config);
633 g_key_file_set_string(keyfile, service->identifier,
634 "Proxy.Method", cst_str);
636 if (service->proxies != NULL) {
637 guint len = g_strv_length(service->proxies);
639 g_key_file_set_string_list(keyfile, service->identifier,
641 (const gchar **) service->proxies, len);
643 g_key_file_remove_key(keyfile, service->identifier,
644 "Proxy.Servers", NULL);
646 if (service->excludes != NULL) {
647 guint len = g_strv_length(service->excludes);
649 g_key_file_set_string_list(keyfile, service->identifier,
651 (const gchar **) service->excludes, len);
653 g_key_file_remove_key(keyfile, service->identifier,
654 "Proxy.Excludes", NULL);
656 if (service->pac != NULL && strlen(service->pac) > 0)
657 g_key_file_set_string(keyfile, service->identifier,
658 "Proxy.URL", service->pac);
660 g_key_file_remove_key(keyfile, service->identifier,
663 data = g_key_file_to_data(keyfile, &length, NULL);
665 if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
666 connman_error("Failed to store service information");
671 g_key_file_free(keyfile);
678 static guint changed_timeout = 0;
680 static gboolean notify_services_changed(gpointer user_data)
684 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
685 CONNMAN_MANAGER_INTERFACE, "Services",
686 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
692 static void services_changed(gboolean delayed)
696 if (changed_timeout > 0) {
697 g_source_remove(changed_timeout);
701 if (__connman_connection_update_gateway() == TRUE) {
702 notify_services_changed(NULL);
706 if (delayed == FALSE) {
707 notify_services_changed(NULL);
711 changed_timeout = g_timeout_add_seconds(1, notify_services_changed,
715 static enum connman_service_state combine_state(
716 enum connman_service_state state_a,
717 enum connman_service_state state_b)
719 enum connman_service_state result;
721 if (state_a == state_b) {
726 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
731 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
736 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
741 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
746 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
747 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION ||
748 state_b == CONNMAN_SERVICE_STATE_ONLINE ||
749 state_b == CONNMAN_SERVICE_STATE_READY)
756 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
757 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION ||
758 state_a == CONNMAN_SERVICE_STATE_ONLINE ||
759 state_a == CONNMAN_SERVICE_STATE_READY)
766 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
767 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
768 state_b == CONNMAN_SERVICE_STATE_READY)
775 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
776 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
777 state_a == CONNMAN_SERVICE_STATE_READY)
784 if (state_a == CONNMAN_SERVICE_STATE_READY) {
785 if (state_b == CONNMAN_SERVICE_STATE_ONLINE ||
786 state_b == CONNMAN_SERVICE_STATE_DISCONNECT)
793 if (state_b == CONNMAN_SERVICE_STATE_READY) {
794 if (state_a == CONNMAN_SERVICE_STATE_ONLINE ||
795 state_a == CONNMAN_SERVICE_STATE_DISCONNECT)
802 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
803 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT)
810 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
811 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT)
818 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
823 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
828 result = CONNMAN_SERVICE_STATE_FAILURE;
834 static connman_bool_t is_connecting_state(struct connman_service *service,
835 enum connman_service_state state)
838 case CONNMAN_SERVICE_STATE_UNKNOWN:
839 case CONNMAN_SERVICE_STATE_IDLE:
840 case CONNMAN_SERVICE_STATE_FAILURE:
841 if (service->network != NULL)
842 return connman_network_get_connecting(service->network);
843 case CONNMAN_SERVICE_STATE_DISCONNECT:
844 case CONNMAN_SERVICE_STATE_READY:
845 case CONNMAN_SERVICE_STATE_ONLINE:
847 case CONNMAN_SERVICE_STATE_ASSOCIATION:
848 case CONNMAN_SERVICE_STATE_CONFIGURATION:
855 static connman_bool_t is_connected_state(const struct connman_service *service,
856 enum connman_service_state state)
859 case CONNMAN_SERVICE_STATE_UNKNOWN:
860 case CONNMAN_SERVICE_STATE_IDLE:
861 case CONNMAN_SERVICE_STATE_ASSOCIATION:
862 case CONNMAN_SERVICE_STATE_CONFIGURATION:
863 case CONNMAN_SERVICE_STATE_DISCONNECT:
864 case CONNMAN_SERVICE_STATE_FAILURE:
866 case CONNMAN_SERVICE_STATE_READY:
867 case CONNMAN_SERVICE_STATE_ONLINE:
874 static connman_bool_t is_connecting(struct connman_service *service)
876 return is_connecting_state(service, service->state);
879 static connman_bool_t is_connected(struct connman_service *service)
881 return is_connected_state(service, service->state);
884 static void update_nameservers(struct connman_service *service)
888 if (service->ipconfig_ipv4)
889 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
890 else if (service->ipconfig_ipv6)
891 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
898 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
899 case CONNMAN_SERVICE_STATE_UNKNOWN:
900 case CONNMAN_SERVICE_STATE_IDLE:
901 case CONNMAN_SERVICE_STATE_ASSOCIATION:
902 case CONNMAN_SERVICE_STATE_CONFIGURATION:
904 case CONNMAN_SERVICE_STATE_FAILURE:
905 case CONNMAN_SERVICE_STATE_DISCONNECT:
906 connman_resolver_remove_all(ifname);
908 case CONNMAN_SERVICE_STATE_READY:
909 case CONNMAN_SERVICE_STATE_ONLINE:
913 connman_resolver_remove_all(ifname);
915 if (service->nameservers_config != NULL) {
918 for (i = 0; service->nameservers_config[i] != NULL; i++) {
919 connman_resolver_append(ifname, NULL,
920 service->nameservers_config[i]);
922 } else if (service->nameservers != NULL) {
925 for (i = 0; service->nameservers[i] != NULL; i++) {
926 connman_resolver_append(ifname, NULL,
927 service->nameservers[i]);
931 if (service->domains != NULL) {
934 for (i = 0; service->domains[i]; i++)
935 connman_resolver_append(ifname, service->domains[i],
937 } else if (service->domainname != NULL)
938 connman_resolver_append(ifname, service->domainname, NULL);
940 connman_resolver_flush();
943 int __connman_service_nameserver_append(struct connman_service *service,
944 const char *nameserver)
948 DBG("service %p nameserver %s", service, nameserver);
950 if (nameserver == NULL)
953 if (service->nameservers != NULL) {
956 for (i = 0; service->nameservers[i] != NULL; i++)
957 if (g_strcmp0(service->nameservers[i], nameserver) == 0)
960 len = g_strv_length(service->nameservers);
961 service->nameservers = g_try_renew(char *, service->nameservers,
965 service->nameservers = g_try_new0(char *, len + 2);
968 if (service->nameservers == NULL)
971 service->nameservers[len] = g_strdup(nameserver);
972 service->nameservers[len + 1] = NULL;
974 update_nameservers(service);
979 int __connman_service_nameserver_remove(struct connman_service *service,
980 const char *nameserver)
985 DBG("service %p nameserver %s", service, nameserver);
987 if (nameserver == NULL)
990 if (service->nameservers == NULL)
993 len = g_strv_length(service->nameservers);
995 if (g_strcmp0(service->nameservers[0], nameserver) != 0)
998 g_strfreev(service->nameservers);
999 service->nameservers = NULL;
1004 servers = g_try_new0(char *, len);
1005 if (servers == NULL)
1008 for (i = 0, j = 0; i < len; i++) {
1009 if (g_strcmp0(service->nameservers[i], nameserver) != 0) {
1010 servers[j] = g_strdup(service->nameservers[i]);
1014 servers[len - 1] = NULL;
1016 g_strfreev(service->nameservers);
1017 service->nameservers = servers;
1019 update_nameservers(service);
1024 void __connman_service_nameserver_clear(struct connman_service *service)
1026 g_strfreev(service->nameservers);
1027 service->nameservers = NULL;
1029 update_nameservers(service);
1032 static void nameserver_add_routes(int index, char **nameservers,
1036 struct addrinfo hints;
1037 struct addrinfo *addr;
1039 for (i = 0; nameservers[i] != NULL; i++) {
1040 memset(&hints, 0, sizeof(struct addrinfo));
1041 hints.ai_flags = AI_NUMERICHOST;
1044 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1045 if (ret == EAI_NONAME)
1046 family = AF_INET; /* use the IPv4 as a default */
1050 family = addr->ai_family;
1052 if (family == AF_INET) {
1053 if (connman_inet_compare_subnet(index,
1054 nameservers[i]) != TRUE)
1055 connman_inet_add_host_route(index,
1056 nameservers[i], gw);
1057 } else if (family == AF_INET6)
1058 connman_inet_add_ipv6_host_route(index,
1059 nameservers[i], gw);
1065 static void nameserver_del_routes(int index, char **nameservers)
1068 struct addrinfo hints;
1069 struct addrinfo *addr;
1071 for (i = 0; nameservers[i] != NULL; i++) {
1072 memset(&hints, 0, sizeof(struct addrinfo));
1073 hints.ai_flags = AI_NUMERICHOST;
1076 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1077 if (ret == EAI_NONAME)
1078 family = AF_INET; /* use the IPv4 as a default */
1082 family = addr->ai_family;
1084 if (family == AF_INET)
1085 connman_inet_del_host_route(index, nameservers[i]);
1086 else if (family == AF_INET6)
1087 connman_inet_del_ipv6_host_route(index,
1094 void __connman_service_nameserver_add_routes(struct connman_service *service,
1099 if (service == NULL)
1102 if (service->network != NULL)
1103 index = connman_network_get_index(service->network);
1104 else if (service->provider != NULL)
1105 index = connman_provider_get_index(service->provider);
1107 if (service->nameservers_config != NULL) {
1109 * Configured nameserver takes preference over the
1110 * discoverd nameserver gathered from DHCP, VPN, etc.
1112 nameserver_add_routes(index, service->nameservers_config, gw);
1113 } else if (service->nameservers != NULL) {
1115 * We add nameservers host routes for nameservers that
1116 * are not on our subnet. For those who are, the subnet
1117 * route will be installed by the time the dns proxy code
1118 * tries to reach them. The subnet route is installed
1119 * when setting the interface IP address.
1121 nameserver_add_routes(index, service->nameservers, gw);
1125 void __connman_service_nameserver_del_routes(struct connman_service *service)
1129 if (service == NULL)
1132 if (service->network != NULL)
1133 index = connman_network_get_index(service->network);
1134 else if (service->provider != NULL)
1135 index = connman_provider_get_index(service->provider);
1137 if (service->nameservers_config != NULL)
1138 nameserver_del_routes(index, service->nameservers_config);
1139 else if (service->nameservers != NULL)
1140 nameserver_del_routes(index, service->nameservers);
1143 static struct connman_stats *stats_get(struct connman_service *service)
1145 if (service->roaming == TRUE)
1146 return &service->stats_roaming;
1148 return &service->stats;
1151 static connman_bool_t stats_enabled(struct connman_service *service)
1153 struct connman_stats *stats = stats_get(service);
1155 return stats->enabled;
1158 static void stats_start(struct connman_service *service)
1160 struct connman_stats *stats = stats_get(service);
1162 DBG("service %p", service);
1164 if (stats->timer == NULL)
1167 stats->enabled = TRUE;
1168 stats->data_last.time = stats->data.time;
1170 g_timer_start(stats->timer);
1173 static void stats_stop(struct connman_service *service)
1175 struct connman_stats *stats = stats_get(service);
1176 unsigned int seconds;
1178 DBG("service %p", service);
1180 if (stats->timer == NULL)
1183 if (stats->enabled == FALSE)
1186 g_timer_stop(stats->timer);
1188 seconds = g_timer_elapsed(stats->timer, NULL);
1189 stats->data.time = stats->data_last.time + seconds;
1191 stats->enabled = FALSE;
1194 static void reset_stats(struct connman_service *service)
1196 DBG("service %p", service);
1199 service->stats.valid = FALSE;
1201 service->stats.data.rx_packets = 0;
1202 service->stats.data.tx_packets = 0;
1203 service->stats.data.rx_bytes = 0;
1204 service->stats.data.tx_bytes = 0;
1205 service->stats.data.rx_errors = 0;
1206 service->stats.data.tx_errors = 0;
1207 service->stats.data.rx_dropped = 0;
1208 service->stats.data.tx_dropped = 0;
1209 service->stats.data.time = 0;
1210 service->stats.data_last.time = 0;
1212 g_timer_reset(service->stats.timer);
1215 service->stats_roaming.valid = FALSE;
1217 service->stats_roaming.data.rx_packets = 0;
1218 service->stats_roaming.data.tx_packets = 0;
1219 service->stats_roaming.data.rx_bytes = 0;
1220 service->stats_roaming.data.tx_bytes = 0;
1221 service->stats_roaming.data.rx_errors = 0;
1222 service->stats_roaming.data.tx_errors = 0;
1223 service->stats_roaming.data.rx_dropped = 0;
1224 service->stats_roaming.data.tx_dropped = 0;
1225 service->stats_roaming.data.time = 0;
1226 service->stats_roaming.data_last.time = 0;
1228 g_timer_reset(service->stats_roaming.timer);
1231 static struct connman_service *get_default(void)
1233 struct connman_service *service;
1234 GSequenceIter *iter;
1236 iter = g_sequence_get_begin_iter(service_list);
1238 if (g_sequence_iter_is_end(iter) == TRUE)
1241 service = g_sequence_get(iter);
1243 if (is_connected(service) == FALSE)
1249 static void default_changed(void)
1251 struct connman_service *service = get_default();
1253 __connman_notifier_default_changed(service);
1256 const char *__connman_service_default(void)
1258 struct connman_service *service;
1260 service = get_default();
1261 if (service == NULL)
1264 return __connman_service_type2string(service->type);
1267 static void state_changed(struct connman_service *service)
1271 __connman_notifier_service_state_changed(service, service->state);
1273 str = state2string(service->state);
1277 connman_dbus_property_changed_basic(service->path,
1278 CONNMAN_SERVICE_INTERFACE, "State",
1279 DBUS_TYPE_STRING, &str);
1282 static void strength_changed(struct connman_service *service)
1284 if (service->strength == 0)
1287 connman_dbus_property_changed_basic(service->path,
1288 CONNMAN_SERVICE_INTERFACE, "Strength",
1289 DBUS_TYPE_BYTE, &service->strength);
1292 static void favorite_changed(struct connman_service *service)
1294 if (service->path == NULL)
1297 connman_dbus_property_changed_basic(service->path,
1298 CONNMAN_SERVICE_INTERFACE, "Favorite",
1299 DBUS_TYPE_BOOLEAN, &service->favorite);
1302 static void immutable_changed(struct connman_service *service)
1304 if (service->path == NULL)
1307 connman_dbus_property_changed_basic(service->path,
1308 CONNMAN_SERVICE_INTERFACE, "Immutable",
1309 DBUS_TYPE_BOOLEAN, &service->immutable);
1312 static void roaming_changed(struct connman_service *service)
1314 if (service->path == NULL)
1317 connman_dbus_property_changed_basic(service->path,
1318 CONNMAN_SERVICE_INTERFACE, "Roaming",
1319 DBUS_TYPE_BOOLEAN, &service->roaming);
1322 static void autoconnect_changed(struct connman_service *service)
1324 if (service->path == NULL)
1327 connman_dbus_property_changed_basic(service->path,
1328 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
1329 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1332 static void passphrase_changed(struct connman_service *service)
1334 dbus_bool_t required;
1336 switch (service->type) {
1337 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1338 case CONNMAN_SERVICE_TYPE_SYSTEM:
1339 case CONNMAN_SERVICE_TYPE_ETHERNET:
1340 case CONNMAN_SERVICE_TYPE_WIMAX:
1341 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1342 case CONNMAN_SERVICE_TYPE_CELLULAR:
1343 case CONNMAN_SERVICE_TYPE_GPS:
1344 case CONNMAN_SERVICE_TYPE_VPN:
1345 case CONNMAN_SERVICE_TYPE_GADGET:
1347 case CONNMAN_SERVICE_TYPE_WIFI:
1350 switch (service->security) {
1351 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1352 case CONNMAN_SERVICE_SECURITY_NONE:
1354 case CONNMAN_SERVICE_SECURITY_WEP:
1355 case CONNMAN_SERVICE_SECURITY_PSK:
1356 case CONNMAN_SERVICE_SECURITY_WPA:
1357 case CONNMAN_SERVICE_SECURITY_RSN:
1358 if (service->passphrase == NULL)
1361 case CONNMAN_SERVICE_SECURITY_8021X:
1367 connman_dbus_property_changed_basic(service->path,
1368 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
1369 DBUS_TYPE_BOOLEAN, &required);
1372 static void login_changed(struct connman_service *service)
1374 dbus_bool_t required = service->login_required;
1376 if (service->path == NULL)
1379 connman_dbus_property_changed_basic(service->path,
1380 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
1381 DBUS_TYPE_BOOLEAN, &required);
1384 static void append_security(DBusMessageIter *iter, void *user_data)
1386 struct connman_service *service = user_data;
1389 str = security2string(service->security);
1391 dbus_message_iter_append_basic(iter,
1392 DBUS_TYPE_STRING, &str);
1395 if (service->wps == TRUE)
1396 dbus_message_iter_append_basic(iter,
1397 DBUS_TYPE_STRING, &str);
1400 static void append_ethernet(DBusMessageIter *iter, void *user_data)
1402 struct connman_service *service = user_data;
1404 if (service->ipconfig_ipv4 != NULL)
1405 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
1407 else if (service->ipconfig_ipv6 != NULL)
1408 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
1412 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1414 struct connman_service *service = user_data;
1416 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1417 state2string(service->state_ipv4));
1419 if (is_connected_state(service, service->state_ipv4) == FALSE)
1422 if (service->ipconfig_ipv4 != NULL)
1423 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1426 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1428 struct connman_service *service = user_data;
1430 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1431 state2string(service->state_ipv6));
1433 if (is_connected_state(service, service->state_ipv6) == FALSE)
1436 if (service->ipconfig_ipv6 != NULL)
1437 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1438 service->ipconfig_ipv4);
1441 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1443 struct connman_service *service = user_data;
1445 if (service->ipconfig_ipv4 != NULL)
1446 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1450 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1452 struct connman_service *service = user_data;
1454 if (service->ipconfig_ipv6 != NULL)
1455 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1459 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1464 servers = *nameservers;
1466 for (i = 0; servers[i] != NULL; i++) {
1467 dbus_message_iter_append_basic(iter,
1468 DBUS_TYPE_STRING, &servers[i]);
1472 static void append_dns(DBusMessageIter *iter, void *user_data)
1474 struct connman_service *service = user_data;
1476 if (is_connected(service) == FALSE)
1479 if (service->nameservers_config != NULL) {
1480 append_nameserver(iter, &service->nameservers_config);
1482 } else if (service->nameservers != NULL) {
1483 append_nameserver(iter, &service->nameservers);
1488 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1490 struct connman_service *service = user_data;
1493 if (service->nameservers_config == NULL)
1496 for (i = 0; service->nameservers_config[i]; i++) {
1497 dbus_message_iter_append_basic(iter,
1499 &service->nameservers_config[i]);
1503 static void append_domain(DBusMessageIter *iter, void *user_data)
1505 struct connman_service *service = user_data;
1507 if (is_connected(service) == FALSE &&
1508 is_connecting(service) == FALSE)
1511 if (service->domainname == NULL)
1514 dbus_message_iter_append_basic(iter,
1515 DBUS_TYPE_STRING, &service->domainname);
1518 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1520 struct connman_service *service = user_data;
1523 if (service->domains == NULL)
1526 for (i = 0; service->domains[i]; i++)
1527 dbus_message_iter_append_basic(iter,
1528 DBUS_TYPE_STRING, &service->domains[i]);
1531 static void append_proxies(DBusMessageIter *iter, void *user_data)
1533 struct connman_service *service = user_data;
1536 if (service->proxies == NULL)
1539 for (i = 0; service->proxies[i]; i++)
1540 dbus_message_iter_append_basic(iter,
1541 DBUS_TYPE_STRING, &service->proxies[i]);
1544 static void append_excludes(DBusMessageIter *iter, void *user_data)
1546 struct connman_service *service = user_data;
1549 if (service->excludes == NULL)
1552 for (i = 0; service->excludes[i]; i++)
1553 dbus_message_iter_append_basic(iter,
1554 DBUS_TYPE_STRING, &service->excludes[i]);
1557 static void append_proxy(DBusMessageIter *iter, void *user_data)
1559 struct connman_service *service = user_data;
1560 enum connman_service_proxy_method proxy;
1561 const char *pac = NULL;
1562 const char *method = proxymethod2string(
1563 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1567 if (is_connected(service) == FALSE)
1570 proxy = connman_service_get_proxy_method(service);
1573 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1575 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1577 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1578 connman_dbus_dict_append_array(iter, "Servers",
1579 DBUS_TYPE_STRING, append_proxies,
1582 connman_dbus_dict_append_array(iter, "Excludes",
1583 DBUS_TYPE_STRING, append_excludes,
1586 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1587 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1588 if (service->ipconfig_ipv4 != NULL)
1589 pac = __connman_ipconfig_get_proxy_autoconfig(
1590 service->ipconfig_ipv4);
1591 else if (service->ipconfig_ipv6 != NULL)
1592 pac = __connman_ipconfig_get_proxy_autoconfig(
1593 service->ipconfig_ipv6);
1595 if (service->pac == NULL && pac == NULL)
1598 if (service->pac != NULL)
1601 connman_dbus_dict_append_basic(iter, "URL",
1602 DBUS_TYPE_STRING, &pac);
1606 method = proxymethod2string(proxy);
1609 connman_dbus_dict_append_basic(iter, "Method",
1610 DBUS_TYPE_STRING, &method);
1613 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1615 struct connman_service *service = user_data;
1618 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1621 switch (service->proxy_config) {
1622 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1624 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1626 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1627 if (service->proxies != NULL)
1628 connman_dbus_dict_append_array(iter, "Servers",
1630 append_proxies, service);
1632 if (service->excludes != NULL)
1633 connman_dbus_dict_append_array(iter, "Excludes",
1635 append_excludes, service);
1637 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1638 if (service->pac != NULL)
1639 connman_dbus_dict_append_basic(iter, "URL",
1640 DBUS_TYPE_STRING, &service->pac);
1644 method = proxymethod2string(service->proxy_config);
1646 connman_dbus_dict_append_basic(iter, "Method",
1647 DBUS_TYPE_STRING, &method);
1650 static void append_provider(DBusMessageIter *iter, void *user_data)
1652 struct connman_service *service = user_data;
1654 DBG("%p %p", service, service->provider);
1656 if (is_connected(service) == FALSE)
1659 if (service->provider != NULL)
1660 __connman_provider_append_properties(service->provider, iter);
1664 static void settings_changed(struct connman_service *service,
1665 struct connman_ipconfig *ipconfig)
1667 connman_dbus_property_changed_dict(service->path,
1668 CONNMAN_SERVICE_INTERFACE, "IPv4",
1669 append_ipv4, service);
1671 connman_dbus_property_changed_dict(service->path,
1672 CONNMAN_SERVICE_INTERFACE, "IPv6",
1673 append_ipv6, service);
1675 __connman_notifier_ipconfig_changed(service, ipconfig);
1678 static void ipv4_configuration_changed(struct connman_service *service)
1680 connman_dbus_property_changed_dict(service->path,
1681 CONNMAN_SERVICE_INTERFACE,
1682 "IPv4.Configuration",
1687 static void ipv6_configuration_changed(struct connman_service *service)
1689 connman_dbus_property_changed_dict(service->path,
1690 CONNMAN_SERVICE_INTERFACE,
1691 "IPv6.Configuration",
1696 static void dns_changed(struct connman_service *service)
1698 connman_dbus_property_changed_array(service->path,
1699 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1700 DBUS_TYPE_STRING, append_dns, service);
1703 static void dns_configuration_changed(struct connman_service *service)
1705 connman_dbus_property_changed_array(service->path,
1706 CONNMAN_SERVICE_INTERFACE,
1707 "Nameservers.Configuration",
1708 DBUS_TYPE_STRING, append_dnsconfig, service);
1710 dns_changed(service);
1713 static void domain_changed(struct connman_service *service)
1715 connman_dbus_property_changed_array(service->path,
1716 CONNMAN_SERVICE_INTERFACE, "Domains",
1717 DBUS_TYPE_STRING, append_domain, service);
1720 static void domain_configuration_changed(struct connman_service *service)
1722 connman_dbus_property_changed_array(service->path,
1723 CONNMAN_SERVICE_INTERFACE,
1724 "Domains.Configuration",
1725 DBUS_TYPE_STRING, append_domainconfig, service);
1728 static void proxy_changed(struct connman_service *service)
1730 connman_dbus_property_changed_dict(service->path,
1731 CONNMAN_SERVICE_INTERFACE, "Proxy",
1732 append_proxy, service);
1735 static void proxy_configuration_changed(struct connman_service *service)
1737 connman_dbus_property_changed_dict(service->path,
1738 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1739 append_proxyconfig, service);
1741 proxy_changed(service);
1744 static void link_changed(struct connman_service *service)
1746 connman_dbus_property_changed_dict(service->path,
1747 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1748 append_ethernet, service);
1751 static void stats_append_counters(DBusMessageIter *dict,
1752 struct connman_stats_data *stats,
1753 struct connman_stats_data *counters,
1754 connman_bool_t append_all)
1756 if (counters->rx_packets != stats->rx_packets || append_all) {
1757 counters->rx_packets = stats->rx_packets;
1758 connman_dbus_dict_append_basic(dict, "RX.Packets",
1759 DBUS_TYPE_UINT32, &stats->rx_packets);
1762 if (counters->tx_packets != stats->tx_packets || append_all) {
1763 counters->tx_packets = stats->tx_packets;
1764 connman_dbus_dict_append_basic(dict, "TX.Packets",
1765 DBUS_TYPE_UINT32, &stats->tx_packets);
1768 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1769 counters->rx_bytes = stats->rx_bytes;
1770 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1771 DBUS_TYPE_UINT32, &stats->rx_bytes);
1774 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1775 counters->tx_bytes = stats->tx_bytes;
1776 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1777 DBUS_TYPE_UINT32, &stats->tx_bytes);
1780 if (counters->rx_errors != stats->rx_errors || append_all) {
1781 counters->rx_errors = stats->rx_errors;
1782 connman_dbus_dict_append_basic(dict, "RX.Errors",
1783 DBUS_TYPE_UINT32, &stats->rx_errors);
1786 if (counters->tx_errors != stats->tx_errors || append_all) {
1787 counters->tx_errors = stats->tx_errors;
1788 connman_dbus_dict_append_basic(dict, "TX.Errors",
1789 DBUS_TYPE_UINT32, &stats->tx_errors);
1792 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1793 counters->rx_dropped = stats->rx_dropped;
1794 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1795 DBUS_TYPE_UINT32, &stats->rx_dropped);
1798 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1799 counters->tx_dropped = stats->tx_dropped;
1800 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1801 DBUS_TYPE_UINT32, &stats->tx_dropped);
1804 if (counters->time != stats->time || append_all) {
1805 counters->time = stats->time;
1806 connman_dbus_dict_append_basic(dict, "Time",
1807 DBUS_TYPE_UINT32, &stats->time);
1811 static void stats_append(struct connman_service *service,
1812 const char *counter,
1813 struct connman_stats_counter *counters,
1814 connman_bool_t append_all)
1816 DBusMessageIter array, dict;
1819 DBG("service %p counter %s", service, counter);
1821 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1825 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1826 &service->path, DBUS_TYPE_INVALID);
1828 dbus_message_iter_init_append(msg, &array);
1831 connman_dbus_dict_open(&array, &dict);
1833 stats_append_counters(&dict, &service->stats.data,
1834 &counters->stats.data, append_all);
1836 connman_dbus_dict_close(&array, &dict);
1838 /* roaming counter */
1839 connman_dbus_dict_open(&array, &dict);
1841 stats_append_counters(&dict, &service->stats_roaming.data,
1842 &counters->stats_roaming.data, append_all);
1844 connman_dbus_dict_close(&array, &dict);
1846 __connman_counter_send_usage(counter, msg);
1849 static void stats_update(struct connman_service *service,
1850 unsigned int rx_packets, unsigned int tx_packets,
1851 unsigned int rx_bytes, unsigned int tx_bytes,
1852 unsigned int rx_errors, unsigned int tx_errors,
1853 unsigned int rx_dropped, unsigned int tx_dropped)
1855 struct connman_stats *stats = stats_get(service);
1856 struct connman_stats_data *data_last = &stats->data_last;
1857 struct connman_stats_data *data = &stats->data;
1858 unsigned int seconds;
1860 DBG("service %p", service);
1862 if (stats->valid == TRUE) {
1864 rx_packets - data_last->rx_packets;
1866 tx_packets - data_last->tx_packets;
1868 rx_bytes - data_last->rx_bytes;
1870 tx_bytes - data_last->tx_bytes;
1872 rx_errors - data_last->rx_errors;
1874 tx_errors - data_last->tx_errors;
1876 rx_dropped - data_last->rx_dropped;
1878 tx_dropped - data_last->tx_dropped;
1880 stats->valid = TRUE;
1883 data_last->rx_packets = rx_packets;
1884 data_last->tx_packets = tx_packets;
1885 data_last->rx_bytes = rx_bytes;
1886 data_last->tx_bytes = tx_bytes;
1887 data_last->rx_errors = rx_errors;
1888 data_last->tx_errors = tx_errors;
1889 data_last->rx_dropped = rx_dropped;
1890 data_last->tx_dropped = tx_dropped;
1892 seconds = g_timer_elapsed(stats->timer, NULL);
1893 stats->data.time = stats->data_last.time + seconds;
1896 static char *wifi_build_group_name(const unsigned char *ssid,
1897 unsigned int ssid_len,
1899 const char *security)
1904 /* the last 3 is for the 2 '_' and '\0' */
1905 str = g_string_sized_new((ssid_len * 2) + strlen(mode)
1906 + strlen(security) + 3);
1910 for (i = 0; i < ssid_len; i++)
1911 g_string_append_printf(str, "%02x", ssid[i]);
1913 g_string_append_printf(str, "_%s_%s", mode, security);
1915 return g_string_free(str, FALSE);
1918 void __connman_service_notify(struct connman_service *service,
1919 unsigned int rx_packets, unsigned int tx_packets,
1920 unsigned int rx_bytes, unsigned int tx_bytes,
1921 unsigned int rx_errors, unsigned int tx_errors,
1922 unsigned int rx_dropped, unsigned int tx_dropped)
1924 GHashTableIter iter;
1925 gpointer key, value;
1926 const char *counter;
1927 struct connman_stats_counter *counters;
1928 struct connman_stats_data *data;
1931 if (service == NULL)
1934 if (is_connected(service) == FALSE)
1937 stats_update(service,
1938 rx_packets, tx_packets,
1940 rx_errors, tx_errors,
1941 rx_dropped, tx_dropped);
1943 data = &stats_get(service)->data;
1944 err = __connman_stats_update(service, service->roaming, data);
1946 connman_error("Failed to store statistics for %s",
1947 service->identifier);
1949 g_hash_table_iter_init(&iter, service->counter_table);
1950 while (g_hash_table_iter_next(&iter, &key, &value)) {
1954 stats_append(service, counter, counters, counters->append_all);
1955 counters->append_all = FALSE;
1959 int __connman_service_counter_register(const char *counter)
1961 struct connman_service *service;
1962 GSequenceIter *iter;
1963 struct connman_stats_counter *counters;
1965 DBG("counter %s", counter);
1967 counter_list = g_slist_append(counter_list, (gpointer)counter);
1969 iter = g_sequence_get_begin_iter(service_list);
1971 while (g_sequence_iter_is_end(iter) == FALSE) {
1972 service = g_sequence_get(iter);
1974 counters = g_try_new0(struct connman_stats_counter, 1);
1975 if (counters == NULL)
1978 counters->append_all = TRUE;
1980 g_hash_table_replace(service->counter_table, (gpointer)counter,
1983 iter = g_sequence_iter_next(iter);
1989 void __connman_service_counter_unregister(const char *counter)
1991 struct connman_service *service;
1992 GSequenceIter *iter;
1994 DBG("counter %s", counter);
1996 iter = g_sequence_get_begin_iter(service_list);
1998 while (g_sequence_iter_is_end(iter) == FALSE) {
1999 service = g_sequence_get(iter);
2001 g_hash_table_remove(service->counter_table, counter);
2003 iter = g_sequence_iter_next(iter);
2006 counter_list = g_slist_remove(counter_list, counter);
2009 GSequence *__connman_service_get_list(struct connman_session *session,
2010 service_match_cb service_match,
2011 create_service_entry_cb create_service_entry,
2012 GDestroyNotify destroy_service_entry)
2015 GSequenceIter *iter;
2016 struct connman_service *service;
2017 struct service_entry *entry;
2019 list = g_sequence_new(destroy_service_entry);
2023 iter = g_sequence_get_begin_iter(service_list);
2025 while (g_sequence_iter_is_end(iter) == FALSE) {
2026 service = g_sequence_get(iter);
2028 if (service_match(session, service) == TRUE) {
2029 entry = create_service_entry(service, service->name,
2034 g_sequence_append(list, entry);
2037 iter = g_sequence_iter_next(iter);
2043 void __connman_service_session_inc(struct connman_service *service)
2045 DBG("service %p ref count %d", service,
2046 g_atomic_int_get(&service->session_usage_count) + 1);
2048 g_atomic_int_inc(&service->session_usage_count);
2051 connman_bool_t __connman_service_session_dec(struct connman_service *service)
2053 DBG("service %p ref count %d", service,
2054 g_atomic_int_get(&service->session_usage_count) - 1);
2056 if (g_atomic_int_dec_and_test(&service->session_usage_count) == FALSE)
2062 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
2063 struct connman_service *service)
2065 dbus_bool_t required;
2068 str = __connman_service_type2string(service->type);
2070 connman_dbus_dict_append_basic(dict, "Type",
2071 DBUS_TYPE_STRING, &str);
2073 connman_dbus_dict_append_array(dict, "Security",
2074 DBUS_TYPE_STRING, append_security, service);
2076 str = state2string(service->state);
2078 connman_dbus_dict_append_basic(dict, "State",
2079 DBUS_TYPE_STRING, &str);
2081 str = error2string(service->error);
2083 connman_dbus_dict_append_basic(dict, "Error",
2084 DBUS_TYPE_STRING, &str);
2086 if (service->strength > 0)
2087 connman_dbus_dict_append_basic(dict, "Strength",
2088 DBUS_TYPE_BYTE, &service->strength);
2090 connman_dbus_dict_append_basic(dict, "Favorite",
2091 DBUS_TYPE_BOOLEAN, &service->favorite);
2093 connman_dbus_dict_append_basic(dict, "Immutable",
2094 DBUS_TYPE_BOOLEAN, &service->immutable);
2096 if (service->favorite == TRUE)
2097 connman_dbus_dict_append_basic(dict, "AutoConnect",
2098 DBUS_TYPE_BOOLEAN, &service->autoconnect);
2100 connman_dbus_dict_append_basic(dict, "AutoConnect",
2101 DBUS_TYPE_BOOLEAN, &service->favorite);
2103 if (service->name != NULL)
2104 connman_dbus_dict_append_basic(dict, "Name",
2105 DBUS_TYPE_STRING, &service->name);
2107 connman_dbus_dict_append_basic(dict, "LoginRequired",
2108 DBUS_TYPE_BOOLEAN, &service->login_required);
2110 switch (service->type) {
2111 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2112 case CONNMAN_SERVICE_TYPE_SYSTEM:
2113 case CONNMAN_SERVICE_TYPE_GPS:
2114 case CONNMAN_SERVICE_TYPE_VPN:
2115 case CONNMAN_SERVICE_TYPE_GADGET:
2117 case CONNMAN_SERVICE_TYPE_CELLULAR:
2118 connman_dbus_dict_append_basic(dict, "Roaming",
2119 DBUS_TYPE_BOOLEAN, &service->roaming);
2122 connman_dbus_dict_append_basic(dict, "SetupRequired",
2123 DBUS_TYPE_BOOLEAN, &required);
2124 connman_dbus_dict_append_dict(dict, "Ethernet",
2125 append_ethernet, service);
2127 case CONNMAN_SERVICE_TYPE_WIFI:
2128 if (service->passphrase != NULL && limited == FALSE)
2129 connman_dbus_dict_append_basic(dict, "Passphrase",
2130 DBUS_TYPE_STRING, &service->passphrase);
2134 switch (service->security) {
2135 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2136 case CONNMAN_SERVICE_SECURITY_NONE:
2138 case CONNMAN_SERVICE_SECURITY_WEP:
2139 case CONNMAN_SERVICE_SECURITY_PSK:
2140 case CONNMAN_SERVICE_SECURITY_WPA:
2141 case CONNMAN_SERVICE_SECURITY_RSN:
2142 if (service->passphrase == NULL)
2145 case CONNMAN_SERVICE_SECURITY_8021X:
2149 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
2150 DBUS_TYPE_BOOLEAN, &required);
2153 case CONNMAN_SERVICE_TYPE_ETHERNET:
2154 case CONNMAN_SERVICE_TYPE_WIMAX:
2155 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2156 connman_dbus_dict_append_dict(dict, "Ethernet",
2157 append_ethernet, service);
2161 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
2163 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
2164 append_ipv4config, service);
2166 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
2168 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
2169 append_ipv6config, service);
2171 connman_dbus_dict_append_array(dict, "Nameservers",
2172 DBUS_TYPE_STRING, append_dns, service);
2174 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
2175 DBUS_TYPE_STRING, append_dnsconfig, service);
2177 connman_dbus_dict_append_array(dict, "Domains",
2178 DBUS_TYPE_STRING, append_domain, service);
2180 connman_dbus_dict_append_array(dict, "Domains.Configuration",
2181 DBUS_TYPE_STRING, append_domainconfig, service);
2183 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
2185 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
2186 append_proxyconfig, service);
2188 connman_dbus_dict_append_dict(dict, "Provider",
2189 append_provider, service);
2192 static void append_struct(gpointer value, gpointer user_data)
2194 struct connman_service *service = value;
2195 DBusMessageIter *iter = user_data;
2196 DBusMessageIter entry, dict;
2198 if (service->path == NULL || service->hidden == TRUE)
2201 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
2203 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2206 connman_dbus_dict_open(&entry, &dict);
2207 append_properties(&dict, TRUE, service);
2208 connman_dbus_dict_close(&entry, &dict);
2210 dbus_message_iter_close_container(iter, &entry);
2213 void __connman_service_list_struct(DBusMessageIter *iter)
2215 g_sequence_foreach(service_list, append_struct, iter);
2218 int __connman_service_get_index(struct connman_service *service)
2220 if (service == NULL)
2223 if (service->network != NULL)
2224 return connman_network_get_index(service->network);
2225 else if (service->provider != NULL)
2226 return connman_provider_get_index(service->provider);
2231 void __connman_service_set_domainname(struct connman_service *service,
2232 const char *domainname)
2234 if (service == NULL)
2237 g_free(service->domainname);
2238 service->domainname = g_strdup(domainname);
2240 domain_changed(service);
2243 const char *connman_service_get_domainname(struct connman_service *service)
2245 if (service == NULL)
2248 if (service->domains != NULL)
2249 return service->domains[0];
2251 return service->domainname;
2254 char **connman_service_get_nameservers(struct connman_service *service)
2256 if (service == NULL)
2259 if (service->nameservers_config != NULL)
2260 return service->nameservers_config;
2261 else if (service->nameservers != NULL)
2262 return service->nameservers;
2267 void connman_service_set_proxy_method(struct connman_service *service,
2268 enum connman_service_proxy_method method)
2270 if (service == NULL)
2273 service->proxy = method;
2275 proxy_changed(service);
2277 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
2278 __connman_notifier_proxy_changed(service);
2281 enum connman_service_proxy_method connman_service_get_proxy_method(
2282 struct connman_service *service)
2284 if (service == NULL)
2285 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2287 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
2288 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
2289 service->pac == NULL)
2290 return service->proxy;
2292 return service->proxy_config;
2295 return service->proxy;
2298 char **connman_service_get_proxy_servers(struct connman_service *service)
2300 return g_strdupv(service->proxies);
2303 char **connman_service_get_proxy_excludes(struct connman_service *service)
2305 return g_strdupv(service->excludes);
2308 const char *connman_service_get_proxy_url(struct connman_service *service)
2310 if (service == NULL)
2313 return service->pac;
2316 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
2319 if (service == NULL)
2322 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
2324 if (service->ipconfig_ipv4) {
2325 if (__connman_ipconfig_set_proxy_autoconfig(
2326 service->ipconfig_ipv4, url) < 0)
2328 } else if (service->ipconfig_ipv6) {
2329 if (__connman_ipconfig_set_proxy_autoconfig(
2330 service->ipconfig_ipv6, url) < 0)
2335 proxy_changed(service);
2337 __connman_notifier_proxy_changed(service);
2340 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
2342 if (service == NULL)
2345 if (service->ipconfig_ipv4)
2346 return __connman_ipconfig_get_proxy_autoconfig(
2347 service->ipconfig_ipv4);
2348 else if (service->ipconfig_ipv6)
2349 return __connman_ipconfig_get_proxy_autoconfig(
2350 service->ipconfig_ipv6);
2354 static void update_timeservers(struct connman_service *service)
2358 if (service->timeservers == NULL)
2361 switch (service->state) {
2362 case CONNMAN_SERVICE_STATE_UNKNOWN:
2363 case CONNMAN_SERVICE_STATE_IDLE:
2364 case CONNMAN_SERVICE_STATE_ASSOCIATION:
2365 case CONNMAN_SERVICE_STATE_CONFIGURATION:
2367 case CONNMAN_SERVICE_STATE_FAILURE:
2368 case CONNMAN_SERVICE_STATE_DISCONNECT:
2369 for (i = 0; service->timeservers[i] != NULL; i++)
2370 connman_timeserver_remove(service->timeservers[i]);
2372 case CONNMAN_SERVICE_STATE_READY:
2373 case CONNMAN_SERVICE_STATE_ONLINE:
2377 for (i = 0; service->timeservers[i] != NULL; i++)
2378 connman_timeserver_append(service->timeservers[i]);
2381 int __connman_service_timeserver_append(struct connman_service *service,
2382 const char *timeserver)
2386 DBG("service %p timeserver %s", service, timeserver);
2388 if (timeserver == NULL)
2391 if (service->timeservers != NULL) {
2394 for (i = 0; service->timeservers[i] != NULL; i++)
2395 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2398 len = g_strv_length(service->timeservers);
2399 service->timeservers = g_try_renew(char *, service->timeservers,
2403 service->timeservers = g_try_new0(char *, len + 2);
2406 if (service->timeservers == NULL)
2409 service->timeservers[len] = g_strdup(timeserver);
2410 service->timeservers[len + 1] = NULL;
2412 update_timeservers(service);
2417 int __connman_service_timeserver_remove(struct connman_service *service,
2418 const char *timeserver)
2423 DBG("service %p timeserver %s", service, timeserver);
2425 if (timeserver == NULL)
2428 if (service->timeservers == NULL)
2431 len = g_strv_length(service->timeservers);
2433 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
2436 g_strfreev(service->timeservers);
2437 service->timeservers = NULL;
2442 servers = g_try_new0(char *, len - 1);
2443 if (servers == NULL)
2446 for (i = 0, j = 0; i < len; i++) {
2447 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2448 servers[j] = g_strdup(service->timeservers[i]);
2452 servers[len - 2] = NULL;
2454 g_strfreev(service->timeservers);
2455 service->timeservers = servers;
2457 update_timeservers(service);
2462 void __connman_service_set_pac(struct connman_service *service,
2465 g_free(service->pac);
2466 service->pac = g_strdup(pac);
2468 proxy_changed(service);
2471 void __connman_service_set_identity(struct connman_service *service,
2472 const char *identity)
2474 if (service->immutable)
2477 g_free(service->identity);
2478 service->identity = g_strdup(identity);
2480 if (service->network != NULL)
2481 connman_network_set_string(service->network,
2486 void __connman_service_set_agent_identity(struct connman_service *service,
2487 const char *agent_identity)
2489 g_free(service->agent_identity);
2490 service->agent_identity = g_strdup(agent_identity);
2492 if (service->network != NULL)
2493 connman_network_set_string(service->network,
2494 "WiFi.AgentIdentity",
2495 service->agent_identity);
2498 void __connman_service_set_passphrase(struct connman_service *service,
2499 const char* passphrase)
2501 if (service->immutable == TRUE)
2504 g_free(service->passphrase);
2505 service->passphrase = g_strdup(passphrase);
2507 passphrase_changed(service);
2509 if (service->network != NULL)
2510 connman_network_set_string(service->network,
2512 service->passphrase);
2514 service_save(service);
2517 void __connman_service_set_agent_passphrase(struct connman_service *service,
2518 const char *agent_passphrase)
2520 g_free(service->agent_passphrase);
2521 service->agent_passphrase = g_strdup(agent_passphrase);
2523 if (service->network != NULL)
2524 connman_network_set_string(service->network,
2525 "WiFi.AgentPassphrase",
2526 service->agent_passphrase);
2529 static DBusMessage *get_properties(DBusConnection *conn,
2530 DBusMessage *msg, void *user_data)
2532 struct connman_service *service = user_data;
2534 DBusMessageIter array, dict;
2536 DBG("service %p", service);
2538 reply = dbus_message_new_method_return(msg);
2542 dbus_message_iter_init_append(reply, &array);
2544 connman_dbus_dict_open(&array, &dict);
2545 append_properties(&dict, FALSE, service);
2546 connman_dbus_dict_close(&array, &dict);
2551 static int update_proxy_configuration(struct connman_service *service,
2552 DBusMessageIter *array)
2554 DBusMessageIter dict;
2555 enum connman_service_proxy_method method;
2556 GString *servers_str = NULL;
2557 GString *excludes_str = NULL;
2558 const char *url = NULL;
2560 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2562 dbus_message_iter_recurse(array, &dict);
2564 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2565 DBusMessageIter entry, variant;
2569 dbus_message_iter_recurse(&dict, &entry);
2571 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2574 dbus_message_iter_get_basic(&entry, &key);
2575 dbus_message_iter_next(&entry);
2577 if (dbus_message_iter_get_arg_type(&entry) !=
2581 dbus_message_iter_recurse(&entry, &variant);
2582 type = dbus_message_iter_get_arg_type(&variant);
2584 if (g_str_equal(key, "Method") == TRUE) {
2587 if (type != DBUS_TYPE_STRING)
2590 dbus_message_iter_get_basic(&variant, &val);
2591 method = string2proxymethod(val);
2592 } else if (g_str_equal(key, "URL") == TRUE) {
2593 if (type != DBUS_TYPE_STRING)
2596 dbus_message_iter_get_basic(&variant, &url);
2597 } else if (g_str_equal(key, "Servers") == TRUE) {
2598 DBusMessageIter str_array;
2600 if (type != DBUS_TYPE_ARRAY)
2603 servers_str = g_string_new(NULL);
2604 if (servers_str == NULL)
2607 dbus_message_iter_recurse(&variant, &str_array);
2609 while (dbus_message_iter_get_arg_type(&str_array) ==
2613 dbus_message_iter_get_basic(&str_array, &val);
2615 if (servers_str->len > 0)
2616 g_string_append_printf(servers_str,
2619 g_string_append(servers_str, val);
2621 dbus_message_iter_next(&str_array);
2623 } else if (g_str_equal(key, "Excludes") == TRUE) {
2624 DBusMessageIter str_array;
2626 if (type != DBUS_TYPE_ARRAY)
2629 excludes_str = g_string_new(NULL);
2630 if (excludes_str == NULL)
2633 dbus_message_iter_recurse(&variant, &str_array);
2635 while (dbus_message_iter_get_arg_type(&str_array) ==
2639 dbus_message_iter_get_basic(&str_array, &val);
2641 if (excludes_str->len > 0)
2642 g_string_append_printf(excludes_str,
2645 g_string_append(excludes_str, val);
2647 dbus_message_iter_next(&str_array);
2651 dbus_message_iter_next(&dict);
2655 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2657 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2658 if (servers_str == NULL && service->proxies == NULL)
2661 if (servers_str != NULL) {
2662 g_strfreev(service->proxies);
2664 if (servers_str->len > 0)
2665 service->proxies = g_strsplit_set(
2666 servers_str->str, " ", 0);
2668 service->proxies = NULL;
2671 if (excludes_str != NULL) {
2672 g_strfreev(service->excludes);
2674 if (excludes_str->len > 0)
2675 service->excludes = g_strsplit_set(
2676 excludes_str->str, " ", 0);
2678 service->excludes = NULL;
2681 if (service->proxies == NULL)
2682 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2685 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2686 g_free(service->pac);
2688 if (url != NULL && strlen(url) > 0)
2689 service->pac = g_strdup(url);
2691 service->pac = NULL;
2693 /* if we are connected:
2694 - if service->pac == NULL
2695 - if __connman_ipconfig_get_proxy_autoconfig(
2696 service->ipconfig) == NULL
2697 --> We should start WPAD */
2700 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2704 if (servers_str != NULL)
2705 g_string_free(servers_str, TRUE);
2707 if (excludes_str != NULL)
2708 g_string_free(excludes_str, TRUE);
2710 service->proxy_config = method;
2715 if (servers_str != NULL)
2716 g_string_free(servers_str, TRUE);
2718 if (excludes_str != NULL)
2719 g_string_free(excludes_str, TRUE);
2724 static int set_ipconfig(struct connman_service *service,
2725 struct connman_ipconfig *ipconfig,
2726 DBusMessageIter *array,
2727 enum connman_service_state state,
2728 enum connman_service_state *new_state)
2730 enum connman_ipconfig_method old_method;
2731 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2732 enum connman_ipconfig_type type;
2735 if (ipconfig == NULL)
2738 old_method = __connman_ipconfig_get_method(ipconfig);
2740 if (is_connecting_state(service, state) ||
2741 is_connected_state(service, state))
2742 __connman_network_clear_ipconfig(service->network, ipconfig);
2744 err = __connman_ipconfig_set_config(ipconfig, array);
2745 method = __connman_ipconfig_get_method(ipconfig);
2746 type = __connman_ipconfig_get_config_type(ipconfig);
2748 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2749 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2750 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2751 *new_state = service->state_ipv4 =
2752 CONNMAN_SERVICE_STATE_CONFIGURATION;
2753 __connman_ipconfig_enable(ipconfig);
2756 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2757 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2758 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2759 *new_state = service->state_ipv6;
2760 __connman_ipconfig_enable(ipconfig);
2764 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2765 type, method, state2string(*new_state));
2770 static DBusMessage *set_property(DBusConnection *conn,
2771 DBusMessage *msg, void *user_data)
2773 struct connman_service *service = user_data;
2774 DBusMessageIter iter, value;
2778 DBG("service %p", service);
2780 if (dbus_message_iter_init(msg, &iter) == FALSE)
2781 return __connman_error_invalid_arguments(msg);
2783 dbus_message_iter_get_basic(&iter, &name);
2784 dbus_message_iter_next(&iter);
2785 dbus_message_iter_recurse(&iter, &value);
2787 type = dbus_message_iter_get_arg_type(&value);
2789 if (g_str_equal(name, "AutoConnect") == TRUE) {
2790 connman_bool_t autoconnect;
2792 if (type != DBUS_TYPE_BOOLEAN)
2793 return __connman_error_invalid_arguments(msg);
2795 if (service->favorite == FALSE)
2796 return __connman_error_invalid_service(msg);
2798 dbus_message_iter_get_basic(&value, &autoconnect);
2800 if (service->autoconnect == autoconnect)
2801 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2803 service->autoconnect = autoconnect;
2805 autoconnect_changed(service);
2807 service_save(service);
2808 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2809 const char *passphrase;
2811 if (type != DBUS_TYPE_STRING)
2812 return __connman_error_invalid_arguments(msg);
2814 if (service->immutable == TRUE)
2815 return __connman_error_not_supported(msg);
2817 dbus_message_iter_get_basic(&value, &passphrase);
2819 __connman_service_set_passphrase(service, passphrase);
2820 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2821 DBusMessageIter entry;
2826 if (type != DBUS_TYPE_ARRAY)
2827 return __connman_error_invalid_arguments(msg);
2829 str = g_string_new(NULL);
2831 return __connman_error_invalid_arguments(msg);
2833 index = connman_network_get_index(service->network);
2834 gw = __connman_ipconfig_get_gateway_from_index(index);
2836 if (gw && strlen(gw))
2837 __connman_service_nameserver_del_routes(service);
2839 dbus_message_iter_recurse(&value, &entry);
2841 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2843 dbus_message_iter_get_basic(&entry, &val);
2844 dbus_message_iter_next(&entry);
2846 g_string_append_printf(str, " %s", val);
2848 g_string_append(str, val);
2851 g_strfreev(service->nameservers_config);
2854 service->nameservers_config =
2855 g_strsplit_set(str->str, " ", 0);
2857 service->nameservers_config = NULL;
2860 g_string_free(str, TRUE);
2862 if (gw && strlen(gw))
2863 __connman_service_nameserver_add_routes(service, gw);
2865 update_nameservers(service);
2866 dns_configuration_changed(service);
2868 service_save(service);
2869 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2870 DBusMessageIter entry;
2873 if (type != DBUS_TYPE_ARRAY)
2874 return __connman_error_invalid_arguments(msg);
2876 str = g_string_new(NULL);
2878 return __connman_error_invalid_arguments(msg);
2880 dbus_message_iter_recurse(&value, &entry);
2882 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2884 dbus_message_iter_get_basic(&entry, &val);
2885 dbus_message_iter_next(&entry);
2887 g_string_append_printf(str, " %s", val);
2889 g_string_append(str, val);
2892 g_strfreev(service->domains);
2895 service->domains = g_strsplit_set(str->str, " ", 0);
2897 service->domains = NULL;
2899 g_string_free(str, TRUE);
2901 update_nameservers(service);
2902 domain_configuration_changed(service);
2904 service_save(service);
2905 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2908 if (type != DBUS_TYPE_ARRAY)
2909 return __connman_error_invalid_arguments(msg);
2911 err = update_proxy_configuration(service, &value);
2914 return __connman_error_failed(msg, -err);
2916 proxy_configuration_changed(service);
2918 __connman_notifier_proxy_changed(service);
2920 service_save(service);
2921 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2922 g_str_equal(name, "IPv6.Configuration")) {
2924 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2925 enum connman_service_state state =
2926 CONNMAN_SERVICE_STATE_UNKNOWN;
2931 if (service->ipconfig_ipv4 == NULL &&
2932 service->ipconfig_ipv6 == NULL)
2933 return __connman_error_invalid_property(msg);
2935 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2936 ipv4 = service->ipconfig_ipv4;
2937 err = set_ipconfig(service, ipv4, &value,
2938 service->state_ipv4, &state);
2940 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2941 ipv6 = service->ipconfig_ipv6;
2942 err = set_ipconfig(service, ipv6, &value,
2943 service->state_ipv6, &state);
2947 if (is_connected_state(service, state) ||
2948 is_connecting_state(service, state))
2949 __connman_network_set_ipconfig(service->network,
2951 return __connman_error_failed(msg, -err);
2955 ipv4_configuration_changed(service);
2957 ipv6_configuration_changed(service);
2959 if (is_connecting(service) || is_connected(service))
2960 __connman_network_set_ipconfig(service->network,
2963 service_save(service);
2965 return __connman_error_invalid_property(msg);
2967 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2970 static void set_idle(struct connman_service *service)
2972 service->state = service->state_ipv4 = service->state_ipv6 =
2973 CONNMAN_SERVICE_STATE_IDLE;
2974 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2975 state_changed(service);
2978 static DBusMessage *clear_property(DBusConnection *conn,
2979 DBusMessage *msg, void *user_data)
2981 struct connman_service *service = user_data;
2984 DBG("service %p", service);
2986 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2989 if (g_str_equal(name, "Error") == TRUE) {
2992 g_get_current_time(&service->modified);
2993 service_save(service);
2994 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2995 if (service->immutable == TRUE)
2996 return __connman_error_not_supported(msg);
2998 g_free(service->passphrase);
2999 service->passphrase = NULL;
3001 passphrase_changed(service);
3003 service_save(service);
3005 return __connman_error_invalid_property(msg);
3007 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3010 static connman_bool_t is_ignore(struct connman_service *service)
3012 if (service->autoconnect == FALSE)
3015 if (service->roaming == TRUE)
3018 if (service->ignore == TRUE)
3021 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
3027 void __connman_service_auto_connect(void)
3029 struct connman_service *service = NULL;
3030 GSequenceIter *iter;
3034 if (__connman_session_mode() == TRUE) {
3035 DBG("Session mode enabled: auto connect disabled");
3039 iter = g_sequence_get_begin_iter(service_list);
3041 while (g_sequence_iter_is_end(iter) == FALSE) {
3042 service = g_sequence_get(iter);
3044 if (service->pending != NULL)
3047 if (is_connecting(service) == TRUE)
3050 if (service->favorite == FALSE)
3053 if (is_connected(service) == TRUE)
3056 if (is_ignore(service) == FALSE && service->state ==
3057 CONNMAN_SERVICE_STATE_IDLE)
3062 iter = g_sequence_iter_next(iter);
3065 if (service != NULL) {
3066 service->userconnect = FALSE;
3067 __connman_service_connect(service);
3071 static void remove_timeout(struct connman_service *service)
3073 if (service->timeout > 0) {
3074 g_source_remove(service->timeout);
3075 service->timeout = 0;
3079 static void reply_pending(struct connman_service *service, int error)
3081 remove_timeout(service);
3083 if (service->pending != NULL) {
3087 reply = __connman_error_failed(service->pending,
3090 g_dbus_send_message(connection, reply);
3094 sender = dbus_message_get_interface(service->pending);
3096 DBG("sender %s", sender);
3098 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
3099 g_dbus_send_reply(connection, service->pending,
3100 DBUS_TYPE_OBJECT_PATH, &service->path,
3103 g_dbus_send_reply(connection, service->pending,
3107 dbus_message_unref(service->pending);
3108 service->pending = NULL;
3112 static gboolean connect_timeout(gpointer user_data)
3114 struct connman_service *service = user_data;
3115 connman_bool_t autoconnect = FALSE;
3117 DBG("service %p", service);
3119 service->timeout = 0;
3121 if (service->network != NULL)
3122 __connman_network_disconnect(service->network);
3124 __connman_ipconfig_disable(service->ipconfig_ipv4);
3125 __connman_ipconfig_disable(service->ipconfig_ipv6);
3127 __connman_stats_service_unregister(service);
3129 if (service->pending != NULL) {
3132 reply = __connman_error_operation_timeout(service->pending);
3134 g_dbus_send_message(connection, reply);
3136 dbus_message_unref(service->pending);
3137 service->pending = NULL;
3141 __connman_service_ipconfig_indicate_state(service,
3142 CONNMAN_SERVICE_STATE_FAILURE,
3143 CONNMAN_IPCONFIG_TYPE_IPV4);
3144 __connman_service_ipconfig_indicate_state(service,
3145 CONNMAN_SERVICE_STATE_FAILURE,
3146 CONNMAN_IPCONFIG_TYPE_IPV6);
3148 if (autoconnect == TRUE && service->userconnect == FALSE)
3149 __connman_service_auto_connect();
3154 static void set_reconnect_state(struct connman_service *service,
3155 connman_bool_t reconnect)
3157 struct connman_device *device;
3159 if (service->network == NULL)
3162 device = connman_network_get_device(service->network);
3166 __connman_device_set_reconnect(device, reconnect);
3169 static connman_bool_t get_reconnect_state(struct connman_service *service)
3171 struct connman_device *device;
3173 if (service->network == NULL)
3176 device = connman_network_get_device(service->network);
3180 return __connman_device_get_reconnect(device);
3183 static void request_input_cb (struct connman_service *service,
3184 const char *identity, const char *passphrase,
3187 DBG ("RequestInput return, %p", service);
3189 if (identity == NULL && passphrase == NULL && service->wps == FALSE)
3192 if (identity != NULL)
3193 __connman_service_set_agent_identity(service, identity);
3195 if (passphrase != NULL) {
3196 switch (service->security) {
3197 case CONNMAN_SERVICE_SECURITY_WEP:
3198 case CONNMAN_SERVICE_SECURITY_PSK:
3199 __connman_service_set_passphrase(service, passphrase);
3201 case CONNMAN_SERVICE_SECURITY_8021X:
3202 __connman_service_set_agent_passphrase(service,
3205 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3206 case CONNMAN_SERVICE_SECURITY_NONE:
3207 case CONNMAN_SERVICE_SECURITY_WPA:
3208 case CONNMAN_SERVICE_SECURITY_RSN:
3209 DBG("service security '%s' not handled",
3210 security2string(service->security));
3215 __connman_service_connect(service);
3217 /* Never cache agent provided credentials */
3218 __connman_service_set_agent_identity(service, NULL);
3219 __connman_service_set_agent_passphrase(service, NULL);
3222 static DBusMessage *connect_service(DBusConnection *conn,
3223 DBusMessage *msg, void *user_data)
3225 struct connman_service *service = user_data;
3226 GSequenceIter *iter;
3229 DBG("service %p", service);
3231 if (service->pending != NULL)
3232 return __connman_error_in_progress(msg);
3234 iter = g_sequence_get_begin_iter(service_list);
3236 while (g_sequence_iter_is_end(iter) == FALSE) {
3237 struct connman_service *temp = g_sequence_get(iter);
3239 if (service->type == temp->type && is_connecting(temp) == TRUE)
3240 return __connman_error_in_progress(msg);
3242 iter = g_sequence_iter_next(iter);
3245 service->ignore = FALSE;
3247 service->userconnect = TRUE;
3249 service->pending = dbus_message_ref(msg);
3251 set_reconnect_state(service, FALSE);
3253 err = __connman_service_connect(service);
3255 if (service->pending == NULL)
3258 if (err != -EINPROGRESS) {
3259 dbus_message_unref(service->pending);
3260 service->pending = NULL;
3262 return __connman_error_failed(msg, -err);
3268 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3271 static DBusMessage *disconnect_service(DBusConnection *conn,
3272 DBusMessage *msg, void *user_data)
3274 struct connman_service *service = user_data;
3277 DBG("service %p", service);
3279 reply_pending(service, ECONNABORTED);
3281 service->ignore = TRUE;
3283 set_reconnect_state(service, FALSE);
3285 err = __connman_service_disconnect(service);
3287 if (err != -EINPROGRESS)
3288 return __connman_error_failed(msg, -err);
3293 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3296 static DBusMessage *remove_service(DBusConnection *conn,
3297 DBusMessage *msg, void *user_data)
3299 struct connman_service *service = user_data;
3301 DBG("service %p", service);
3303 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
3304 return __connman_error_not_supported(msg);
3306 if (service->immutable == TRUE)
3307 return __connman_error_not_supported(msg);
3309 if (service->favorite == FALSE && service->state !=
3310 CONNMAN_SERVICE_STATE_FAILURE)
3311 return __connman_error_not_supported(msg);
3313 if (service->network != NULL) {
3314 set_reconnect_state(service, FALSE);
3316 __connman_network_disconnect(service->network);
3319 g_free(service->passphrase);
3320 service->passphrase = NULL;
3322 passphrase_changed(service);
3326 __connman_service_set_favorite(service, FALSE);
3327 service_save(service);
3329 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3332 static gboolean check_suitable_state(enum connman_service_state a,
3333 enum connman_service_state b)
3336 * Special check so that "ready" service can be moved before
3339 if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
3340 b == CONNMAN_SERVICE_STATE_READY) ||
3341 (b == CONNMAN_SERVICE_STATE_ONLINE &&
3342 a == CONNMAN_SERVICE_STATE_READY))
3348 static DBusMessage *move_service(DBusConnection *conn,
3349 DBusMessage *msg, void *user_data,
3352 struct connman_service *service = user_data;
3353 struct connman_service *target;
3355 GSequenceIter *src, *dst;
3356 enum connman_ipconfig_method target4, target6;
3357 enum connman_ipconfig_method service4, service6;
3359 DBG("service %p", service);
3361 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3364 if (service->favorite == FALSE)
3365 return __connman_error_not_supported(msg);
3367 target = find_service(path);
3368 if (target == NULL || target->favorite == FALSE || target == service)
3369 return __connman_error_invalid_service(msg);
3371 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3372 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3373 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3374 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3376 DBG("target %s method %d/%d state %d/%d", target->identifier,
3378 target->state_ipv4, target->state_ipv6);
3380 DBG("service %s method %d/%d state %d/%d", service->identifier,
3382 service->state_ipv4, service->state_ipv6);
3385 * If method is OFF, then we do not need to check the corresponding
3388 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3389 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3390 if (check_suitable_state(target->state_ipv6,
3391 service->state_ipv6) == FALSE)
3392 return __connman_error_invalid_service(msg);
3396 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3397 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3398 if (check_suitable_state(target->state_ipv4,
3399 service->state_ipv4) == FALSE)
3400 return __connman_error_invalid_service(msg);
3404 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3405 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3406 if (check_suitable_state(target->state_ipv6,
3407 service->state_ipv6) == FALSE)
3408 return __connman_error_invalid_service(msg);
3412 if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3413 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3414 if (check_suitable_state(target->state_ipv4,
3415 service->state_ipv4) == FALSE)
3416 return __connman_error_invalid_service(msg);
3420 g_get_current_time(&service->modified);
3421 service_save(service);
3423 src = g_hash_table_lookup(service_hash, service->identifier);
3424 dst = g_hash_table_lookup(service_hash, target->identifier);
3426 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
3428 services_changed(FALSE);
3430 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3433 static DBusMessage *move_before(DBusConnection *conn,
3434 DBusMessage *msg, void *user_data)
3436 return move_service(conn, msg, user_data, TRUE);
3439 static DBusMessage *move_after(DBusConnection *conn,
3440 DBusMessage *msg, void *user_data)
3442 return move_service(conn, msg, user_data, FALSE);
3445 static DBusMessage *reset_counters(DBusConnection *conn,
3446 DBusMessage *msg, void *user_data)
3448 struct connman_service *service = user_data;
3450 reset_stats(service);
3452 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3455 static GDBusMethodTable service_methods[] = {
3456 { "GetProperties", "", "a{sv}", get_properties },
3457 { "SetProperty", "sv", "", set_property },
3458 { "ClearProperty", "s", "", clear_property },
3459 { "Connect", "", "", connect_service,
3460 G_DBUS_METHOD_FLAG_ASYNC },
3461 { "Disconnect", "", "", disconnect_service },
3462 { "Remove", "", "", remove_service },
3463 { "MoveBefore", "o", "", move_before },
3464 { "MoveAfter", "o", "", move_after },
3465 { "ResetCounters", "", "", reset_counters },
3469 static GDBusSignalTable service_signals[] = {
3470 { "PropertyChanged", "sv" },
3474 static void service_free(gpointer user_data)
3476 struct connman_service *service = user_data;
3477 char *path = service->path;
3479 DBG("service %p", service);
3481 reply_pending(service, ENOENT);
3483 g_hash_table_remove(service_hash, service->identifier);
3485 __connman_notifier_service_remove(service);
3487 stats_stop(service);
3488 service_save(service);
3490 service->path = NULL;
3493 services_changed(FALSE);
3495 g_dbus_unregister_interface(connection, path,
3496 CONNMAN_SERVICE_INTERFACE);
3500 g_hash_table_destroy(service->counter_table);
3502 if (service->network != NULL) {
3503 if (service->network_created == TRUE)
3504 connman_network_unref(service->network);
3507 if (service->provider != NULL)
3508 connman_provider_unref(service->provider);
3510 if (service->ipconfig_ipv4 != NULL) {
3511 connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3512 connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3513 connman_ipconfig_unref(service->ipconfig_ipv4);
3514 service->ipconfig_ipv4 = NULL;
3517 if (service->ipconfig_ipv6 != NULL) {
3518 connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3519 connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3520 connman_ipconfig_unref(service->ipconfig_ipv6);
3521 service->ipconfig_ipv6 = NULL;
3524 if (service->location != NULL)
3525 connman_location_unref(service->location);
3527 g_strfreev(service->nameservers);
3528 g_strfreev(service->nameservers_config);
3529 g_strfreev(service->domains);
3530 g_strfreev(service->proxies);
3531 g_strfreev(service->excludes);
3533 g_free(service->domainname);
3534 g_free(service->pac);
3535 g_free(service->name);
3536 g_free(service->passphrase);
3537 g_free(service->agent_passphrase);
3538 g_free(service->identifier);
3539 g_free(service->eap);
3540 g_free(service->identity);
3541 g_free(service->agent_identity);
3542 g_free(service->ca_cert_file);
3543 g_free(service->client_cert_file);
3544 g_free(service->private_key_file);
3545 g_free(service->private_key_passphrase);
3546 g_free(service->phase2);
3548 if (service->stats.timer != NULL)
3549 g_timer_destroy(service->stats.timer);
3550 if (service->stats_roaming.timer != NULL)
3551 g_timer_destroy(service->stats_roaming.timer);
3557 * __connman_service_put:
3558 * @service: service structure
3560 * Release service if no longer needed
3562 void __connman_service_put(struct connman_service *service)
3564 DBG("service %p", service);
3566 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
3567 GSequenceIter *iter;
3569 iter = g_hash_table_lookup(service_hash, service->identifier);
3571 reply_pending(service, ECONNABORTED);
3573 __connman_service_disconnect(service);
3575 g_sequence_remove(iter);
3577 service_free(service);
3581 static void stats_init(struct connman_service *service)
3584 service->stats.valid = FALSE;
3585 service->stats.enabled = FALSE;
3586 service->stats.timer = g_timer_new();
3589 service->stats_roaming.valid = FALSE;
3590 service->stats_roaming.enabled = FALSE;
3591 service->stats_roaming.timer = g_timer_new();
3594 static void service_initialize(struct connman_service *service)
3596 DBG("service %p", service);
3598 service->refcount = 1;
3599 service->session_usage_count = 0;
3601 service->network_created = FALSE;
3603 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3604 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3606 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3607 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3608 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3610 service->favorite = FALSE;
3611 service->immutable = FALSE;
3612 service->hidden = FALSE;
3614 service->ignore = FALSE;
3616 service->userconnect = FALSE;
3620 stats_init(service);
3622 service->provider = NULL;
3624 service->wps = FALSE;
3628 * connman_service_create:
3630 * Allocate a new service.
3632 * Returns: a newly-allocated #connman_service structure
3634 struct connman_service *connman_service_create(void)
3637 struct connman_stats_counter *counters;
3638 const char *counter;
3640 struct connman_service *service;
3642 service = g_try_new0(struct connman_service, 1);
3643 if (service == NULL)
3646 DBG("service %p", service);
3648 service->counter_table = g_hash_table_new_full(g_str_hash,
3649 g_str_equal, NULL, g_free);
3651 for (list = counter_list; list; list = list->next) {
3652 counter = list->data;
3654 counters = g_try_new0(struct connman_stats_counter, 1);
3655 if (counters == NULL) {
3656 g_hash_table_destroy(service->counter_table);
3661 counters->append_all = TRUE;
3663 g_hash_table_replace(service->counter_table, (gpointer)counter,
3667 service_initialize(service);
3669 service->location = __connman_location_create(service);
3674 struct connman_location *__connman_service_get_location(struct connman_service *service)
3676 return service->location;
3680 * connman_service_ref:
3681 * @service: service structure
3683 * Increase reference counter of service
3685 struct connman_service *connman_service_ref(struct connman_service *service)
3689 g_atomic_int_inc(&service->refcount);
3695 * connman_service_unref:
3696 * @service: service structure
3698 * Decrease reference counter of service
3700 void connman_service_unref(struct connman_service *service)
3702 __connman_service_put(service);
3705 static gint service_compare(gconstpointer a, gconstpointer b,
3708 struct connman_service *service_a = (void *) a;
3709 struct connman_service *service_b = (void *) b;
3710 enum connman_service_state state_a, state_b;
3712 state_a = service_a->state;
3713 state_b = service_b->state;
3715 if (state_a != state_b) {
3716 gboolean a_connected = is_connected(service_a);
3717 gboolean b_connected = is_connected(service_b);
3719 if (a_connected == TRUE && b_connected == TRUE) {
3720 /* We prefer online over ready state */
3721 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
3724 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
3728 if (a_connected == TRUE)
3730 if (b_connected == TRUE)
3733 if (is_connecting(service_a) == TRUE)
3735 if (is_connecting(service_b) == TRUE)
3739 if (service_a->order > service_b->order)
3742 if (service_a->order < service_b->order)
3745 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3748 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3751 if (service_a->type != service_b->type) {
3752 switch (service_a->type) {
3753 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3754 case CONNMAN_SERVICE_TYPE_SYSTEM:
3755 case CONNMAN_SERVICE_TYPE_ETHERNET:
3756 case CONNMAN_SERVICE_TYPE_GPS:
3757 case CONNMAN_SERVICE_TYPE_VPN:
3758 case CONNMAN_SERVICE_TYPE_GADGET:
3760 case CONNMAN_SERVICE_TYPE_WIFI:
3762 case CONNMAN_SERVICE_TYPE_WIMAX:
3763 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3764 case CONNMAN_SERVICE_TYPE_CELLULAR:
3769 return (gint) service_b->strength - (gint) service_a->strength;
3773 * connman_service_get_type:
3774 * @service: service structure
3776 * Get the type of service
3778 enum connman_service_type connman_service_get_type(struct connman_service *service)
3780 if (service == NULL)
3781 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3783 return service->type;
3787 * connman_service_get_interface:
3788 * @service: service structure
3790 * Get network interface of service
3792 char *connman_service_get_interface(struct connman_service *service)
3796 if (service == NULL)
3799 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3800 if (service->ipconfig_ipv4)
3801 index = connman_ipconfig_get_index(
3802 service->ipconfig_ipv4);
3803 else if (service->ipconfig_ipv6)
3804 index = connman_ipconfig_get_index(
3805 service->ipconfig_ipv6);
3809 return connman_inet_ifname(index);
3812 if (service->network == NULL)
3815 index = connman_network_get_index(service->network);
3817 return connman_inet_ifname(index);
3821 * connman_service_get_network:
3822 * @service: service structure
3824 * Get the service network
3826 struct connman_network *
3827 __connman_service_get_network(struct connman_service *service)
3829 if (service == NULL)
3832 return service->network;
3835 struct connman_ipconfig *
3836 __connman_service_get_ip4config(struct connman_service *service)
3838 if (service == NULL)
3841 return service->ipconfig_ipv4;
3844 struct connman_ipconfig *
3845 __connman_service_get_ip6config(struct connman_service *service)
3847 if (service == NULL)
3850 return service->ipconfig_ipv6;
3853 struct connman_ipconfig *
3854 __connman_service_get_ipconfig(struct connman_service *service, int family)
3856 if (family == AF_INET)
3857 return __connman_service_get_ip4config(service);
3858 else if (family == AF_INET6)
3859 return __connman_service_get_ip6config(service);
3865 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3867 if (service == NULL)
3868 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3870 return service->security;
3873 const char *__connman_service_get_phase2(struct connman_service *service)
3875 if (service == NULL)
3878 return service->phase2;
3881 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3883 if (service == NULL)
3886 return service->wps;
3890 * __connman_service_set_favorite:
3891 * @service: service structure
3892 * @favorite: favorite value
3894 * Change the favorite setting of service
3896 int __connman_service_set_favorite(struct connman_service *service,
3897 connman_bool_t favorite)
3899 GSequenceIter *iter;
3901 iter = g_hash_table_lookup(service_hash, service->identifier);
3905 if (service->favorite == favorite)
3908 service->favorite = favorite;
3910 favorite_changed(service);
3912 g_sequence_sort_changed(iter, service_compare, NULL);
3914 services_changed(FALSE);
3919 int __connman_service_set_immutable(struct connman_service *service,
3920 connman_bool_t immutable)
3922 service->immutable = immutable;
3924 immutable_changed(service);
3929 void __connman_service_set_string(struct connman_service *service,
3930 const char *key, const char *value)
3932 if (g_str_equal(key, "EAP") == TRUE) {
3933 g_free(service->eap);
3934 service->eap = g_strdup(value);
3935 } else if (g_str_equal(key, "Identity") == TRUE) {
3936 g_free(service->identity);
3937 service->identity = g_strdup(value);
3938 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3939 g_free(service->ca_cert_file);
3940 service->ca_cert_file = g_strdup(value);
3941 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3942 g_free(service->client_cert_file);
3943 service->client_cert_file = g_strdup(value);
3944 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3945 g_free(service->private_key_file);
3946 service->private_key_file = g_strdup(value);
3947 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3948 g_free(service->private_key_passphrase);
3949 service->private_key_passphrase = g_strdup(value);
3950 } else if (g_str_equal(key, "Phase2") == TRUE) {
3951 g_free(service->phase2);
3952 service->phase2 = g_strdup(value);
3953 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3954 g_free(service->passphrase);
3955 service->passphrase = g_strdup(value);
3959 static void service_complete(struct connman_service *service)
3961 reply_pending(service, EIO);
3963 if (service->userconnect == FALSE)
3964 __connman_service_auto_connect();
3966 g_get_current_time(&service->modified);
3967 service_save(service);
3970 static void report_error_cb(struct connman_service *service,
3971 gboolean retry, void *user_data)
3974 __connman_service_connect(service);
3976 service_complete(service);
3977 services_changed(FALSE);
3978 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3982 static int __connman_service_indicate_state(struct connman_service *service)
3984 enum connman_service_state old_state, new_state;
3985 GSequenceIter *iter;
3987 if (service == NULL)
3990 old_state = service->state;
3991 new_state = combine_state(service->state_ipv4, service->state_ipv6);
3993 if (old_state == new_state)
3996 DBG("service %p old %s - new %s/%s => %s",
3998 state2string(old_state),
3999 state2string(service->state_ipv4),
4000 state2string(service->state_ipv6),
4001 state2string(new_state));
4003 service->state = new_state;
4004 state_changed(service);
4006 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
4007 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
4008 reply_pending(service, ECONNABORTED);
4010 __connman_service_disconnect(service);
4013 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
4014 if (__connman_stats_service_register(service) == 0) {
4015 __connman_stats_get(service, FALSE,
4016 &service->stats.data);
4017 __connman_stats_get(service, TRUE,
4018 &service->stats_roaming.data);
4022 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4023 if (service->login_required == TRUE) {
4024 service->login_required = FALSE;
4025 login_changed(service);
4028 connman_timeserver_sync();
4031 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
4032 connman_bool_t reconnect;
4034 reconnect = get_reconnect_state(service);
4035 if (reconnect == TRUE)
4036 __connman_service_auto_connect();
4038 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4041 if (new_state == CONNMAN_SERVICE_STATE_READY) {
4042 enum connman_ipconfig_method method;
4044 set_reconnect_state(service, TRUE);
4046 __connman_service_set_favorite(service, TRUE);
4048 reply_pending(service, 0);
4050 service->userconnect = FALSE;
4052 g_get_current_time(&service->modified);
4053 service_save(service);
4055 update_nameservers(service);
4056 dns_changed(service);
4057 domain_changed(service);
4059 __connman_notifier_connect(service->type);
4061 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4062 connman_network_get_bool(service->network,
4063 "WiFi.UseWPS") == TRUE) {
4066 pass = connman_network_get_string(service->network,
4069 __connman_service_set_passphrase(service, pass);
4071 connman_network_set_bool(service->network,
4072 "WiFi.UseWPS", FALSE);
4077 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4078 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4079 __connman_ipconfig_disable_ipv6(
4080 service->ipconfig_ipv6);
4082 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4083 struct connman_service *def_service = get_default();
4085 if (__connman_notifier_count_connected() == 0 &&
4086 def_service != NULL &&
4087 def_service->provider != NULL)
4088 __connman_provider_disconnect(def_service->provider);
4090 __connman_location_finish(service);
4094 __connman_wpad_stop(service);
4096 update_nameservers(service);
4097 dns_changed(service);
4098 domain_changed(service);
4100 __connman_notifier_disconnect(service->type);
4103 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4104 if (service->userconnect == TRUE &&
4105 __connman_agent_report_error(service,
4106 error2string(service->error),
4107 report_error_cb, NULL) == -EIO)
4109 service_complete(service);
4111 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4113 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4115 iter = g_hash_table_lookup(service_hash, service->identifier);
4117 g_sequence_sort_changed(iter, service_compare, NULL);
4119 services_changed(FALSE);
4121 if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
4127 int __connman_service_indicate_error(struct connman_service *service,
4128 enum connman_service_error error)
4130 DBG("service %p error %d", service, error);
4132 if (service == NULL)
4135 service->error = error;
4137 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4138 __connman_service_set_passphrase(service, NULL);
4140 __connman_service_ipconfig_indicate_state(service,
4141 CONNMAN_SERVICE_STATE_FAILURE,
4142 CONNMAN_IPCONFIG_TYPE_IPV4);
4143 __connman_service_ipconfig_indicate_state(service,
4144 CONNMAN_SERVICE_STATE_FAILURE,
4145 CONNMAN_IPCONFIG_TYPE_IPV6);
4149 int __connman_service_clear_error(struct connman_service *service)
4151 DBG("service %p", service);
4153 if (service == NULL)
4156 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4159 service->state_ipv4 = service->state_ipv6 =
4160 CONNMAN_SERVICE_STATE_UNKNOWN;
4161 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
4163 if (service->favorite == TRUE)
4164 set_reconnect_state(service, TRUE);
4166 __connman_service_ipconfig_indicate_state(service,
4167 CONNMAN_SERVICE_STATE_IDLE,
4168 CONNMAN_IPCONFIG_TYPE_IPV6);
4171 * Toggling the IPv6 state to IDLE could trigger the auto connect
4172 * machinery and consequently the IPv4 state.
4174 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4175 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4178 return __connman_service_ipconfig_indicate_state(service,
4179 CONNMAN_SERVICE_STATE_IDLE,
4180 CONNMAN_IPCONFIG_TYPE_IPV4);
4183 int __connman_service_indicate_default(struct connman_service *service)
4185 DBG("service %p", service);
4189 __connman_location_detect(service);
4194 static void check_proxy_setup(struct connman_service *service)
4197 * We start WPAD if we haven't got a PAC URL from DHCP and
4198 * if our proxy manual configuration is either empty or set
4199 * to AUTO with an empty URL.
4202 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4205 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4206 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4207 service->pac != NULL))
4210 if (__connman_wpad_start(service) < 0) {
4211 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4212 __connman_notifier_proxy_changed(service);
4216 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
4217 enum connman_service_state new_state,
4218 enum connman_ipconfig_type type)
4220 struct connman_ipconfig *ipconfig = NULL;
4221 enum connman_service_state old_state;
4223 if (service == NULL)
4226 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4227 old_state = service->state_ipv4;
4228 ipconfig = service->ipconfig_ipv4;
4229 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4230 old_state = service->state_ipv6;
4231 ipconfig = service->ipconfig_ipv6;
4234 if (ipconfig == NULL)
4238 if (old_state == new_state)
4241 DBG("service %p (%s) state %d (%s) type %d (%s)",
4242 service, service ? service->identifier : NULL,
4243 new_state, state2string(new_state),
4244 type, __connman_ipconfig_type2string(type));
4246 switch (new_state) {
4247 case CONNMAN_SERVICE_STATE_UNKNOWN:
4248 case CONNMAN_SERVICE_STATE_IDLE:
4249 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
4252 case CONNMAN_SERVICE_STATE_ASSOCIATION:
4254 case CONNMAN_SERVICE_STATE_CONFIGURATION:
4255 __connman_ipconfig_enable(ipconfig);
4257 case CONNMAN_SERVICE_STATE_READY:
4258 update_nameservers(service);
4260 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4261 check_proxy_setup(service);
4263 case CONNMAN_SERVICE_STATE_ONLINE:
4265 case CONNMAN_SERVICE_STATE_DISCONNECT:
4266 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
4269 case CONNMAN_SERVICE_STATE_FAILURE:
4273 /* We keep that state */
4274 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4275 service->state_ipv4 = new_state;
4276 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4277 service->state_ipv6 = new_state;
4279 return __connman_service_indicate_state(service);
4282 int __connman_service_request_login(struct connman_service *service)
4284 DBG("service %p", service);
4286 if (service == NULL)
4289 service->login_required = TRUE;
4290 login_changed(service);
4295 static connman_bool_t prepare_network(struct connman_service *service)
4297 enum connman_network_type type;
4298 unsigned int ssid_len;
4300 type = connman_network_get_type(service->network);
4303 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4304 case CONNMAN_NETWORK_TYPE_VENDOR:
4306 case CONNMAN_NETWORK_TYPE_WIFI:
4307 if (connman_network_get_blob(service->network, "WiFi.SSID",
4311 if (service->passphrase != NULL)
4312 connman_network_set_string(service->network,
4313 "WiFi.Passphrase", service->passphrase);
4315 case CONNMAN_NETWORK_TYPE_ETHERNET:
4316 case CONNMAN_NETWORK_TYPE_WIMAX:
4317 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4318 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4319 case CONNMAN_NETWORK_TYPE_CELLULAR:
4326 static void prepare_8021x(struct connman_service *service)
4328 if (service->eap != NULL)
4329 connman_network_set_string(service->network, "WiFi.EAP",
4332 if (service->identity != NULL)
4333 connman_network_set_string(service->network, "WiFi.Identity",
4336 if (service->ca_cert_file != NULL)
4337 connman_network_set_string(service->network, "WiFi.CACertFile",
4338 service->ca_cert_file);
4340 if (service->client_cert_file != NULL)
4341 connman_network_set_string(service->network,
4342 "WiFi.ClientCertFile",
4343 service->client_cert_file);
4345 if (service->private_key_file != NULL)
4346 connman_network_set_string(service->network,
4347 "WiFi.PrivateKeyFile",
4348 service->private_key_file);
4350 if (service->private_key_passphrase != NULL)
4351 connman_network_set_string(service->network,
4352 "WiFi.PrivateKeyPassphrase",
4353 service->private_key_passphrase);
4355 if (service->phase2 != NULL)
4356 connman_network_set_string(service->network, "WiFi.Phase2",
4360 static int service_connect(struct connman_service *service)
4364 switch (service->type) {
4365 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4366 case CONNMAN_SERVICE_TYPE_SYSTEM:
4367 case CONNMAN_SERVICE_TYPE_GPS:
4368 case CONNMAN_SERVICE_TYPE_GADGET:
4370 case CONNMAN_SERVICE_TYPE_ETHERNET:
4371 case CONNMAN_SERVICE_TYPE_WIMAX:
4372 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4373 case CONNMAN_SERVICE_TYPE_CELLULAR:
4374 case CONNMAN_SERVICE_TYPE_VPN:
4376 case CONNMAN_SERVICE_TYPE_WIFI:
4377 switch (service->security) {
4378 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4379 case CONNMAN_SERVICE_SECURITY_NONE:
4381 case CONNMAN_SERVICE_SECURITY_WEP:
4382 case CONNMAN_SERVICE_SECURITY_PSK:
4383 case CONNMAN_SERVICE_SECURITY_WPA:
4384 case CONNMAN_SERVICE_SECURITY_RSN:
4385 if (service->passphrase == NULL) {
4386 if (service->network == NULL)
4389 if (service->wps == FALSE ||
4390 connman_network_get_bool(
4392 "WiFi.UseWPS") == FALSE)
4396 case CONNMAN_SERVICE_SECURITY_8021X:
4397 if (service->eap == NULL)
4401 * never request credentials if using EAP-TLS
4402 * (EAP-TLS networks need to be fully provisioned)
4404 if (g_str_equal(service->eap, "tls") == TRUE)
4408 * Return -ENOKEY if either identity or passphrase is
4409 * missing. Agent provided credentials can be used as
4410 * fallback if needed.
4412 if ((service->identity == NULL &&
4413 service->agent_identity == NULL) ||
4414 (service->passphrase == NULL &&
4415 service->agent_passphrase == NULL))
4423 if (service->network != NULL) {
4424 if (prepare_network(service) == FALSE)
4427 switch (service->security) {
4428 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4429 case CONNMAN_SERVICE_SECURITY_NONE:
4430 case CONNMAN_SERVICE_SECURITY_WEP:
4431 case CONNMAN_SERVICE_SECURITY_PSK:
4432 case CONNMAN_SERVICE_SECURITY_WPA:
4433 case CONNMAN_SERVICE_SECURITY_RSN:
4435 case CONNMAN_SERVICE_SECURITY_8021X:
4436 prepare_8021x(service);
4440 if (__connman_stats_service_register(service) == 0) {
4441 __connman_stats_get(service, FALSE,
4442 &service->stats.data);
4443 __connman_stats_get(service, TRUE,
4444 &service->stats_roaming.data);
4447 if (service->ipconfig_ipv4)
4448 __connman_ipconfig_enable(service->ipconfig_ipv4);
4449 if (service->ipconfig_ipv6)
4450 __connman_ipconfig_enable(service->ipconfig_ipv6);
4452 err = __connman_network_connect(service->network);
4453 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4454 service->provider != NULL)
4455 err = __connman_provider_connect(service->provider);
4460 if (err != -EINPROGRESS) {
4461 __connman_ipconfig_disable(service->ipconfig_ipv4);
4462 __connman_ipconfig_disable(service->ipconfig_ipv6);
4463 __connman_stats_service_unregister(service);
4471 int __connman_service_connect(struct connman_service *service)
4475 DBG("service %p state %s", service, state2string(service->state));
4477 if (is_connected(service) == TRUE)
4480 if (is_connecting(service) == TRUE)
4483 switch (service->type) {
4484 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4485 case CONNMAN_SERVICE_TYPE_SYSTEM:
4486 case CONNMAN_SERVICE_TYPE_GPS:
4487 case CONNMAN_SERVICE_TYPE_GADGET:
4490 err = service_connect(service);
4496 if (err == -EINPROGRESS) {
4497 if (service->timeout == 0)
4498 service->timeout = g_timeout_add_seconds(
4499 CONNECT_TIMEOUT, connect_timeout, service);
4501 return -EINPROGRESS;
4504 __connman_service_ipconfig_indicate_state(service,
4505 CONNMAN_SERVICE_STATE_FAILURE,
4506 CONNMAN_IPCONFIG_TYPE_IPV4);
4507 __connman_service_ipconfig_indicate_state(service,
4508 CONNMAN_SERVICE_STATE_FAILURE,
4509 CONNMAN_IPCONFIG_TYPE_IPV6);
4511 if (service->network != NULL)
4512 __connman_network_disconnect(service->network);
4513 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4514 service->provider != NULL)
4515 __connman_provider_disconnect(service->provider);
4517 if (service->userconnect == TRUE) {
4518 if (err == -ENOKEY) {
4519 if (__connman_agent_request_input(service,
4522 return -EINPROGRESS;
4524 reply_pending(service, err);
4530 int __connman_service_disconnect(struct connman_service *service)
4534 DBG("service %p", service);
4536 if (service->network != NULL) {
4537 err = __connman_network_disconnect(service->network);
4538 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4539 service->provider != NULL)
4540 err = __connman_provider_disconnect(service->provider);
4544 if (err < 0 && err != -EINPROGRESS)
4547 __connman_6to4_remove(service->ipconfig_ipv4);
4549 if (service->ipconfig_ipv4)
4550 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
4553 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
4556 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
4557 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
4559 __connman_ipconfig_disable(service->ipconfig_ipv4);
4560 __connman_ipconfig_disable(service->ipconfig_ipv6);
4562 __connman_stats_service_unregister(service);
4567 int __connman_service_disconnect_all(void)
4569 GSequenceIter *iter;
4573 iter = g_sequence_get_begin_iter(service_list);
4575 while (g_sequence_iter_is_end(iter) == FALSE) {
4576 struct connman_service *service = g_sequence_get(iter);
4578 service->ignore = TRUE;
4580 set_reconnect_state(service, FALSE);
4582 __connman_service_disconnect(service);
4584 iter = g_sequence_iter_next(iter);
4592 * __connman_service_lookup:
4593 * @pattern: search pattern
4594 * @path: return object path
4596 * Look up a service path from a search pattern
4598 int __connman_service_lookup(const char *pattern, const char **path)
4600 GHashTableIter iter;
4601 gpointer key, value;
4603 g_hash_table_iter_init(&iter, service_hash);
4605 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4606 GSequenceIter *iter = value;
4607 struct connman_service *service = g_sequence_get(iter);
4609 if (g_strcmp0(service->identifier, pattern) == 0 ||
4610 g_strcmp0(service->name, pattern) == 0) {
4611 *path = (const char *) service->path;
4620 * lookup_by_identifier:
4621 * @identifier: service identifier
4623 * Look up a service by identifier (reference count will not be increased)
4625 static struct connman_service *lookup_by_identifier(const char *identifier)
4627 GSequenceIter *iter;
4629 iter = g_hash_table_lookup(service_hash, identifier);
4631 return g_sequence_get(iter);
4636 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4637 const char *ssid, const char *mode, const char *security,
4640 struct connman_network *network;
4643 unsigned int i, ssid_len;
4645 ssid_len = strlen(ssid);
4649 network = connman_network_create(group, CONNMAN_NETWORK_TYPE_WIFI);
4650 if (network == NULL)
4653 connman_network_set_blob(network, "WiFi.SSID",
4654 (unsigned char *) ssid, ssid_len);
4656 connman_network_set_string(network, "WiFi.Mode", mode);
4657 connman_network_set_string(network, "WiFi.Security", security);
4659 name = g_try_malloc0(ssid_len + 1);
4661 connman_network_unref(network);
4665 for (i = 0; i < ssid_len; i++) {
4666 if (g_ascii_isprint(ssid[i]))
4672 connman_network_set_name(network, name);
4676 index = connman_device_get_index(device);
4677 connman_network_set_index(network, index);
4679 if (connman_device_add_network(device, network) < 0) {
4680 connman_network_unref(network);
4684 connman_network_set_available(network, TRUE);
4689 int __connman_service_create_and_connect(DBusMessage *msg)
4691 struct connman_service *service;
4692 struct connman_network *network;
4693 struct connman_device *device;
4694 DBusMessageIter iter, array;
4695 const char *mode = "managed", *security = "none", *group_security;
4696 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4697 unsigned int ssid_len = 0;
4702 dbus_message_iter_init(msg, &iter);
4703 dbus_message_iter_recurse(&iter, &array);
4705 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4706 DBusMessageIter entry, value;
4709 dbus_message_iter_recurse(&array, &entry);
4710 dbus_message_iter_get_basic(&entry, &key);
4712 dbus_message_iter_next(&entry);
4713 dbus_message_iter_recurse(&entry, &value);
4715 switch (dbus_message_iter_get_arg_type(&value)) {
4716 case DBUS_TYPE_STRING:
4717 if (g_str_equal(key, "Type") == TRUE)
4718 dbus_message_iter_get_basic(&value, &type);
4719 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4720 g_str_equal(key, "Mode") == TRUE)
4721 dbus_message_iter_get_basic(&value, &mode);
4722 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4723 g_str_equal(key, "Security") == TRUE)
4724 dbus_message_iter_get_basic(&value, &security);
4725 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4726 g_str_equal(key, "Passphrase") == TRUE)
4727 dbus_message_iter_get_basic(&value, &passphrase);
4728 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4729 g_str_equal(key, "SSID") == TRUE)
4730 dbus_message_iter_get_basic(&value, &ssid);
4733 dbus_message_iter_next(&array);
4739 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4745 ssid_len = strlen(ssid);
4749 if (g_strcmp0(security, "none") != 0 &&
4750 g_strcmp0(security, "wep") != 0 &&
4751 g_strcmp0(security, "psk") != 0 &&
4752 g_strcmp0(security, "wpa") != 0 &&
4753 g_strcmp0(security, "rsn") != 0 &&
4754 g_strcmp0(security, "ieee8021x") != 0)
4757 device = __connman_device_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4761 ident = connman_device_get_ident(device);
4766 if (!g_strcmp0(security, "wpa") ||
4767 !g_strcmp0(security, "rsn"))
4768 group_security = "psk";
4770 group_security = security;
4772 group = wifi_build_group_name((unsigned char *) ssid,
4773 ssid_len, mode, group_security);
4777 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4779 service = lookup_by_identifier(name);
4781 if (service != NULL)
4784 network = create_hidden_wifi(device, ssid, mode, security, group);
4785 if (network != NULL)
4786 connman_network_set_group(network, group);
4788 service = lookup_by_identifier(name);
4794 if (service == NULL) {
4799 service->network_created = TRUE;
4801 if (is_connected(service) == TRUE) {
4806 if (is_connecting(service) == TRUE) {
4811 set_reconnect_state(service, FALSE);
4813 __connman_device_disconnect(device);
4815 if (passphrase != NULL) {
4816 g_free(service->passphrase);
4817 service->passphrase = g_strdup(passphrase);
4820 service->userconnect = TRUE;
4822 err = __connman_service_connect(service);
4823 if (err < 0 && err != -EINPROGRESS)
4826 service->pending = dbus_message_ref(msg);
4831 if (service != NULL && service->network_created == TRUE) {
4832 struct connman_network *network = service->network;
4834 if (network != NULL) {
4835 connman_network_set_available(network, FALSE);
4836 __connman_device_cleanup_networks(device);
4838 __connman_service_put(service);
4844 static void provision_changed(gpointer value, gpointer user_data)
4846 struct connman_service *service = value;
4847 char *path = user_data;
4849 __connman_config_provision_service_ident(service, path);
4852 void __connman_service_provision_changed(const char *ident)
4854 g_sequence_foreach(service_list, provision_changed, (void *)ident);
4857 int __connman_service_provision(DBusMessage *msg)
4859 GKeyFile *keyfile = NULL;
4860 const char *config_str = NULL;
4861 char *group = NULL, *ident = NULL;
4863 struct connman_service *service;
4867 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
4870 if (config_str == NULL || strlen(config_str) == 0)
4873 keyfile = g_key_file_new();
4875 /* populate GKeyFile with config_str */
4876 if (g_key_file_load_from_data(keyfile, config_str,
4877 strlen(config_str), 0, NULL) == FALSE) {
4883 * read only one group of settings (only one service supported, no
4886 group = g_key_file_get_start_group(keyfile);
4888 if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
4893 err = __connman_config_load_service(keyfile, group, TRUE);
4897 ident = group + strlen("service_");
4899 /* trigger service provisioning if service exists */
4900 service = lookup_by_identifier(ident);
4901 if (service != NULL)
4902 __connman_config_provision_service(service);
4904 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
4910 if (keyfile != NULL)
4911 g_key_file_free(keyfile);
4917 * __connman_service_get:
4918 * @identifier: service identifier
4920 * Look up a service by identifier or create a new one if not found
4922 static struct connman_service *service_get(const char *identifier)
4924 struct connman_service *service;
4925 GSequenceIter *iter;
4927 iter = g_hash_table_lookup(service_hash, identifier);
4929 service = g_sequence_get(iter);
4930 if (service != NULL)
4931 connman_service_ref(service);
4935 service = connman_service_create();
4936 if (service == NULL)
4939 DBG("service %p", service);
4941 service->identifier = g_strdup(identifier);
4943 iter = g_sequence_insert_sorted(service_list, service,
4944 service_compare, NULL);
4946 g_hash_table_insert(service_hash, service->identifier, iter);
4951 static int service_register(struct connman_service *service)
4953 GSequenceIter *iter;
4955 DBG("service %p", service);
4957 if (service->path != NULL)
4960 service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
4961 service->identifier);
4963 DBG("path %s", service->path);
4965 __connman_config_provision_service(service);
4967 service_load(service);
4969 g_dbus_register_interface(connection, service->path,
4970 CONNMAN_SERVICE_INTERFACE,
4971 service_methods, service_signals,
4972 NULL, service, NULL);
4974 iter = g_hash_table_lookup(service_hash, service->identifier);
4976 g_sequence_sort_changed(iter, service_compare, NULL);
4978 services_changed(TRUE);
4983 static void service_up(struct connman_ipconfig *ipconfig)
4985 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4987 DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
4989 link_changed(service);
4991 service->stats.valid = FALSE;
4992 service->stats_roaming.valid = FALSE;
4995 static void service_down(struct connman_ipconfig *ipconfig)
4997 DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
5000 static void service_lower_up(struct connman_ipconfig *ipconfig)
5002 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5004 DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
5006 stats_start(service);
5009 static void service_lower_down(struct connman_ipconfig *ipconfig)
5011 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5013 DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
5015 stats_stop(service);
5016 service_save(service);
5019 static void service_ip_bound(struct connman_ipconfig *ipconfig)
5021 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5022 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5023 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5025 DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
5027 type = __connman_ipconfig_get_config_type(ipconfig);
5028 method = __connman_ipconfig_get_method(ipconfig);
5030 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5033 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5034 method == CONNMAN_IPCONFIG_METHOD_AUTO)
5035 __connman_service_ipconfig_indicate_state(service,
5036 CONNMAN_SERVICE_STATE_READY,
5037 CONNMAN_IPCONFIG_TYPE_IPV6);
5039 settings_changed(service, ipconfig);
5042 static void service_ip_release(struct connman_ipconfig *ipconfig)
5044 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5045 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5046 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5048 DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
5050 type = __connman_ipconfig_get_config_type(ipconfig);
5051 method = __connman_ipconfig_get_method(ipconfig);
5053 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5056 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5057 method == CONNMAN_IPCONFIG_METHOD_OFF)
5058 __connman_service_ipconfig_indicate_state(service,
5059 CONNMAN_SERVICE_STATE_DISCONNECT,
5060 CONNMAN_IPCONFIG_TYPE_IPV6);
5062 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
5063 method == CONNMAN_IPCONFIG_METHOD_OFF)
5064 __connman_service_ipconfig_indicate_state(service,
5065 CONNMAN_SERVICE_STATE_DISCONNECT,
5066 CONNMAN_IPCONFIG_TYPE_IPV4);
5068 settings_changed(service, ipconfig);
5071 static const struct connman_ipconfig_ops service_ops = {
5073 .down = service_down,
5074 .lower_up = service_lower_up,
5075 .lower_down = service_lower_down,
5076 .ip_bound = service_ip_bound,
5077 .ip_release = service_ip_release,
5080 static void setup_ip4config(struct connman_service *service, int index,
5081 enum connman_ipconfig_method method)
5086 service->ipconfig_ipv4 = connman_ipconfig_create(index,
5087 CONNMAN_IPCONFIG_TYPE_IPV4);
5088 if (service->ipconfig_ipv4 == NULL)
5091 connman_ipconfig_set_method(service->ipconfig_ipv4, method);
5093 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
5095 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
5098 static void setup_ip6config(struct connman_service *service, int index)
5103 service->ipconfig_ipv6 = connman_ipconfig_create(index,
5104 CONNMAN_IPCONFIG_TYPE_IPV6);
5105 if (service->ipconfig_ipv6 == NULL)
5108 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
5110 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
5113 void __connman_service_read_ip4config(struct connman_service *service)
5117 if (service->ipconfig_ipv4 == NULL)
5120 keyfile = __connman_storage_open_profile("default");
5121 if (keyfile == NULL)
5124 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5125 service->identifier, "IPv4.");
5127 g_key_file_free(keyfile);
5130 void __connman_service_create_ip4config(struct connman_service *service,
5133 DBG("ipv4 %p", service->ipconfig_ipv4);
5135 if (service->ipconfig_ipv4 != NULL)
5138 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5139 __connman_service_read_ip4config(service);
5142 void __connman_service_read_ip6config(struct connman_service *service)
5146 if (service->ipconfig_ipv6 == NULL)
5149 keyfile = __connman_storage_open_profile("default");
5150 if (keyfile == NULL)
5153 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5154 service->identifier, "IPv6.");
5156 g_key_file_free(keyfile);
5159 void __connman_service_create_ip6config(struct connman_service *service,
5162 DBG("ipv6 %p", service->ipconfig_ipv6);
5164 if (service->ipconfig_ipv6 != NULL)
5167 setup_ip6config(service, index);
5168 __connman_service_read_ip6config(service);
5172 * __connman_service_lookup_from_network:
5173 * @network: network structure
5175 * Look up a service by network (reference count will not be increased)
5177 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
5179 struct connman_service *service;
5180 const char *ident, *group;
5183 DBG("network %p", network);
5185 ident = __connman_network_get_ident(network);
5189 group = connman_network_get_group(network);
5193 name = g_strdup_printf("%s_%s_%s",
5194 __connman_network_get_type(network), ident, group);
5195 service = lookup_by_identifier(name);
5201 struct connman_service *__connman_service_lookup_from_index(int index)
5203 struct connman_service *service;
5204 GSequenceIter *iter;
5206 iter = g_sequence_get_begin_iter(service_list);
5208 while (g_sequence_iter_is_end(iter) == FALSE) {
5209 service = g_sequence_get(iter);
5211 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
5215 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
5219 iter = g_sequence_iter_next(iter);
5225 const char *__connman_service_get_ident(struct connman_service *service)
5227 return service->identifier;
5230 const char *__connman_service_get_path(struct connman_service *service)
5232 return service->path;
5235 unsigned int __connman_service_get_order(struct connman_service *service)
5237 GSequenceIter *iter;
5239 if (service == NULL)
5242 if (service->favorite == FALSE) {
5247 iter = g_hash_table_lookup(service_hash, service->identifier);
5249 if (g_sequence_iter_get_position(iter) == 0)
5251 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
5252 service->order = 10;
5258 return service->order;
5261 static enum connman_service_type convert_network_type(struct connman_network *network)
5263 enum connman_network_type type = connman_network_get_type(network);
5266 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5267 case CONNMAN_NETWORK_TYPE_VENDOR:
5269 case CONNMAN_NETWORK_TYPE_ETHERNET:
5270 return CONNMAN_SERVICE_TYPE_ETHERNET;
5271 case CONNMAN_NETWORK_TYPE_WIFI:
5272 return CONNMAN_SERVICE_TYPE_WIFI;
5273 case CONNMAN_NETWORK_TYPE_WIMAX:
5274 return CONNMAN_SERVICE_TYPE_WIMAX;
5275 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5276 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5277 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5278 case CONNMAN_NETWORK_TYPE_CELLULAR:
5279 return CONNMAN_SERVICE_TYPE_CELLULAR;
5282 return CONNMAN_SERVICE_TYPE_UNKNOWN;
5285 static enum connman_service_security convert_wifi_security(const char *security)
5287 if (security == NULL)
5288 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5289 else if (g_str_equal(security, "none") == TRUE)
5290 return CONNMAN_SERVICE_SECURITY_NONE;
5291 else if (g_str_equal(security, "wep") == TRUE)
5292 return CONNMAN_SERVICE_SECURITY_WEP;
5293 else if (g_str_equal(security, "psk") == TRUE)
5294 return CONNMAN_SERVICE_SECURITY_PSK;
5295 else if (g_str_equal(security, "ieee8021x") == TRUE)
5296 return CONNMAN_SERVICE_SECURITY_8021X;
5297 else if (g_str_equal(security, "wpa") == TRUE)
5298 return CONNMAN_SERVICE_SECURITY_WPA;
5299 else if (g_str_equal(security, "rsn") == TRUE)
5300 return CONNMAN_SERVICE_SECURITY_RSN;
5302 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5305 static void update_from_network(struct connman_service *service,
5306 struct connman_network *network)
5308 connman_uint8_t strength = service->strength;
5309 GSequenceIter *iter;
5312 DBG("service %p network %p", service, network);
5314 if (is_connected(service) == TRUE)
5317 if (is_connecting(service) == TRUE)
5320 str = connman_network_get_string(network, "Name");
5322 g_free(service->name);
5323 service->name = g_strdup(str);
5324 service->hidden = FALSE;
5326 g_free(service->name);
5327 service->name = NULL;
5328 service->hidden = TRUE;
5331 service->strength = connman_network_get_strength(network);
5332 service->roaming = connman_network_get_bool(network, "Roaming");
5334 if (service->strength == 0) {
5336 * Filter out 0-values; it's unclear what they mean
5337 * and they cause anomalous sorting of the priority list.
5339 service->strength = strength;
5342 str = connman_network_get_string(network, "WiFi.Security");
5343 service->security = convert_wifi_security(str);
5345 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5346 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5348 if (service->strength > strength && service->network != NULL) {
5349 service->network = network;
5351 strength_changed(service);
5354 if (service->network == NULL)
5355 service->network = network;
5357 iter = g_hash_table_lookup(service_hash, service->identifier);
5359 g_sequence_sort_changed(iter, service_compare, NULL);
5363 * __connman_service_create_from_network:
5364 * @network: network structure
5366 * Look up service by network and if not found, create one
5368 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5370 struct connman_service *service;
5371 struct connman_device *device;
5372 const char *ident, *group;
5376 DBG("network %p", network);
5378 ident = __connman_network_get_ident(network);
5382 group = connman_network_get_group(network);
5386 name = g_strdup_printf("%s_%s_%s",
5387 __connman_network_get_type(network), ident, group);
5388 service = service_get(name);
5391 if (service == NULL)
5394 if (__connman_network_get_weakness(network) == TRUE)
5397 if (service->path != NULL) {
5398 update_from_network(service, network);
5399 services_changed(TRUE);
5403 service->type = convert_network_type(network);
5405 switch (service->type) {
5406 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5407 case CONNMAN_SERVICE_TYPE_SYSTEM:
5408 case CONNMAN_SERVICE_TYPE_ETHERNET:
5409 case CONNMAN_SERVICE_TYPE_WIMAX:
5410 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5411 case CONNMAN_SERVICE_TYPE_GPS:
5412 case CONNMAN_SERVICE_TYPE_VPN:
5413 case CONNMAN_SERVICE_TYPE_GADGET:
5414 service->autoconnect = FALSE;
5416 case CONNMAN_SERVICE_TYPE_WIFI:
5417 case CONNMAN_SERVICE_TYPE_CELLULAR:
5418 service->autoconnect = TRUE;
5422 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5423 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5425 update_from_network(service, network);
5427 index = connman_network_get_index(network);
5429 if (service->ipconfig_ipv4 == NULL)
5430 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5432 if (service->ipconfig_ipv6 == NULL)
5433 setup_ip6config(service, index);
5435 service_register(service);
5437 if (service->favorite == TRUE) {
5438 device = connman_network_get_device(service->network);
5439 if (device && __connman_device_scanning(device) == FALSE)
5440 __connman_service_auto_connect();
5443 __connman_notifier_service_add(service, service->name);
5448 void __connman_service_update_from_network(struct connman_network *network)
5450 struct connman_service *service;
5451 connman_uint8_t strength;
5452 connman_bool_t roaming;
5453 GSequenceIter *iter;
5455 connman_bool_t stats_enable;
5457 DBG("network %p", network);
5459 service = __connman_service_lookup_from_network(network);
5460 if (service == NULL)
5463 if (service->network == NULL)
5466 name = connman_network_get_string(service->network, "Name");
5467 if (g_strcmp0(service->name, name) != 0) {
5468 g_free(service->name);
5469 service->name = g_strdup(name);
5470 connman_dbus_property_changed_basic(service->path,
5471 CONNMAN_SERVICE_INTERFACE, "Name",
5472 DBUS_TYPE_STRING, &service->name);
5475 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5476 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5478 strength = connman_network_get_strength(service->network);
5479 if (strength == service->strength)
5482 service->strength = strength;
5484 strength_changed(service);
5487 roaming = connman_network_get_bool(service->network, "Roaming");
5488 if (roaming == service->roaming)
5491 stats_enable = stats_enabled(service);
5492 if (stats_enable == TRUE)
5493 stats_stop(service);
5495 service->roaming = roaming;
5497 if (stats_enable == TRUE)
5498 stats_start(service);
5500 roaming_changed(service);
5502 iter = g_hash_table_lookup(service_hash, service->identifier);
5504 g_sequence_sort_changed(iter, service_compare, NULL);
5507 void __connman_service_remove_from_network(struct connman_network *network)
5509 struct connman_service *service;
5511 DBG("network %p", network);
5513 service = __connman_service_lookup_from_network(network);
5514 if (service == NULL)
5517 __connman_service_put(service);
5521 * __connman_service_create_from_provider:
5522 * @provider: provider structure
5524 * Look up service by provider and if not found, create one
5526 struct connman_service *
5527 __connman_service_create_from_provider(struct connman_provider *provider)
5529 struct connman_service *service;
5530 const char *ident, *str;
5532 int index = connman_provider_get_index(provider);
5534 DBG("provider %p", provider);
5536 ident = __connman_provider_get_ident(provider);
5540 name = g_strdup_printf("vpn_%s", ident);
5541 service = service_get(name);
5544 if (service == NULL)
5547 service->type = CONNMAN_SERVICE_TYPE_VPN;
5548 service->provider = connman_provider_ref(provider);
5549 service->autoconnect = FALSE;
5551 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5553 str = connman_provider_get_string(provider, "Name");
5555 g_free(service->name);
5556 service->name = g_strdup(str);
5557 service->hidden = FALSE;
5559 g_free(service->name);
5560 service->name = NULL;
5561 service->hidden = TRUE;
5564 service->strength = 0;
5566 if (service->ipconfig_ipv4 == NULL)
5567 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5569 if (service->ipconfig_ipv6 == NULL)
5570 setup_ip6config(service, index);
5572 service_register(service);
5574 __connman_notifier_service_add(service, service->name);
5579 void __connman_service_downgrade_state(struct connman_service *service)
5581 if (service == NULL)
5584 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
5585 service->state_ipv6);
5587 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
5588 __connman_service_ipconfig_indicate_state(service,
5589 CONNMAN_SERVICE_STATE_READY,
5590 CONNMAN_IPCONFIG_TYPE_IPV4);
5592 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
5593 __connman_service_ipconfig_indicate_state(service,
5594 CONNMAN_SERVICE_STATE_READY,
5595 CONNMAN_IPCONFIG_TYPE_IPV6);
5598 int __connman_service_init(void)
5602 connection = connman_dbus_get_connection();
5604 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5607 service_list = g_sequence_new(service_free);
5612 void __connman_service_cleanup(void)
5618 list = service_list;
5619 service_list = NULL;
5620 g_sequence_free(list);
5622 g_hash_table_destroy(service_hash);
5623 service_hash = NULL;
5625 g_slist_free(counter_list);
5626 counter_list = NULL;
5628 dbus_connection_unref(connection);