5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <connman/storage.h>
36 #define CONNECT_TIMEOUT 120
38 static DBusConnection *connection = NULL;
40 static GSequence *service_list = NULL;
41 static GHashTable *service_hash = NULL;
42 static GSList *counter_list = NULL;
44 struct connman_stats {
46 connman_bool_t enabled;
47 struct connman_stats_data data_last;
48 struct connman_stats_data data;
52 struct connman_stats_counter {
53 connman_bool_t append_all;
54 struct connman_stats stats;
55 struct connman_stats stats_roaming;
58 struct connman_service {
60 int session_usage_count;
63 enum connman_service_type type;
64 enum connman_service_security security;
65 enum connman_service_state state;
66 enum connman_service_state state_ipv4;
67 enum connman_service_state state_ipv6;
68 enum connman_service_error error;
69 connman_uint8_t strength;
70 connman_bool_t favorite;
71 connman_bool_t immutable;
72 connman_bool_t hidden;
73 connman_bool_t ignore;
74 connman_bool_t autoconnect;
75 connman_bool_t userconnect;
80 char *agent_passphrase;
81 connman_bool_t roaming;
82 struct connman_ipconfig *ipconfig_ipv4;
83 struct connman_ipconfig *ipconfig_ipv6;
84 struct connman_network *network;
85 struct connman_provider *provider;
87 char **nameservers_config;
88 char **nameservers_auto;
92 /* 802.1x settings from the config files */
97 char *client_cert_file;
98 char *private_key_file;
99 char *private_key_passphrase;
101 DBusMessage *pending;
103 struct connman_stats stats;
104 struct connman_stats stats_roaming;
105 GHashTable *counter_table;
106 enum connman_service_proxy_method proxy;
107 enum connman_service_proxy_method proxy_config;
112 int online_check_count;
117 struct connman_service *service;
120 static void compare_path(gpointer value, gpointer user_data)
122 struct connman_service *service = value;
123 struct find_data *data = user_data;
125 if (data->service != NULL)
128 if (g_strcmp0(service->path, data->path) == 0)
129 data->service = service;
132 static struct connman_service *find_service(const char *path)
134 struct find_data data = { .path = path, .service = NULL };
136 DBG("path %s", path);
138 g_sequence_foreach(service_list, compare_path, &data);
143 const char *__connman_service_type2string(enum connman_service_type type)
146 case CONNMAN_SERVICE_TYPE_UNKNOWN:
148 case CONNMAN_SERVICE_TYPE_SYSTEM:
150 case CONNMAN_SERVICE_TYPE_ETHERNET:
152 case CONNMAN_SERVICE_TYPE_WIFI:
154 case CONNMAN_SERVICE_TYPE_WIMAX:
156 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
158 case CONNMAN_SERVICE_TYPE_CELLULAR:
160 case CONNMAN_SERVICE_TYPE_GPS:
162 case CONNMAN_SERVICE_TYPE_VPN:
164 case CONNMAN_SERVICE_TYPE_GADGET:
171 static const char *security2string(enum connman_service_security security)
174 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
176 case CONNMAN_SERVICE_SECURITY_NONE:
178 case CONNMAN_SERVICE_SECURITY_WEP:
180 case CONNMAN_SERVICE_SECURITY_PSK:
181 case CONNMAN_SERVICE_SECURITY_WPA:
182 case CONNMAN_SERVICE_SECURITY_RSN:
184 case CONNMAN_SERVICE_SECURITY_8021X:
191 static const char *state2string(enum connman_service_state state)
194 case CONNMAN_SERVICE_STATE_UNKNOWN:
196 case CONNMAN_SERVICE_STATE_IDLE:
198 case CONNMAN_SERVICE_STATE_ASSOCIATION:
199 return "association";
200 case CONNMAN_SERVICE_STATE_CONFIGURATION:
201 return "configuration";
202 case CONNMAN_SERVICE_STATE_READY:
204 case CONNMAN_SERVICE_STATE_ONLINE:
206 case CONNMAN_SERVICE_STATE_DISCONNECT:
208 case CONNMAN_SERVICE_STATE_FAILURE:
215 static const char *error2string(enum connman_service_error error)
218 case CONNMAN_SERVICE_ERROR_UNKNOWN:
220 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
221 return "out-of-range";
222 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
223 return "pin-missing";
224 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
225 return "dhcp-failed";
226 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
227 return "connect-failed";
228 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
229 return "login-failed";
230 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
231 return "auth-failed";
232 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
233 return "invalid-key";
239 static enum connman_service_error string2error(const char *error)
241 if (g_strcmp0(error, "dhcp-failed") == 0)
242 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
243 else if (g_strcmp0(error, "pin-missing") == 0)
244 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
245 else if (g_strcmp0(error, "invalid-key") == 0)
246 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
248 return CONNMAN_SERVICE_ERROR_UNKNOWN;
251 static const char *proxymethod2string(enum connman_service_proxy_method method)
254 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
256 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
258 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
260 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
267 static enum connman_service_proxy_method string2proxymethod(const char *method)
269 if (g_strcmp0(method, "direct") == 0)
270 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
271 else if (g_strcmp0(method, "auto") == 0)
272 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
273 else if (g_strcmp0(method, "manual") == 0)
274 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
276 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
279 static int service_load(struct connman_service *service)
282 GError *error = NULL;
285 connman_bool_t autoconnect;
286 unsigned int ssid_len;
289 DBG("service %p", service);
291 keyfile = connman_storage_load_service(service->identifier);
295 switch (service->type) {
296 case CONNMAN_SERVICE_TYPE_UNKNOWN:
297 case CONNMAN_SERVICE_TYPE_SYSTEM:
298 case CONNMAN_SERVICE_TYPE_ETHERNET:
299 case CONNMAN_SERVICE_TYPE_GPS:
300 case CONNMAN_SERVICE_TYPE_VPN:
301 case CONNMAN_SERVICE_TYPE_GADGET:
303 case CONNMAN_SERVICE_TYPE_WIFI:
304 if (service->name == NULL) {
307 name = g_key_file_get_string(keyfile,
308 service->identifier, "Name", NULL);
310 g_free(service->name);
311 service->name = name;
314 if (service->network != NULL)
315 connman_network_set_name(service->network,
319 if (service->network &&
320 connman_network_get_blob(service->network,
321 "WiFi.SSID", &ssid_len) == NULL) {
324 hex_ssid = g_key_file_get_string(keyfile,
328 if (hex_ssid != NULL) {
330 unsigned int i, j = 0, hex;
331 size_t hex_ssid_len = strlen(hex_ssid);
333 ssid = g_try_malloc0(hex_ssid_len / 2);
340 for (i = 0; i < hex_ssid_len; i += 2) {
341 sscanf(hex_ssid + i, "%02x", &hex);
345 connman_network_set_blob(service->network,
346 "WiFi.SSID", ssid, hex_ssid_len / 2);
353 case CONNMAN_SERVICE_TYPE_WIMAX:
354 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
355 case CONNMAN_SERVICE_TYPE_CELLULAR:
356 service->favorite = g_key_file_get_boolean(keyfile,
357 service->identifier, "Favorite", NULL);
359 autoconnect = g_key_file_get_boolean(keyfile,
360 service->identifier, "AutoConnect", &error);
362 service->autoconnect = autoconnect;
363 g_clear_error(&error);
365 str = g_key_file_get_string(keyfile,
366 service->identifier, "Failure", NULL);
368 if (service->favorite == FALSE)
369 service->state_ipv4 = service->state_ipv6 =
370 CONNMAN_SERVICE_STATE_FAILURE;
371 service->error = string2error(str);
377 str = g_key_file_get_string(keyfile,
378 service->identifier, "Modified", NULL);
380 g_time_val_from_iso8601(str, &service->modified);
384 str = g_key_file_get_string(keyfile,
385 service->identifier, "Passphrase", NULL);
387 g_free(service->passphrase);
388 service->passphrase = str;
391 if (service->ipconfig_ipv4 != NULL)
392 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
393 service->identifier, "IPv4.");
395 if (service->ipconfig_ipv6 != NULL)
396 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
397 service->identifier, "IPv6.");
399 service->nameservers_config = g_key_file_get_string_list(keyfile,
400 service->identifier, "Nameservers", &length, NULL);
401 if (service->nameservers_config != NULL && length == 0) {
402 g_strfreev(service->nameservers_config);
403 service->nameservers_config = NULL;
406 service->domains = g_key_file_get_string_list(keyfile,
407 service->identifier, "Domains", &length, NULL);
408 if (service->domains != NULL && length == 0) {
409 g_strfreev(service->domains);
410 service->domains = NULL;
413 str = g_key_file_get_string(keyfile,
414 service->identifier, "Proxy.Method", NULL);
416 service->proxy_config = string2proxymethod(str);
420 service->proxies = g_key_file_get_string_list(keyfile,
421 service->identifier, "Proxy.Servers", &length, NULL);
422 if (service->proxies != NULL && length == 0) {
423 g_strfreev(service->proxies);
424 service->proxies = NULL;
427 service->excludes = g_key_file_get_string_list(keyfile,
428 service->identifier, "Proxy.Excludes", &length, NULL);
429 if (service->excludes != NULL && length == 0) {
430 g_strfreev(service->excludes);
431 service->excludes = NULL;
434 str = g_key_file_get_string(keyfile,
435 service->identifier, "Proxy.URL", NULL);
437 g_free(service->pac);
442 g_key_file_free(keyfile);
447 static int service_save(struct connman_service *service)
452 const char *cst_str = NULL;
455 DBG("service %p", service);
457 keyfile = __connman_storage_open_service(service->identifier);
461 if (service->name != NULL)
462 g_key_file_set_string(keyfile, service->identifier,
463 "Name", service->name);
465 switch (service->type) {
466 case CONNMAN_SERVICE_TYPE_UNKNOWN:
467 case CONNMAN_SERVICE_TYPE_SYSTEM:
468 case CONNMAN_SERVICE_TYPE_ETHERNET:
469 case CONNMAN_SERVICE_TYPE_GPS:
470 case CONNMAN_SERVICE_TYPE_VPN:
471 case CONNMAN_SERVICE_TYPE_GADGET:
473 case CONNMAN_SERVICE_TYPE_WIFI:
474 if (service->network) {
475 const unsigned char *ssid;
476 unsigned int ssid_len = 0;
478 ssid = connman_network_get_blob(service->network,
479 "WiFi.SSID", &ssid_len);
481 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
482 char *identifier = service->identifier;
486 str = g_string_sized_new(ssid_len * 2);
492 for (i = 0; i < ssid_len; i++)
493 g_string_append_printf(str,
496 g_key_file_set_string(keyfile, identifier,
499 g_string_free(str, TRUE);
502 freq = connman_network_get_frequency(service->network);
503 g_key_file_set_integer(keyfile, service->identifier,
508 case CONNMAN_SERVICE_TYPE_WIMAX:
509 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
510 case CONNMAN_SERVICE_TYPE_CELLULAR:
511 g_key_file_set_boolean(keyfile, service->identifier,
512 "Favorite", service->favorite);
514 if (service->favorite == TRUE)
515 g_key_file_set_boolean(keyfile, service->identifier,
516 "AutoConnect", service->autoconnect);
518 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
519 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
520 const char *failure = error2string(service->error);
522 g_key_file_set_string(keyfile,
526 g_key_file_remove_key(keyfile, service->identifier,
532 str = g_time_val_to_iso8601(&service->modified);
534 g_key_file_set_string(keyfile, service->identifier,
539 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
540 g_key_file_set_string(keyfile, service->identifier,
541 "Passphrase", service->passphrase);
543 g_key_file_remove_key(keyfile, service->identifier,
546 if (service->ipconfig_ipv4 != NULL)
547 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
548 service->identifier, "IPv4.");
550 if (service->ipconfig_ipv6 != NULL)
551 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
552 service->identifier, "IPv6.");
554 if (service->nameservers_config != NULL) {
555 guint len = g_strv_length(service->nameservers_config);
557 g_key_file_set_string_list(keyfile, service->identifier,
559 (const gchar **) service->nameservers_config, len);
561 g_key_file_remove_key(keyfile, service->identifier,
562 "Nameservers", NULL);
564 if (service->domains != NULL) {
565 guint len = g_strv_length(service->domains);
567 g_key_file_set_string_list(keyfile, service->identifier,
569 (const gchar **) service->domains, len);
571 g_key_file_remove_key(keyfile, service->identifier,
574 cst_str = proxymethod2string(service->proxy_config);
576 g_key_file_set_string(keyfile, service->identifier,
577 "Proxy.Method", cst_str);
579 if (service->proxies != NULL) {
580 guint len = g_strv_length(service->proxies);
582 g_key_file_set_string_list(keyfile, service->identifier,
584 (const gchar **) service->proxies, len);
586 g_key_file_remove_key(keyfile, service->identifier,
587 "Proxy.Servers", NULL);
589 if (service->excludes != NULL) {
590 guint len = g_strv_length(service->excludes);
592 g_key_file_set_string_list(keyfile, service->identifier,
594 (const gchar **) service->excludes, len);
596 g_key_file_remove_key(keyfile, service->identifier,
597 "Proxy.Excludes", NULL);
599 if (service->pac != NULL && strlen(service->pac) > 0)
600 g_key_file_set_string(keyfile, service->identifier,
601 "Proxy.URL", service->pac);
603 g_key_file_remove_key(keyfile, service->identifier,
607 __connman_storage_save_service(keyfile, service->identifier);
609 g_key_file_free(keyfile);
614 static enum connman_service_state combine_state(
615 enum connman_service_state state_a,
616 enum connman_service_state state_b)
618 enum connman_service_state result;
620 if (state_a == state_b) {
625 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
630 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
635 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
640 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
645 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
650 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
655 if (state_a == CONNMAN_SERVICE_STATE_READY) {
660 if (state_b == CONNMAN_SERVICE_STATE_READY) {
665 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
670 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
675 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
680 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
685 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
690 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
695 result = CONNMAN_SERVICE_STATE_FAILURE;
701 static connman_bool_t is_connecting_state(struct connman_service *service,
702 enum connman_service_state state)
705 case CONNMAN_SERVICE_STATE_UNKNOWN:
706 case CONNMAN_SERVICE_STATE_IDLE:
707 case CONNMAN_SERVICE_STATE_FAILURE:
708 if (service->network != NULL)
709 return connman_network_get_connecting(service->network);
710 case CONNMAN_SERVICE_STATE_DISCONNECT:
711 case CONNMAN_SERVICE_STATE_READY:
712 case CONNMAN_SERVICE_STATE_ONLINE:
714 case CONNMAN_SERVICE_STATE_ASSOCIATION:
715 case CONNMAN_SERVICE_STATE_CONFIGURATION:
722 static connman_bool_t is_connected_state(const struct connman_service *service,
723 enum connman_service_state state)
726 case CONNMAN_SERVICE_STATE_UNKNOWN:
727 case CONNMAN_SERVICE_STATE_IDLE:
728 case CONNMAN_SERVICE_STATE_ASSOCIATION:
729 case CONNMAN_SERVICE_STATE_CONFIGURATION:
730 case CONNMAN_SERVICE_STATE_DISCONNECT:
731 case CONNMAN_SERVICE_STATE_FAILURE:
733 case CONNMAN_SERVICE_STATE_READY:
734 case CONNMAN_SERVICE_STATE_ONLINE:
741 static connman_bool_t is_connecting(struct connman_service *service)
743 return is_connecting_state(service, service->state);
746 static connman_bool_t is_connected(struct connman_service *service)
748 return is_connected_state(service, service->state);
751 static void update_nameservers(struct connman_service *service)
755 if (service->ipconfig_ipv4)
756 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv4);
757 else if (service->ipconfig_ipv6)
758 ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv6);
765 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
766 case CONNMAN_SERVICE_STATE_UNKNOWN:
767 case CONNMAN_SERVICE_STATE_IDLE:
768 case CONNMAN_SERVICE_STATE_ASSOCIATION:
769 case CONNMAN_SERVICE_STATE_CONFIGURATION:
771 case CONNMAN_SERVICE_STATE_FAILURE:
772 case CONNMAN_SERVICE_STATE_DISCONNECT:
773 connman_resolver_remove_all(ifname);
775 case CONNMAN_SERVICE_STATE_READY:
776 case CONNMAN_SERVICE_STATE_ONLINE:
780 if (service->nameservers_config != NULL) {
783 for (i = 0; service->nameservers_config[i] != NULL; i++) {
784 connman_resolver_append(ifname, NULL,
785 service->nameservers_config[i]);
787 } else if (service->nameservers != NULL) {
790 for (i = 0; service->nameservers[i] != NULL; i++) {
791 connman_resolver_append(ifname, NULL,
792 service->nameservers[i]);
796 if (service->domains != NULL) {
799 for (i = 0; service->domains[i]; i++)
800 connman_resolver_append(ifname, service->domains[i],
802 } else if (service->domainname != NULL)
803 connman_resolver_append(ifname, service->domainname, NULL);
805 connman_resolver_flush();
809 * The is_auto variable is set to true when IPv6 autoconf nameservers are
810 * inserted to resolver via netlink message (see rtnl.c:rtnl_newnduseropt()
811 * for details) and not through service.c
813 int __connman_service_nameserver_append(struct connman_service *service,
814 const char *nameserver, gboolean is_auto)
819 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
821 if (nameserver == NULL)
825 nameservers = service->nameservers_auto;
827 nameservers = service->nameservers;
829 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
830 if (g_strcmp0(nameservers[i], nameserver) == 0)
833 if (nameservers != NULL) {
834 len = g_strv_length(nameservers);
835 nameservers = g_try_renew(char *, nameservers, len + 2);
838 nameservers = g_try_new0(char *, len + 2);
841 if (nameservers == NULL)
844 nameservers[len] = g_strdup(nameserver);
845 if (nameservers[len] == NULL)
848 nameservers[len + 1] = NULL;
850 if (is_auto == TRUE) {
851 service->nameservers_auto = nameservers;
853 service->nameservers = nameservers;
854 update_nameservers(service);
860 int __connman_service_nameserver_remove(struct connman_service *service,
861 const char *nameserver, gboolean is_auto)
863 char **servers, **nameservers;
864 gboolean found = FALSE;
867 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
869 if (nameserver == NULL)
873 nameservers = service->nameservers_auto;
875 nameservers = service->nameservers;
877 if (nameservers == NULL)
880 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
881 if (g_strcmp0(nameservers[i], nameserver) == 0) {
889 len = g_strv_length(nameservers);
892 g_strfreev(nameservers);
894 service->nameservers_auto = NULL;
896 service->nameservers = NULL;
901 servers = g_try_new0(char *, len);
905 for (i = 0, j = 0; i < len; i++) {
906 if (g_strcmp0(nameservers[i], nameserver) != 0) {
907 servers[j] = g_strdup(nameservers[i]);
908 if (servers[j] == NULL)
913 servers[len - 1] = NULL;
915 g_strfreev(nameservers);
916 nameservers = servers;
918 if (is_auto == TRUE) {
919 service->nameservers_auto = nameservers;
921 service->nameservers = nameservers;
922 update_nameservers(service);
928 void __connman_service_nameserver_clear(struct connman_service *service)
930 g_strfreev(service->nameservers);
931 service->nameservers = NULL;
933 update_nameservers(service);
936 static void nameserver_add_routes(int index, char **nameservers,
940 struct addrinfo hints;
941 struct addrinfo *addr;
943 for (i = 0; nameservers[i] != NULL; i++) {
944 memset(&hints, 0, sizeof(struct addrinfo));
945 hints.ai_flags = AI_NUMERICHOST;
948 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
949 if (ret == EAI_NONAME)
950 family = AF_INET; /* use the IPv4 as a default */
954 family = addr->ai_family;
956 if (family == AF_INET) {
957 if (connman_inet_compare_subnet(index,
958 nameservers[i]) != TRUE)
959 connman_inet_add_host_route(index,
961 } else if (family == AF_INET6)
962 connman_inet_add_ipv6_host_route(index,
969 static void nameserver_del_routes(int index, char **nameservers)
972 struct addrinfo hints;
973 struct addrinfo *addr;
975 for (i = 0; nameservers[i] != NULL; i++) {
976 memset(&hints, 0, sizeof(struct addrinfo));
977 hints.ai_flags = AI_NUMERICHOST;
980 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
981 if (ret == EAI_NONAME)
982 family = AF_INET; /* use the IPv4 as a default */
986 family = addr->ai_family;
988 if (family == AF_INET)
989 connman_inet_del_host_route(index, nameservers[i]);
990 else if (family == AF_INET6)
991 connman_inet_del_ipv6_host_route(index,
998 void __connman_service_nameserver_add_routes(struct connman_service *service,
1003 if (service == NULL)
1006 if (service->network != NULL)
1007 index = connman_network_get_index(service->network);
1008 else if (service->provider != NULL)
1009 index = connman_provider_get_index(service->provider);
1011 if (service->nameservers_config != NULL) {
1013 * Configured nameserver takes preference over the
1014 * discoverd nameserver gathered from DHCP, VPN, etc.
1016 nameserver_add_routes(index, service->nameservers_config, gw);
1017 } else if (service->nameservers != NULL) {
1019 * We add nameservers host routes for nameservers that
1020 * are not on our subnet. For those who are, the subnet
1021 * route will be installed by the time the dns proxy code
1022 * tries to reach them. The subnet route is installed
1023 * when setting the interface IP address.
1025 nameserver_add_routes(index, service->nameservers, gw);
1029 void __connman_service_nameserver_del_routes(struct connman_service *service)
1033 if (service == NULL)
1036 if (service->network != NULL)
1037 index = connman_network_get_index(service->network);
1038 else if (service->provider != NULL)
1039 index = connman_provider_get_index(service->provider);
1041 if (service->nameservers_config != NULL)
1042 nameserver_del_routes(index, service->nameservers_config);
1043 else if (service->nameservers != NULL)
1044 nameserver_del_routes(index, service->nameservers);
1047 static struct connman_stats *stats_get(struct connman_service *service)
1049 if (service->roaming == TRUE)
1050 return &service->stats_roaming;
1052 return &service->stats;
1055 static connman_bool_t stats_enabled(struct connman_service *service)
1057 struct connman_stats *stats = stats_get(service);
1059 return stats->enabled;
1062 static void stats_start(struct connman_service *service)
1064 struct connman_stats *stats = stats_get(service);
1066 DBG("service %p", service);
1068 if (stats->timer == NULL)
1071 stats->enabled = TRUE;
1072 stats->data_last.time = stats->data.time;
1074 g_timer_start(stats->timer);
1077 static void stats_stop(struct connman_service *service)
1079 struct connman_stats *stats = stats_get(service);
1080 unsigned int seconds;
1082 DBG("service %p", service);
1084 if (stats->timer == NULL)
1087 if (stats->enabled == FALSE)
1090 g_timer_stop(stats->timer);
1092 seconds = g_timer_elapsed(stats->timer, NULL);
1093 stats->data.time = stats->data_last.time + seconds;
1095 stats->enabled = FALSE;
1098 static void reset_stats(struct connman_service *service)
1100 DBG("service %p", service);
1103 service->stats.valid = FALSE;
1105 service->stats.data.rx_packets = 0;
1106 service->stats.data.tx_packets = 0;
1107 service->stats.data.rx_bytes = 0;
1108 service->stats.data.tx_bytes = 0;
1109 service->stats.data.rx_errors = 0;
1110 service->stats.data.tx_errors = 0;
1111 service->stats.data.rx_dropped = 0;
1112 service->stats.data.tx_dropped = 0;
1113 service->stats.data.time = 0;
1114 service->stats.data_last.time = 0;
1116 g_timer_reset(service->stats.timer);
1119 service->stats_roaming.valid = FALSE;
1121 service->stats_roaming.data.rx_packets = 0;
1122 service->stats_roaming.data.tx_packets = 0;
1123 service->stats_roaming.data.rx_bytes = 0;
1124 service->stats_roaming.data.tx_bytes = 0;
1125 service->stats_roaming.data.rx_errors = 0;
1126 service->stats_roaming.data.tx_errors = 0;
1127 service->stats_roaming.data.rx_dropped = 0;
1128 service->stats_roaming.data.tx_dropped = 0;
1129 service->stats_roaming.data.time = 0;
1130 service->stats_roaming.data_last.time = 0;
1132 g_timer_reset(service->stats_roaming.timer);
1135 static struct connman_service *get_default(void)
1137 struct connman_service *service;
1138 GSequenceIter *iter;
1140 iter = g_sequence_get_begin_iter(service_list);
1142 if (g_sequence_iter_is_end(iter) == TRUE)
1145 service = g_sequence_get(iter);
1147 if (is_connected(service) == FALSE)
1153 static void default_changed(void)
1155 struct connman_service *service = get_default();
1157 __connman_notifier_default_changed(service);
1160 static void state_changed(struct connman_service *service)
1164 __connman_notifier_service_state_changed(service, service->state);
1166 str = state2string(service->state);
1170 connman_dbus_property_changed_basic(service->path,
1171 CONNMAN_SERVICE_INTERFACE, "State",
1172 DBUS_TYPE_STRING, &str);
1175 static void strength_changed(struct connman_service *service)
1177 if (service->strength == 0)
1180 connman_dbus_property_changed_basic(service->path,
1181 CONNMAN_SERVICE_INTERFACE, "Strength",
1182 DBUS_TYPE_BYTE, &service->strength);
1185 static void favorite_changed(struct connman_service *service)
1187 if (service->path == NULL)
1190 connman_dbus_property_changed_basic(service->path,
1191 CONNMAN_SERVICE_INTERFACE, "Favorite",
1192 DBUS_TYPE_BOOLEAN, &service->favorite);
1195 static void immutable_changed(struct connman_service *service)
1197 if (service->path == NULL)
1200 connman_dbus_property_changed_basic(service->path,
1201 CONNMAN_SERVICE_INTERFACE, "Immutable",
1202 DBUS_TYPE_BOOLEAN, &service->immutable);
1205 static void roaming_changed(struct connman_service *service)
1207 if (service->path == NULL)
1210 connman_dbus_property_changed_basic(service->path,
1211 CONNMAN_SERVICE_INTERFACE, "Roaming",
1212 DBUS_TYPE_BOOLEAN, &service->roaming);
1215 static void autoconnect_changed(struct connman_service *service)
1217 if (service->path == NULL)
1220 connman_dbus_property_changed_basic(service->path,
1221 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
1222 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1225 static void append_security(DBusMessageIter *iter, void *user_data)
1227 struct connman_service *service = user_data;
1230 str = security2string(service->security);
1232 dbus_message_iter_append_basic(iter,
1233 DBUS_TYPE_STRING, &str);
1236 * Some access points incorrectly advertise WPS even when they
1237 * are configured as open or no security, so filter
1240 if (service->wps == TRUE) {
1241 switch (service->security) {
1242 case CONNMAN_SERVICE_SECURITY_PSK:
1243 case CONNMAN_SERVICE_SECURITY_WPA:
1244 case CONNMAN_SERVICE_SECURITY_RSN:
1246 dbus_message_iter_append_basic(iter,
1247 DBUS_TYPE_STRING, &str);
1249 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1250 case CONNMAN_SERVICE_SECURITY_NONE:
1251 case CONNMAN_SERVICE_SECURITY_WEP:
1252 case CONNMAN_SERVICE_SECURITY_8021X:
1258 static void append_ethernet(DBusMessageIter *iter, void *user_data)
1260 struct connman_service *service = user_data;
1262 if (service->ipconfig_ipv4 != NULL)
1263 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
1265 else if (service->ipconfig_ipv6 != NULL)
1266 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
1270 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1272 struct connman_service *service = user_data;
1274 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1275 state2string(service->state_ipv4));
1277 if (is_connected_state(service, service->state_ipv4) == FALSE)
1280 if (service->ipconfig_ipv4 != NULL)
1281 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1284 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1286 struct connman_service *service = user_data;
1288 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1289 state2string(service->state_ipv6));
1291 if (is_connected_state(service, service->state_ipv6) == FALSE)
1294 if (service->ipconfig_ipv6 != NULL)
1295 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1296 service->ipconfig_ipv4);
1299 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1301 struct connman_service *service = user_data;
1303 if (service->ipconfig_ipv4 != NULL)
1304 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1308 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1310 struct connman_service *service = user_data;
1312 if (service->ipconfig_ipv6 != NULL)
1313 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1317 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1322 servers = *nameservers;
1324 for (i = 0; servers[i] != NULL; i++) {
1325 dbus_message_iter_append_basic(iter,
1326 DBUS_TYPE_STRING, &servers[i]);
1330 static void append_dns(DBusMessageIter *iter, void *user_data)
1332 struct connman_service *service = user_data;
1334 if (is_connected(service) == FALSE)
1337 if (service->nameservers_config != NULL) {
1338 append_nameserver(iter, &service->nameservers_config);
1341 if (service->nameservers != NULL)
1342 append_nameserver(iter, &service->nameservers);
1344 if (service->nameservers_auto != NULL)
1345 append_nameserver(iter, &service->nameservers_auto);
1349 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1351 struct connman_service *service = user_data;
1354 if (service->nameservers_config == NULL)
1357 for (i = 0; service->nameservers_config[i]; i++) {
1358 dbus_message_iter_append_basic(iter,
1360 &service->nameservers_config[i]);
1364 static void append_domain(DBusMessageIter *iter, void *user_data)
1366 struct connman_service *service = user_data;
1368 if (is_connected(service) == FALSE &&
1369 is_connecting(service) == FALSE)
1372 if (service->domainname == NULL)
1375 dbus_message_iter_append_basic(iter,
1376 DBUS_TYPE_STRING, &service->domainname);
1379 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1381 struct connman_service *service = user_data;
1384 if (service->domains == NULL)
1387 for (i = 0; service->domains[i]; i++)
1388 dbus_message_iter_append_basic(iter,
1389 DBUS_TYPE_STRING, &service->domains[i]);
1392 static void append_proxies(DBusMessageIter *iter, void *user_data)
1394 struct connman_service *service = user_data;
1397 if (service->proxies == NULL)
1400 for (i = 0; service->proxies[i]; i++)
1401 dbus_message_iter_append_basic(iter,
1402 DBUS_TYPE_STRING, &service->proxies[i]);
1405 static void append_excludes(DBusMessageIter *iter, void *user_data)
1407 struct connman_service *service = user_data;
1410 if (service->excludes == NULL)
1413 for (i = 0; service->excludes[i]; i++)
1414 dbus_message_iter_append_basic(iter,
1415 DBUS_TYPE_STRING, &service->excludes[i]);
1418 static void append_proxy(DBusMessageIter *iter, void *user_data)
1420 struct connman_service *service = user_data;
1421 enum connman_service_proxy_method proxy;
1422 const char *pac = NULL;
1423 const char *method = proxymethod2string(
1424 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1428 if (is_connected(service) == FALSE)
1431 proxy = connman_service_get_proxy_method(service);
1434 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1436 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1438 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1439 connman_dbus_dict_append_array(iter, "Servers",
1440 DBUS_TYPE_STRING, append_proxies,
1443 connman_dbus_dict_append_array(iter, "Excludes",
1444 DBUS_TYPE_STRING, append_excludes,
1447 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1448 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1449 if (service->ipconfig_ipv4 != NULL)
1450 pac = __connman_ipconfig_get_proxy_autoconfig(
1451 service->ipconfig_ipv4);
1452 else if (service->ipconfig_ipv6 != NULL)
1453 pac = __connman_ipconfig_get_proxy_autoconfig(
1454 service->ipconfig_ipv6);
1456 if (service->pac == NULL && pac == NULL)
1459 if (service->pac != NULL)
1462 connman_dbus_dict_append_basic(iter, "URL",
1463 DBUS_TYPE_STRING, &pac);
1467 method = proxymethod2string(proxy);
1470 connman_dbus_dict_append_basic(iter, "Method",
1471 DBUS_TYPE_STRING, &method);
1474 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1476 struct connman_service *service = user_data;
1479 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1482 switch (service->proxy_config) {
1483 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1485 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1487 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1488 if (service->proxies != NULL)
1489 connman_dbus_dict_append_array(iter, "Servers",
1491 append_proxies, service);
1493 if (service->excludes != NULL)
1494 connman_dbus_dict_append_array(iter, "Excludes",
1496 append_excludes, service);
1498 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1499 if (service->pac != NULL)
1500 connman_dbus_dict_append_basic(iter, "URL",
1501 DBUS_TYPE_STRING, &service->pac);
1505 method = proxymethod2string(service->proxy_config);
1507 connman_dbus_dict_append_basic(iter, "Method",
1508 DBUS_TYPE_STRING, &method);
1511 static void append_provider(DBusMessageIter *iter, void *user_data)
1513 struct connman_service *service = user_data;
1515 DBG("%p %p", service, service->provider);
1517 if (is_connected(service) == FALSE)
1520 if (service->provider != NULL)
1521 __connman_provider_append_properties(service->provider, iter);
1525 static void settings_changed(struct connman_service *service,
1526 struct connman_ipconfig *ipconfig)
1528 connman_dbus_property_changed_dict(service->path,
1529 CONNMAN_SERVICE_INTERFACE, "IPv4",
1530 append_ipv4, service);
1532 connman_dbus_property_changed_dict(service->path,
1533 CONNMAN_SERVICE_INTERFACE, "IPv6",
1534 append_ipv6, service);
1536 __connman_notifier_ipconfig_changed(service, ipconfig);
1539 static void ipv4_configuration_changed(struct connman_service *service)
1541 connman_dbus_property_changed_dict(service->path,
1542 CONNMAN_SERVICE_INTERFACE,
1543 "IPv4.Configuration",
1548 static void ipv6_configuration_changed(struct connman_service *service)
1550 connman_dbus_property_changed_dict(service->path,
1551 CONNMAN_SERVICE_INTERFACE,
1552 "IPv6.Configuration",
1557 static void dns_changed(struct connman_service *service)
1559 connman_dbus_property_changed_array(service->path,
1560 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1561 DBUS_TYPE_STRING, append_dns, service);
1564 static void dns_configuration_changed(struct connman_service *service)
1566 connman_dbus_property_changed_array(service->path,
1567 CONNMAN_SERVICE_INTERFACE,
1568 "Nameservers.Configuration",
1569 DBUS_TYPE_STRING, append_dnsconfig, service);
1571 dns_changed(service);
1574 static void domain_changed(struct connman_service *service)
1576 connman_dbus_property_changed_array(service->path,
1577 CONNMAN_SERVICE_INTERFACE, "Domains",
1578 DBUS_TYPE_STRING, append_domain, service);
1581 static void domain_configuration_changed(struct connman_service *service)
1583 connman_dbus_property_changed_array(service->path,
1584 CONNMAN_SERVICE_INTERFACE,
1585 "Domains.Configuration",
1586 DBUS_TYPE_STRING, append_domainconfig, service);
1589 static void proxy_changed(struct connman_service *service)
1591 connman_dbus_property_changed_dict(service->path,
1592 CONNMAN_SERVICE_INTERFACE, "Proxy",
1593 append_proxy, service);
1596 static void proxy_configuration_changed(struct connman_service *service)
1598 connman_dbus_property_changed_dict(service->path,
1599 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1600 append_proxyconfig, service);
1602 proxy_changed(service);
1605 static void link_changed(struct connman_service *service)
1607 connman_dbus_property_changed_dict(service->path,
1608 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1609 append_ethernet, service);
1612 static void stats_append_counters(DBusMessageIter *dict,
1613 struct connman_stats_data *stats,
1614 struct connman_stats_data *counters,
1615 connman_bool_t append_all)
1617 if (counters->rx_packets != stats->rx_packets || append_all) {
1618 counters->rx_packets = stats->rx_packets;
1619 connman_dbus_dict_append_basic(dict, "RX.Packets",
1620 DBUS_TYPE_UINT32, &stats->rx_packets);
1623 if (counters->tx_packets != stats->tx_packets || append_all) {
1624 counters->tx_packets = stats->tx_packets;
1625 connman_dbus_dict_append_basic(dict, "TX.Packets",
1626 DBUS_TYPE_UINT32, &stats->tx_packets);
1629 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1630 counters->rx_bytes = stats->rx_bytes;
1631 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1632 DBUS_TYPE_UINT32, &stats->rx_bytes);
1635 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1636 counters->tx_bytes = stats->tx_bytes;
1637 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1638 DBUS_TYPE_UINT32, &stats->tx_bytes);
1641 if (counters->rx_errors != stats->rx_errors || append_all) {
1642 counters->rx_errors = stats->rx_errors;
1643 connman_dbus_dict_append_basic(dict, "RX.Errors",
1644 DBUS_TYPE_UINT32, &stats->rx_errors);
1647 if (counters->tx_errors != stats->tx_errors || append_all) {
1648 counters->tx_errors = stats->tx_errors;
1649 connman_dbus_dict_append_basic(dict, "TX.Errors",
1650 DBUS_TYPE_UINT32, &stats->tx_errors);
1653 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1654 counters->rx_dropped = stats->rx_dropped;
1655 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1656 DBUS_TYPE_UINT32, &stats->rx_dropped);
1659 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1660 counters->tx_dropped = stats->tx_dropped;
1661 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1662 DBUS_TYPE_UINT32, &stats->tx_dropped);
1665 if (counters->time != stats->time || append_all) {
1666 counters->time = stats->time;
1667 connman_dbus_dict_append_basic(dict, "Time",
1668 DBUS_TYPE_UINT32, &stats->time);
1672 static void stats_append(struct connman_service *service,
1673 const char *counter,
1674 struct connman_stats_counter *counters,
1675 connman_bool_t append_all)
1677 DBusMessageIter array, dict;
1680 DBG("service %p counter %s", service, counter);
1682 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1686 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1687 &service->path, DBUS_TYPE_INVALID);
1689 dbus_message_iter_init_append(msg, &array);
1692 connman_dbus_dict_open(&array, &dict);
1694 stats_append_counters(&dict, &service->stats.data,
1695 &counters->stats.data, append_all);
1697 connman_dbus_dict_close(&array, &dict);
1699 /* roaming counter */
1700 connman_dbus_dict_open(&array, &dict);
1702 stats_append_counters(&dict, &service->stats_roaming.data,
1703 &counters->stats_roaming.data, append_all);
1705 connman_dbus_dict_close(&array, &dict);
1707 __connman_counter_send_usage(counter, msg);
1710 static void stats_update(struct connman_service *service,
1711 unsigned int rx_packets, unsigned int tx_packets,
1712 unsigned int rx_bytes, unsigned int tx_bytes,
1713 unsigned int rx_errors, unsigned int tx_errors,
1714 unsigned int rx_dropped, unsigned int tx_dropped)
1716 struct connman_stats *stats = stats_get(service);
1717 struct connman_stats_data *data_last = &stats->data_last;
1718 struct connman_stats_data *data = &stats->data;
1719 unsigned int seconds;
1721 DBG("service %p", service);
1723 if (stats->valid == TRUE) {
1725 rx_packets - data_last->rx_packets;
1727 tx_packets - data_last->tx_packets;
1729 rx_bytes - data_last->rx_bytes;
1731 tx_bytes - data_last->tx_bytes;
1733 rx_errors - data_last->rx_errors;
1735 tx_errors - data_last->tx_errors;
1737 rx_dropped - data_last->rx_dropped;
1739 tx_dropped - data_last->tx_dropped;
1741 stats->valid = TRUE;
1744 data_last->rx_packets = rx_packets;
1745 data_last->tx_packets = tx_packets;
1746 data_last->rx_bytes = rx_bytes;
1747 data_last->tx_bytes = tx_bytes;
1748 data_last->rx_errors = rx_errors;
1749 data_last->tx_errors = tx_errors;
1750 data_last->rx_dropped = rx_dropped;
1751 data_last->tx_dropped = tx_dropped;
1753 seconds = g_timer_elapsed(stats->timer, NULL);
1754 stats->data.time = stats->data_last.time + seconds;
1757 void __connman_service_notify(struct connman_service *service,
1758 unsigned int rx_packets, unsigned int tx_packets,
1759 unsigned int rx_bytes, unsigned int tx_bytes,
1760 unsigned int rx_errors, unsigned int tx_errors,
1761 unsigned int rx_dropped, unsigned int tx_dropped)
1763 GHashTableIter iter;
1764 gpointer key, value;
1765 const char *counter;
1766 struct connman_stats_counter *counters;
1767 struct connman_stats_data *data;
1770 if (service == NULL)
1773 if (is_connected(service) == FALSE)
1776 stats_update(service,
1777 rx_packets, tx_packets,
1779 rx_errors, tx_errors,
1780 rx_dropped, tx_dropped);
1782 data = &stats_get(service)->data;
1783 err = __connman_stats_update(service, service->roaming, data);
1785 connman_error("Failed to store statistics for %s",
1786 service->identifier);
1788 g_hash_table_iter_init(&iter, service->counter_table);
1789 while (g_hash_table_iter_next(&iter, &key, &value)) {
1793 stats_append(service, counter, counters, counters->append_all);
1794 counters->append_all = FALSE;
1798 int __connman_service_counter_register(const char *counter)
1800 struct connman_service *service;
1801 GSequenceIter *iter;
1802 struct connman_stats_counter *counters;
1804 DBG("counter %s", counter);
1806 counter_list = g_slist_append(counter_list, (gpointer)counter);
1808 iter = g_sequence_get_begin_iter(service_list);
1810 while (g_sequence_iter_is_end(iter) == FALSE) {
1811 service = g_sequence_get(iter);
1813 counters = g_try_new0(struct connman_stats_counter, 1);
1814 if (counters == NULL)
1817 counters->append_all = TRUE;
1819 g_hash_table_replace(service->counter_table, (gpointer)counter,
1822 iter = g_sequence_iter_next(iter);
1828 void __connman_service_counter_unregister(const char *counter)
1830 struct connman_service *service;
1831 GSequenceIter *iter;
1833 DBG("counter %s", counter);
1835 iter = g_sequence_get_begin_iter(service_list);
1837 while (g_sequence_iter_is_end(iter) == FALSE) {
1838 service = g_sequence_get(iter);
1840 g_hash_table_remove(service->counter_table, counter);
1842 iter = g_sequence_iter_next(iter);
1845 counter_list = g_slist_remove(counter_list, counter);
1848 GSequence *__connman_service_get_list(struct connman_session *session,
1849 service_match_cb service_match,
1850 create_service_entry_cb create_service_entry,
1851 GDestroyNotify destroy_service_entry)
1854 GSequenceIter *iter;
1855 struct connman_service *service;
1856 struct service_entry *entry;
1858 list = g_sequence_new(destroy_service_entry);
1862 iter = g_sequence_get_begin_iter(service_list);
1864 while (g_sequence_iter_is_end(iter) == FALSE) {
1865 service = g_sequence_get(iter);
1867 if (service_match(session, service) == TRUE) {
1868 entry = create_service_entry(service, service->name,
1873 g_sequence_append(list, entry);
1876 iter = g_sequence_iter_next(iter);
1882 void __connman_service_session_inc(struct connman_service *service)
1884 DBG("service %p ref count %d", service,
1885 service->session_usage_count + 1);
1887 __sync_fetch_and_add(&service->session_usage_count, 1);
1890 connman_bool_t __connman_service_session_dec(struct connman_service *service)
1892 DBG("service %p ref count %d", service,
1893 service->session_usage_count - 1);
1895 if (__sync_fetch_and_sub(&service->session_usage_count, 1) != 1)
1901 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
1902 struct connman_service *service)
1906 str = __connman_service_type2string(service->type);
1908 connman_dbus_dict_append_basic(dict, "Type",
1909 DBUS_TYPE_STRING, &str);
1911 connman_dbus_dict_append_array(dict, "Security",
1912 DBUS_TYPE_STRING, append_security, service);
1914 str = state2string(service->state);
1916 connman_dbus_dict_append_basic(dict, "State",
1917 DBUS_TYPE_STRING, &str);
1919 str = error2string(service->error);
1921 connman_dbus_dict_append_basic(dict, "Error",
1922 DBUS_TYPE_STRING, &str);
1924 if (service->strength > 0)
1925 connman_dbus_dict_append_basic(dict, "Strength",
1926 DBUS_TYPE_BYTE, &service->strength);
1928 connman_dbus_dict_append_basic(dict, "Favorite",
1929 DBUS_TYPE_BOOLEAN, &service->favorite);
1931 connman_dbus_dict_append_basic(dict, "Immutable",
1932 DBUS_TYPE_BOOLEAN, &service->immutable);
1934 if (service->favorite == TRUE)
1935 connman_dbus_dict_append_basic(dict, "AutoConnect",
1936 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1938 connman_dbus_dict_append_basic(dict, "AutoConnect",
1939 DBUS_TYPE_BOOLEAN, &service->favorite);
1941 if (service->name != NULL)
1942 connman_dbus_dict_append_basic(dict, "Name",
1943 DBUS_TYPE_STRING, &service->name);
1945 switch (service->type) {
1946 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1947 case CONNMAN_SERVICE_TYPE_SYSTEM:
1948 case CONNMAN_SERVICE_TYPE_GPS:
1949 case CONNMAN_SERVICE_TYPE_VPN:
1950 case CONNMAN_SERVICE_TYPE_GADGET:
1952 case CONNMAN_SERVICE_TYPE_CELLULAR:
1953 connman_dbus_dict_append_basic(dict, "Roaming",
1954 DBUS_TYPE_BOOLEAN, &service->roaming);
1956 connman_dbus_dict_append_dict(dict, "Ethernet",
1957 append_ethernet, service);
1959 case CONNMAN_SERVICE_TYPE_WIFI:
1960 case CONNMAN_SERVICE_TYPE_ETHERNET:
1961 case CONNMAN_SERVICE_TYPE_WIMAX:
1962 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1963 connman_dbus_dict_append_dict(dict, "Ethernet",
1964 append_ethernet, service);
1968 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
1970 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
1971 append_ipv4config, service);
1973 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
1975 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
1976 append_ipv6config, service);
1978 connman_dbus_dict_append_array(dict, "Nameservers",
1979 DBUS_TYPE_STRING, append_dns, service);
1981 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
1982 DBUS_TYPE_STRING, append_dnsconfig, service);
1984 connman_dbus_dict_append_array(dict, "Domains",
1985 DBUS_TYPE_STRING, append_domain, service);
1987 connman_dbus_dict_append_array(dict, "Domains.Configuration",
1988 DBUS_TYPE_STRING, append_domainconfig, service);
1990 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
1992 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
1993 append_proxyconfig, service);
1995 connman_dbus_dict_append_dict(dict, "Provider",
1996 append_provider, service);
1999 static void append_struct_service(DBusMessageIter *iter,
2000 connman_dbus_append_cb_t function,
2001 struct connman_service *service)
2003 DBusMessageIter entry, dict;
2005 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
2007 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2010 connman_dbus_dict_open(&entry, &dict);
2011 if (function != NULL)
2012 function(&dict, service);
2013 connman_dbus_dict_close(&entry, &dict);
2015 dbus_message_iter_close_container(iter, &entry);
2018 static void append_dict_properties(DBusMessageIter *dict, void *user_data)
2020 struct connman_service *service = user_data;
2022 append_properties(dict, TRUE, service);
2025 static void append_struct(gpointer value, gpointer user_data)
2027 struct connman_service *service = value;
2028 DBusMessageIter *iter = user_data;
2030 if (service->path == NULL)
2033 append_struct_service(iter, append_dict_properties, service);
2036 void __connman_service_list_struct(DBusMessageIter *iter)
2038 g_sequence_foreach(service_list, append_struct, iter);
2041 connman_bool_t __connman_service_is_hidden(struct connman_service *service)
2043 return service->hidden;
2046 int __connman_service_get_index(struct connman_service *service)
2048 if (service == NULL)
2051 if (service->network != NULL)
2052 return connman_network_get_index(service->network);
2053 else if (service->provider != NULL)
2054 return connman_provider_get_index(service->provider);
2059 void __connman_service_set_domainname(struct connman_service *service,
2060 const char *domainname)
2062 if (service == NULL || service->hidden == TRUE)
2065 g_free(service->domainname);
2066 service->domainname = g_strdup(domainname);
2068 domain_changed(service);
2071 const char *connman_service_get_domainname(struct connman_service *service)
2073 if (service == NULL)
2076 if (service->domains != NULL)
2077 return service->domains[0];
2079 return service->domainname;
2082 char **connman_service_get_nameservers(struct connman_service *service)
2084 if (service == NULL)
2087 if (service->nameservers_config != NULL)
2088 return g_strdupv(service->nameservers_config);
2089 else if (service->nameservers != NULL ||
2090 service->nameservers_auto != NULL) {
2091 int len = 0, len_auto = 0, i;
2094 if (service->nameservers != NULL)
2095 len = g_strv_length(service->nameservers);
2096 if (service->nameservers_auto != NULL)
2097 len_auto = g_strv_length(service->nameservers_auto);
2099 nameservers = g_try_new0(char *, len + len_auto + 1);
2100 if (nameservers == NULL)
2103 for (i = 0; i < len; i++)
2104 nameservers[i] = g_strdup(service->nameservers[i]);
2106 for (i = 0; i < len_auto; i++)
2107 nameservers[i + len] =
2108 g_strdup(service->nameservers_auto[i]);
2116 char **connman_service_get_timeservers(struct connman_service *service)
2118 if (service == NULL)
2121 if (service->timeservers != NULL)
2122 return service->timeservers;
2127 void connman_service_set_proxy_method(struct connman_service *service,
2128 enum connman_service_proxy_method method)
2130 if (service == NULL || service->hidden == TRUE)
2133 service->proxy = method;
2135 proxy_changed(service);
2137 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
2138 __connman_notifier_proxy_changed(service);
2141 enum connman_service_proxy_method connman_service_get_proxy_method(
2142 struct connman_service *service)
2144 if (service == NULL)
2145 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2147 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
2148 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
2149 service->pac == NULL)
2150 return service->proxy;
2152 return service->proxy_config;
2155 return service->proxy;
2158 char **connman_service_get_proxy_servers(struct connman_service *service)
2160 return g_strdupv(service->proxies);
2163 char **connman_service_get_proxy_excludes(struct connman_service *service)
2165 return g_strdupv(service->excludes);
2168 const char *connman_service_get_proxy_url(struct connman_service *service)
2170 if (service == NULL)
2173 return service->pac;
2176 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
2179 if (service == NULL || service->hidden == TRUE)
2182 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
2184 if (service->ipconfig_ipv4) {
2185 if (__connman_ipconfig_set_proxy_autoconfig(
2186 service->ipconfig_ipv4, url) < 0)
2188 } else if (service->ipconfig_ipv6) {
2189 if (__connman_ipconfig_set_proxy_autoconfig(
2190 service->ipconfig_ipv6, url) < 0)
2195 proxy_changed(service);
2197 __connman_notifier_proxy_changed(service);
2200 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
2202 if (service == NULL)
2205 if (service->ipconfig_ipv4)
2206 return __connman_ipconfig_get_proxy_autoconfig(
2207 service->ipconfig_ipv4);
2208 else if (service->ipconfig_ipv6)
2209 return __connman_ipconfig_get_proxy_autoconfig(
2210 service->ipconfig_ipv6);
2214 int __connman_service_timeserver_append(struct connman_service *service,
2215 const char *timeserver)
2219 DBG("service %p timeserver %s", service, timeserver);
2221 if (timeserver == NULL)
2224 if (service->timeservers != NULL) {
2227 for (i = 0; service->timeservers[i] != NULL; i++)
2228 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2231 len = g_strv_length(service->timeservers);
2232 service->timeservers = g_try_renew(char *, service->timeservers,
2236 service->timeservers = g_try_new0(char *, len + 2);
2239 if (service->timeservers == NULL)
2242 service->timeservers[len] = g_strdup(timeserver);
2243 service->timeservers[len + 1] = NULL;
2248 int __connman_service_timeserver_remove(struct connman_service *service,
2249 const char *timeserver)
2252 int len, i, j, found = 0;
2254 DBG("service %p timeserver %s", service, timeserver);
2256 if (timeserver == NULL)
2259 if (service->timeservers == NULL)
2262 for (i = 0; service->timeservers != NULL &&
2263 service->timeservers[i] != NULL; i++)
2264 if (g_strcmp0(service->timeservers[i], timeserver) == 0) {
2272 len = g_strv_length(service->timeservers);
2275 g_strfreev(service->timeservers);
2276 service->timeservers = NULL;
2281 servers = g_try_new0(char *, len);
2282 if (servers == NULL)
2285 for (i = 0, j = 0; i < len; i++) {
2286 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2287 servers[j] = g_strdup(service->timeservers[i]);
2288 if (servers[j] == NULL)
2293 servers[len - 1] = NULL;
2295 g_strfreev(service->timeservers);
2296 service->timeservers = servers;
2301 void __connman_service_set_pac(struct connman_service *service,
2304 if (service->hidden == TRUE)
2306 g_free(service->pac);
2307 service->pac = g_strdup(pac);
2309 proxy_changed(service);
2312 void __connman_service_set_identity(struct connman_service *service,
2313 const char *identity)
2315 if (service->immutable || service->hidden == TRUE)
2318 g_free(service->identity);
2319 service->identity = g_strdup(identity);
2321 if (service->network != NULL)
2322 connman_network_set_string(service->network,
2327 void __connman_service_set_agent_identity(struct connman_service *service,
2328 const char *agent_identity)
2330 if (service->hidden == TRUE)
2332 g_free(service->agent_identity);
2333 service->agent_identity = g_strdup(agent_identity);
2335 if (service->network != NULL)
2336 connman_network_set_string(service->network,
2337 "WiFi.AgentIdentity",
2338 service->agent_identity);
2341 void __connman_service_set_passphrase(struct connman_service *service,
2342 const char* passphrase)
2344 if (service->immutable == TRUE || service->hidden == TRUE)
2347 g_free(service->passphrase);
2348 service->passphrase = g_strdup(passphrase);
2350 if (service->network != NULL)
2351 connman_network_set_string(service->network,
2353 service->passphrase);
2355 service_save(service);
2358 void __connman_service_set_agent_passphrase(struct connman_service *service,
2359 const char *agent_passphrase)
2361 if (service->hidden == TRUE)
2363 g_free(service->agent_passphrase);
2364 service->agent_passphrase = g_strdup(agent_passphrase);
2366 if (service->network != NULL)
2367 connman_network_set_string(service->network,
2368 "WiFi.AgentPassphrase",
2369 service->agent_passphrase);
2372 static DBusMessage *get_properties(DBusConnection *conn,
2373 DBusMessage *msg, void *user_data)
2375 struct connman_service *service = user_data;
2377 DBusMessageIter array, dict;
2379 DBG("service %p", service);
2381 reply = dbus_message_new_method_return(msg);
2385 dbus_message_iter_init_append(reply, &array);
2387 connman_dbus_dict_open(&array, &dict);
2388 append_properties(&dict, FALSE, service);
2389 connman_dbus_dict_close(&array, &dict);
2394 static int update_proxy_configuration(struct connman_service *service,
2395 DBusMessageIter *array)
2397 DBusMessageIter dict;
2398 enum connman_service_proxy_method method;
2399 GString *servers_str = NULL;
2400 GString *excludes_str = NULL;
2401 const char *url = NULL;
2403 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2405 dbus_message_iter_recurse(array, &dict);
2407 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2408 DBusMessageIter entry, variant;
2412 dbus_message_iter_recurse(&dict, &entry);
2414 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2417 dbus_message_iter_get_basic(&entry, &key);
2418 dbus_message_iter_next(&entry);
2420 if (dbus_message_iter_get_arg_type(&entry) !=
2424 dbus_message_iter_recurse(&entry, &variant);
2425 type = dbus_message_iter_get_arg_type(&variant);
2427 if (g_str_equal(key, "Method") == TRUE) {
2430 if (type != DBUS_TYPE_STRING)
2433 dbus_message_iter_get_basic(&variant, &val);
2434 method = string2proxymethod(val);
2435 } else if (g_str_equal(key, "URL") == TRUE) {
2436 if (type != DBUS_TYPE_STRING)
2439 dbus_message_iter_get_basic(&variant, &url);
2440 } else if (g_str_equal(key, "Servers") == TRUE) {
2441 DBusMessageIter str_array;
2443 if (type != DBUS_TYPE_ARRAY)
2446 servers_str = g_string_new(NULL);
2447 if (servers_str == NULL)
2450 dbus_message_iter_recurse(&variant, &str_array);
2452 while (dbus_message_iter_get_arg_type(&str_array) ==
2456 dbus_message_iter_get_basic(&str_array, &val);
2458 if (servers_str->len > 0)
2459 g_string_append_printf(servers_str,
2462 g_string_append(servers_str, val);
2464 dbus_message_iter_next(&str_array);
2466 } else if (g_str_equal(key, "Excludes") == TRUE) {
2467 DBusMessageIter str_array;
2469 if (type != DBUS_TYPE_ARRAY)
2472 excludes_str = g_string_new(NULL);
2473 if (excludes_str == NULL)
2476 dbus_message_iter_recurse(&variant, &str_array);
2478 while (dbus_message_iter_get_arg_type(&str_array) ==
2482 dbus_message_iter_get_basic(&str_array, &val);
2484 if (excludes_str->len > 0)
2485 g_string_append_printf(excludes_str,
2488 g_string_append(excludes_str, val);
2490 dbus_message_iter_next(&str_array);
2494 dbus_message_iter_next(&dict);
2498 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2500 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2501 if (servers_str == NULL && service->proxies == NULL)
2504 if (servers_str != NULL) {
2505 g_strfreev(service->proxies);
2507 if (servers_str->len > 0)
2508 service->proxies = g_strsplit_set(
2509 servers_str->str, " ", 0);
2511 service->proxies = NULL;
2514 if (excludes_str != NULL) {
2515 g_strfreev(service->excludes);
2517 if (excludes_str->len > 0)
2518 service->excludes = g_strsplit_set(
2519 excludes_str->str, " ", 0);
2521 service->excludes = NULL;
2524 if (service->proxies == NULL)
2525 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2528 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2529 g_free(service->pac);
2531 if (url != NULL && strlen(url) > 0)
2532 service->pac = g_strdup(url);
2534 service->pac = NULL;
2536 /* if we are connected:
2537 - if service->pac == NULL
2538 - if __connman_ipconfig_get_proxy_autoconfig(
2539 service->ipconfig) == NULL
2540 --> We should start WPAD */
2543 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2547 if (servers_str != NULL)
2548 g_string_free(servers_str, TRUE);
2550 if (excludes_str != NULL)
2551 g_string_free(excludes_str, TRUE);
2553 service->proxy_config = method;
2558 if (servers_str != NULL)
2559 g_string_free(servers_str, TRUE);
2561 if (excludes_str != NULL)
2562 g_string_free(excludes_str, TRUE);
2567 static int set_ipconfig(struct connman_service *service,
2568 struct connman_ipconfig *ipconfig,
2569 DBusMessageIter *array,
2570 enum connman_service_state state,
2571 enum connman_service_state *new_state)
2573 enum connman_ipconfig_method old_method;
2574 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2575 enum connman_ipconfig_type type;
2578 if (ipconfig == NULL)
2581 old_method = __connman_ipconfig_get_method(ipconfig);
2583 if (is_connecting_state(service, state) ||
2584 is_connected_state(service, state))
2585 __connman_network_clear_ipconfig(service->network, ipconfig);
2587 err = __connman_ipconfig_set_config(ipconfig, array);
2588 method = __connman_ipconfig_get_method(ipconfig);
2589 type = __connman_ipconfig_get_config_type(ipconfig);
2591 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2592 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2593 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2594 *new_state = service->state_ipv4 =
2595 CONNMAN_SERVICE_STATE_CONFIGURATION;
2596 __connman_ipconfig_enable(ipconfig);
2599 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2600 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2601 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2602 *new_state = service->state_ipv6;
2603 __connman_ipconfig_enable(ipconfig);
2607 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2608 type, method, state2string(*new_state));
2613 static DBusMessage *set_property(DBusConnection *conn,
2614 DBusMessage *msg, void *user_data)
2616 struct connman_service *service = user_data;
2617 DBusMessageIter iter, value;
2621 DBG("service %p", service);
2623 if (dbus_message_iter_init(msg, &iter) == FALSE)
2624 return __connman_error_invalid_arguments(msg);
2626 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
2627 return __connman_error_invalid_arguments(msg);
2629 dbus_message_iter_get_basic(&iter, &name);
2630 dbus_message_iter_next(&iter);
2632 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
2633 return __connman_error_invalid_arguments(msg);
2635 dbus_message_iter_recurse(&iter, &value);
2637 type = dbus_message_iter_get_arg_type(&value);
2639 if (g_str_equal(name, "AutoConnect") == TRUE) {
2640 connman_bool_t autoconnect;
2642 if (type != DBUS_TYPE_BOOLEAN)
2643 return __connman_error_invalid_arguments(msg);
2645 if (service->favorite == FALSE)
2646 return __connman_error_invalid_service(msg);
2648 dbus_message_iter_get_basic(&value, &autoconnect);
2650 if (service->autoconnect == autoconnect)
2651 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2653 service->autoconnect = autoconnect;
2655 autoconnect_changed(service);
2657 service_save(service);
2658 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2659 DBusMessageIter entry;
2664 if (type != DBUS_TYPE_ARRAY)
2665 return __connman_error_invalid_arguments(msg);
2667 str = g_string_new(NULL);
2669 return __connman_error_invalid_arguments(msg);
2671 index = connman_network_get_index(service->network);
2672 gw = __connman_ipconfig_get_gateway_from_index(index);
2674 if (gw && strlen(gw))
2675 __connman_service_nameserver_del_routes(service);
2677 dbus_message_iter_recurse(&value, &entry);
2679 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2681 dbus_message_iter_get_basic(&entry, &val);
2682 dbus_message_iter_next(&entry);
2684 g_string_append_printf(str, " %s", val);
2686 g_string_append(str, val);
2689 g_strfreev(service->nameservers_config);
2692 service->nameservers_config =
2693 g_strsplit_set(str->str, " ", 0);
2695 service->nameservers_config = NULL;
2698 g_string_free(str, TRUE);
2700 if (gw && strlen(gw))
2701 __connman_service_nameserver_add_routes(service, gw);
2703 update_nameservers(service);
2704 dns_configuration_changed(service);
2706 service_save(service);
2707 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2708 DBusMessageIter entry;
2711 if (type != DBUS_TYPE_ARRAY)
2712 return __connman_error_invalid_arguments(msg);
2714 str = g_string_new(NULL);
2716 return __connman_error_invalid_arguments(msg);
2718 dbus_message_iter_recurse(&value, &entry);
2720 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2722 dbus_message_iter_get_basic(&entry, &val);
2723 dbus_message_iter_next(&entry);
2725 g_string_append_printf(str, " %s", val);
2727 g_string_append(str, val);
2730 g_strfreev(service->domains);
2733 service->domains = g_strsplit_set(str->str, " ", 0);
2735 service->domains = NULL;
2737 g_string_free(str, TRUE);
2739 update_nameservers(service);
2740 domain_configuration_changed(service);
2742 service_save(service);
2743 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2746 if (type != DBUS_TYPE_ARRAY)
2747 return __connman_error_invalid_arguments(msg);
2749 err = update_proxy_configuration(service, &value);
2752 return __connman_error_failed(msg, -err);
2754 proxy_configuration_changed(service);
2756 __connman_notifier_proxy_changed(service);
2758 service_save(service);
2759 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2760 g_str_equal(name, "IPv6.Configuration")) {
2762 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2763 enum connman_service_state state =
2764 CONNMAN_SERVICE_STATE_UNKNOWN;
2769 if (service->ipconfig_ipv4 == NULL &&
2770 service->ipconfig_ipv6 == NULL)
2771 return __connman_error_invalid_property(msg);
2773 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2774 ipv4 = service->ipconfig_ipv4;
2775 err = set_ipconfig(service, ipv4, &value,
2776 service->state_ipv4, &state);
2778 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2779 ipv6 = service->ipconfig_ipv6;
2780 err = set_ipconfig(service, ipv6, &value,
2781 service->state_ipv6, &state);
2785 if (is_connected_state(service, state) ||
2786 is_connecting_state(service, state))
2787 __connman_network_set_ipconfig(service->network,
2789 return __connman_error_failed(msg, -err);
2793 ipv4_configuration_changed(service);
2795 ipv6_configuration_changed(service);
2797 if (is_connecting(service) || is_connected(service))
2798 __connman_network_set_ipconfig(service->network,
2801 service_save(service);
2803 return __connman_error_invalid_property(msg);
2805 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2808 static void set_idle(struct connman_service *service)
2810 service->state = service->state_ipv4 = service->state_ipv6 =
2811 CONNMAN_SERVICE_STATE_IDLE;
2812 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2813 state_changed(service);
2816 static DBusMessage *clear_property(DBusConnection *conn,
2817 DBusMessage *msg, void *user_data)
2819 struct connman_service *service = user_data;
2822 DBG("service %p", service);
2824 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2827 if (g_str_equal(name, "Error") == TRUE) {
2830 g_get_current_time(&service->modified);
2831 service_save(service);
2833 return __connman_error_invalid_property(msg);
2835 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2838 static connman_bool_t is_ignore(struct connman_service *service)
2840 if (service->autoconnect == FALSE)
2843 if (service->roaming == TRUE)
2846 if (service->ignore == TRUE)
2849 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
2855 void __connman_service_auto_connect(void)
2857 struct connman_service *service = NULL;
2858 GSequenceIter *iter;
2862 if (__connman_session_mode() == TRUE) {
2863 DBG("Session mode enabled: auto connect disabled");
2867 iter = g_sequence_get_begin_iter(service_list);
2869 while (g_sequence_iter_is_end(iter) == FALSE) {
2870 service = g_sequence_get(iter);
2872 if (service->pending != NULL)
2875 if (is_connecting(service) == TRUE)
2878 if (service->favorite == FALSE)
2881 if (is_connected(service) == TRUE)
2884 if (is_ignore(service) == FALSE && service->state ==
2885 CONNMAN_SERVICE_STATE_IDLE)
2890 iter = g_sequence_iter_next(iter);
2893 if (service != NULL) {
2894 service->userconnect = FALSE;
2895 __connman_service_connect(service);
2899 static void remove_timeout(struct connman_service *service)
2901 if (service->timeout > 0) {
2902 g_source_remove(service->timeout);
2903 service->timeout = 0;
2907 static void reply_pending(struct connman_service *service, int error)
2909 remove_timeout(service);
2911 if (service->pending != NULL) {
2915 reply = __connman_error_failed(service->pending,
2918 g_dbus_send_message(connection, reply);
2922 sender = dbus_message_get_interface(service->pending);
2924 DBG("sender %s", sender);
2926 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
2927 g_dbus_send_reply(connection, service->pending,
2928 DBUS_TYPE_OBJECT_PATH, &service->path,
2931 g_dbus_send_reply(connection, service->pending,
2935 dbus_message_unref(service->pending);
2936 service->pending = NULL;
2940 static gboolean connect_timeout(gpointer user_data)
2942 struct connman_service *service = user_data;
2943 connman_bool_t autoconnect = FALSE;
2945 DBG("service %p", service);
2947 service->timeout = 0;
2949 if (service->network != NULL)
2950 __connman_network_disconnect(service->network);
2952 __connman_ipconfig_disable(service->ipconfig_ipv4);
2953 __connman_ipconfig_disable(service->ipconfig_ipv6);
2955 __connman_stats_service_unregister(service);
2957 if (service->pending != NULL) {
2960 reply = __connman_error_operation_timeout(service->pending);
2962 g_dbus_send_message(connection, reply);
2964 dbus_message_unref(service->pending);
2965 service->pending = NULL;
2969 __connman_service_ipconfig_indicate_state(service,
2970 CONNMAN_SERVICE_STATE_FAILURE,
2971 CONNMAN_IPCONFIG_TYPE_IPV4);
2972 __connman_service_ipconfig_indicate_state(service,
2973 CONNMAN_SERVICE_STATE_FAILURE,
2974 CONNMAN_IPCONFIG_TYPE_IPV6);
2976 if (autoconnect == TRUE && service->userconnect == FALSE)
2977 __connman_service_auto_connect();
2982 static void set_reconnect_state(struct connman_service *service,
2983 connman_bool_t reconnect)
2985 struct connman_device *device;
2987 if (service->network == NULL)
2990 device = connman_network_get_device(service->network);
2994 __connman_device_set_reconnect(device, reconnect);
2997 static connman_bool_t get_reconnect_state(struct connman_service *service)
2999 struct connman_device *device;
3001 if (service->network == NULL)
3004 device = connman_network_get_device(service->network);
3008 return __connman_device_get_reconnect(device);
3011 static DBusMessage *connect_service(DBusConnection *conn,
3012 DBusMessage *msg, void *user_data)
3014 struct connman_service *service = user_data;
3015 GSequenceIter *iter;
3018 DBG("service %p", service);
3020 if (service->pending != NULL)
3021 return __connman_error_in_progress(msg);
3023 iter = g_sequence_get_begin_iter(service_list);
3025 while (g_sequence_iter_is_end(iter) == FALSE) {
3026 struct connman_service *temp = g_sequence_get(iter);
3028 if (service->type == temp->type && is_connecting(temp) == TRUE)
3029 return __connman_error_in_progress(msg);
3031 iter = g_sequence_iter_next(iter);
3034 service->ignore = FALSE;
3036 service->userconnect = TRUE;
3038 service->pending = dbus_message_ref(msg);
3040 set_reconnect_state(service, FALSE);
3042 err = __connman_service_connect(service);
3044 if (service->pending == NULL)
3047 if (err != -EINPROGRESS) {
3048 dbus_message_unref(service->pending);
3049 service->pending = NULL;
3051 return __connman_error_failed(msg, -err);
3057 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3060 static DBusMessage *disconnect_service(DBusConnection *conn,
3061 DBusMessage *msg, void *user_data)
3063 struct connman_service *service = user_data;
3066 DBG("service %p", service);
3068 reply_pending(service, ECONNABORTED);
3070 service->ignore = TRUE;
3072 set_reconnect_state(service, FALSE);
3074 err = __connman_service_disconnect(service);
3076 if (err != -EINPROGRESS)
3077 return __connman_error_failed(msg, -err);
3082 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3085 static DBusMessage *remove_service(DBusConnection *conn,
3086 DBusMessage *msg, void *user_data)
3088 struct connman_service *service = user_data;
3090 DBG("service %p", service);
3092 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
3093 return __connman_error_not_supported(msg);
3095 if (service->immutable == TRUE || service->hidden == TRUE)
3096 return __connman_error_not_supported(msg);
3098 if (service->favorite == FALSE && service->state !=
3099 CONNMAN_SERVICE_STATE_FAILURE)
3100 return __connman_error_not_supported(msg);
3102 set_reconnect_state(service, FALSE);
3104 __connman_service_disconnect(service);
3106 g_free(service->passphrase);
3107 service->passphrase = NULL;
3111 __connman_service_set_favorite(service, FALSE);
3112 service_save(service);
3114 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3117 static gboolean check_suitable_state(enum connman_service_state a,
3118 enum connman_service_state b)
3121 * Special check so that "ready" service can be moved before
3124 if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
3125 b == CONNMAN_SERVICE_STATE_READY) ||
3126 (b == CONNMAN_SERVICE_STATE_ONLINE &&
3127 a == CONNMAN_SERVICE_STATE_READY))
3133 static void downgrade_state(struct connman_service *service)
3135 if (service == NULL)
3138 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
3139 service->state_ipv6);
3141 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
3142 __connman_service_ipconfig_indicate_state(service,
3143 CONNMAN_SERVICE_STATE_READY,
3144 CONNMAN_IPCONFIG_TYPE_IPV4);
3146 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
3147 __connman_service_ipconfig_indicate_state(service,
3148 CONNMAN_SERVICE_STATE_READY,
3149 CONNMAN_IPCONFIG_TYPE_IPV6);
3152 static void apply_relevant_default_downgrade(struct connman_service *service)
3154 struct connman_service *def_service;
3156 def_service = get_default();
3157 if (def_service == NULL)
3160 if (def_service == service &&
3161 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3162 def_service->state = CONNMAN_SERVICE_STATE_READY;
3165 static DBusMessage *move_service(DBusConnection *conn,
3166 DBusMessage *msg, void *user_data,
3169 struct connman_service *service = user_data;
3170 struct connman_service *target;
3172 GSequenceIter *src, *dst;
3173 enum connman_ipconfig_method target4, target6;
3174 enum connman_ipconfig_method service4, service6;
3176 DBG("service %p", service);
3178 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3181 if (service->favorite == FALSE)
3182 return __connman_error_not_supported(msg);
3184 target = find_service(path);
3185 if (target == NULL || target->favorite == FALSE || target == service ||
3186 target->type == CONNMAN_SERVICE_TYPE_VPN)
3187 return __connman_error_invalid_service(msg);
3189 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3190 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3191 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3192 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3194 DBG("target %s method %d/%d state %d/%d", target->identifier,
3196 target->state_ipv4, target->state_ipv6);
3198 DBG("service %s method %d/%d state %d/%d", service->identifier,
3200 service->state_ipv4, service->state_ipv6);
3203 * If method is OFF, then we do not need to check the corresponding
3206 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3207 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3208 if (check_suitable_state(target->state_ipv6,
3209 service->state_ipv6) == FALSE)
3210 return __connman_error_invalid_service(msg);
3214 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3215 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3216 if (check_suitable_state(target->state_ipv4,
3217 service->state_ipv4) == FALSE)
3218 return __connman_error_invalid_service(msg);
3222 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3223 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3224 if (check_suitable_state(target->state_ipv6,
3225 service->state_ipv6) == FALSE)
3226 return __connman_error_invalid_service(msg);
3230 if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3231 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3232 if (check_suitable_state(target->state_ipv4,
3233 service->state_ipv4) == FALSE)
3234 return __connman_error_invalid_service(msg);
3238 g_get_current_time(&service->modified);
3239 service_save(service);
3241 src = g_hash_table_lookup(service_hash, service->identifier);
3242 dst = g_hash_table_lookup(service_hash, target->identifier);
3245 * If the service which goes down is the default service and is
3246 * online, we downgrade directly its state to ready so:
3247 * the service which goes up, needs to recompute its state which
3248 * is triggered via downgrading it - if relevant - to state ready.
3250 if (before == TRUE) {
3251 apply_relevant_default_downgrade(target);
3252 g_sequence_move(src, dst);
3253 downgrade_state(service);
3255 apply_relevant_default_downgrade(service);
3256 g_sequence_move(dst, src);
3257 downgrade_state(target);
3260 __connman_connection_update_gateway();
3262 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3265 static DBusMessage *move_before(DBusConnection *conn,
3266 DBusMessage *msg, void *user_data)
3268 return move_service(conn, msg, user_data, TRUE);
3271 static DBusMessage *move_after(DBusConnection *conn,
3272 DBusMessage *msg, void *user_data)
3274 return move_service(conn, msg, user_data, FALSE);
3277 static DBusMessage *reset_counters(DBusConnection *conn,
3278 DBusMessage *msg, void *user_data)
3280 struct connman_service *service = user_data;
3282 reset_stats(service);
3284 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3287 static struct _services_notify {
3293 static void append_removed(gpointer key, gpointer value, gpointer user_data)
3295 char *objpath = key;
3296 DBusMessageIter *iter = user_data;
3298 DBG("removed %s", objpath);
3299 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &objpath);
3302 static void service_send_removed(void)
3304 DBusMessage *signal;
3305 DBusMessageIter iter, array;
3307 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
3308 CONNMAN_MANAGER_INTERFACE, "ServicesRemoved");
3312 dbus_message_iter_init_append(signal, &iter);
3313 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
3314 DBUS_TYPE_OBJECT_PATH_AS_STRING, &array);
3316 g_hash_table_foreach(services_notify->remove, append_removed, &array);
3318 dbus_message_iter_close_container(&iter, &array);
3320 dbus_connection_send(connection, signal, NULL);
3321 dbus_message_unref(signal);
3323 g_hash_table_remove_all(services_notify->remove);
3326 static void service_send_added_foreach(gpointer data, gpointer user_data)
3328 struct connman_service *service = data;
3329 DBusMessageIter *iter = user_data;
3331 if (service == NULL || service->path == NULL) {
3332 DBG("service %p or path is NULL", service);
3336 DBG("added %s", service->path);
3338 if (g_hash_table_lookup(services_notify->add, service->path) != NULL) {
3339 append_struct(service, iter);
3340 g_hash_table_remove(services_notify->add, service->path);
3342 append_struct_service(iter, NULL, service);
3346 static void service_send_added_ordered(DBusMessageIter *iter, void *user_data)
3348 g_sequence_foreach(service_list, service_send_added_foreach, iter);
3351 static void service_send_added(void)
3353 DBusMessage *signal;
3355 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
3356 CONNMAN_MANAGER_INTERFACE, "ServicesAdded");
3360 __connman_dbus_append_objpath_dict_array(signal,
3361 service_send_added_ordered, NULL);
3363 dbus_connection_send(connection, signal, NULL);
3364 dbus_message_unref(signal);
3366 g_hash_table_remove_all(services_notify->add);
3369 static gboolean service_send_signals(gpointer data)
3371 if (g_hash_table_size(services_notify->remove) > 0)
3372 service_send_removed();
3374 if (g_hash_table_size(services_notify->add) > 0)
3375 service_send_added();
3377 services_notify->id = 0;
3381 static void service_schedule_signals(void)
3383 if (services_notify->id != 0)
3384 g_source_remove(services_notify->id);
3386 services_notify->id = g_timeout_add(100, service_send_signals, NULL);
3389 static void service_schedule_added(struct connman_service *service)
3391 DBG("service %p", service);
3393 g_hash_table_remove(services_notify->remove, service->path);
3394 g_hash_table_insert(services_notify->add, service->path, service);
3396 service_schedule_signals();
3399 static void service_schedule_removed(struct connman_service *service)
3401 DBG("service %p %s", service, service->path);
3403 if (service == NULL || service->path == NULL) {
3404 DBG("service %p or path is NULL", service);
3408 g_hash_table_remove(services_notify->add, service->path);
3409 g_hash_table_insert(services_notify->remove, g_strdup(service->path),
3412 service_schedule_signals();
3415 static GDBusMethodTable service_methods[] = {
3416 { "GetProperties", "", "a{sv}", get_properties },
3417 { "SetProperty", "sv", "", set_property },
3418 { "ClearProperty", "s", "", clear_property },
3419 { "Connect", "", "", connect_service,
3420 G_DBUS_METHOD_FLAG_ASYNC },
3421 { "Disconnect", "", "", disconnect_service },
3422 { "Remove", "", "", remove_service },
3423 { "MoveBefore", "o", "", move_before },
3424 { "MoveAfter", "o", "", move_after },
3425 { "ResetCounters", "", "", reset_counters },
3429 static GDBusSignalTable service_signals[] = {
3430 { "PropertyChanged", "sv" },
3434 static void service_free(gpointer user_data)
3436 struct connman_service *service = user_data;
3437 char *path = service->path;
3439 DBG("service %p", service);
3441 reply_pending(service, ENOENT);
3443 g_hash_table_remove(service_hash, service->identifier);
3445 __connman_notifier_service_remove(service);
3446 service_schedule_removed(service);
3448 stats_stop(service);
3450 service->path = NULL;
3453 __connman_connection_update_gateway();
3455 g_dbus_unregister_interface(connection, path,
3456 CONNMAN_SERVICE_INTERFACE);
3460 g_hash_table_destroy(service->counter_table);
3462 if (service->network != NULL) {
3463 __connman_network_disconnect(service->network);
3464 connman_network_unref(service->network);
3465 service->network = NULL;
3468 if (service->provider != NULL)
3469 connman_provider_unref(service->provider);
3471 if (service->ipconfig_ipv4 != NULL) {
3472 __connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3473 __connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3474 __connman_ipconfig_unref(service->ipconfig_ipv4);
3475 service->ipconfig_ipv4 = NULL;
3478 if (service->ipconfig_ipv6 != NULL) {
3479 __connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3480 __connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3481 __connman_ipconfig_unref(service->ipconfig_ipv6);
3482 service->ipconfig_ipv6 = NULL;
3485 g_strfreev(service->timeservers);
3486 g_strfreev(service->nameservers);
3487 g_strfreev(service->nameservers_config);
3488 g_strfreev(service->nameservers_auto);
3489 g_strfreev(service->domains);
3490 g_strfreev(service->proxies);
3491 g_strfreev(service->excludes);
3493 g_free(service->domainname);
3494 g_free(service->pac);
3495 g_free(service->name);
3496 g_free(service->passphrase);
3497 g_free(service->agent_passphrase);
3498 g_free(service->identifier);
3499 g_free(service->eap);
3500 g_free(service->identity);
3501 g_free(service->agent_identity);
3502 g_free(service->ca_cert_file);
3503 g_free(service->client_cert_file);
3504 g_free(service->private_key_file);
3505 g_free(service->private_key_passphrase);
3506 g_free(service->phase2);
3508 if (service->stats.timer != NULL)
3509 g_timer_destroy(service->stats.timer);
3510 if (service->stats_roaming.timer != NULL)
3511 g_timer_destroy(service->stats_roaming.timer);
3516 static void stats_init(struct connman_service *service)
3519 service->stats.valid = FALSE;
3520 service->stats.enabled = FALSE;
3521 service->stats.timer = g_timer_new();
3524 service->stats_roaming.valid = FALSE;
3525 service->stats_roaming.enabled = FALSE;
3526 service->stats_roaming.timer = g_timer_new();
3529 static void service_initialize(struct connman_service *service)
3531 DBG("service %p", service);
3533 service->refcount = 1;
3534 service->session_usage_count = 0;
3536 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3537 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3539 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3540 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3541 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3543 service->favorite = FALSE;
3544 service->immutable = FALSE;
3545 service->hidden = FALSE;
3547 service->ignore = FALSE;
3549 service->userconnect = FALSE;
3553 stats_init(service);
3555 service->provider = NULL;
3557 service->wps = FALSE;
3561 * connman_service_create:
3563 * Allocate a new service.
3565 * Returns: a newly-allocated #connman_service structure
3567 struct connman_service *connman_service_create(void)
3570 struct connman_stats_counter *counters;
3571 const char *counter;
3573 struct connman_service *service;
3575 service = g_try_new0(struct connman_service, 1);
3576 if (service == NULL)
3579 DBG("service %p", service);
3581 service->counter_table = g_hash_table_new_full(g_str_hash,
3582 g_str_equal, NULL, g_free);
3584 for (list = counter_list; list; list = list->next) {
3585 counter = list->data;
3587 counters = g_try_new0(struct connman_stats_counter, 1);
3588 if (counters == NULL) {
3589 g_hash_table_destroy(service->counter_table);
3594 counters->append_all = TRUE;
3596 g_hash_table_replace(service->counter_table, (gpointer)counter,
3600 service_initialize(service);
3606 * connman_service_ref:
3607 * @service: service structure
3609 * Increase reference counter of service
3611 struct connman_service *
3612 connman_service_ref_debug(struct connman_service *service,
3613 const char *file, int line, const char *caller)
3615 DBG("%p ref %d by %s:%d:%s()", service, service->refcount + 1,
3616 file, line, caller);
3618 __sync_fetch_and_add(&service->refcount, 1);
3624 * connman_service_unref:
3625 * @service: service structure
3627 * Decrease reference counter of service and release service if no
3630 void connman_service_unref_debug(struct connman_service *service,
3631 const char *file, int line, const char *caller)
3633 GSequenceIter *iter;
3635 DBG("%p ref %d by %s:%d:%s()", service, service->refcount - 1,
3636 file, line, caller);
3638 if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
3641 iter = g_hash_table_lookup(service_hash, service->identifier);
3643 reply_pending(service, ECONNABORTED);
3645 __connman_service_disconnect(service);
3647 g_sequence_remove(iter);
3649 service_free(service);
3653 static gint service_compare(gconstpointer a, gconstpointer b,
3656 struct connman_service *service_a = (void *) a;
3657 struct connman_service *service_b = (void *) b;
3658 enum connman_service_state state_a, state_b;
3660 state_a = service_a->state;
3661 state_b = service_b->state;
3663 if (state_a != state_b) {
3664 gboolean a_connected = is_connected(service_a);
3665 gboolean b_connected = is_connected(service_b);
3667 if (a_connected == TRUE && b_connected == TRUE) {
3668 /* We prefer online over ready state */
3669 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
3672 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
3676 if (a_connected == TRUE)
3678 if (b_connected == TRUE)
3681 if (is_connecting(service_a) == TRUE)
3683 if (is_connecting(service_b) == TRUE)
3687 if (service_a->order > service_b->order)
3690 if (service_a->order < service_b->order)
3693 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3696 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3699 if (service_a->type != service_b->type) {
3700 switch (service_a->type) {
3701 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3702 case CONNMAN_SERVICE_TYPE_SYSTEM:
3703 case CONNMAN_SERVICE_TYPE_ETHERNET:
3704 case CONNMAN_SERVICE_TYPE_GPS:
3705 case CONNMAN_SERVICE_TYPE_VPN:
3706 case CONNMAN_SERVICE_TYPE_GADGET:
3708 case CONNMAN_SERVICE_TYPE_WIFI:
3710 case CONNMAN_SERVICE_TYPE_WIMAX:
3711 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3712 case CONNMAN_SERVICE_TYPE_CELLULAR:
3717 return (gint) service_b->strength - (gint) service_a->strength;
3721 * connman_service_get_type:
3722 * @service: service structure
3724 * Get the type of service
3726 enum connman_service_type connman_service_get_type(struct connman_service *service)
3728 if (service == NULL)
3729 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3731 return service->type;
3735 * connman_service_get_interface:
3736 * @service: service structure
3738 * Get network interface of service
3740 char *connman_service_get_interface(struct connman_service *service)
3744 if (service == NULL)
3747 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3748 if (service->ipconfig_ipv4)
3749 index = __connman_ipconfig_get_index(
3750 service->ipconfig_ipv4);
3751 else if (service->ipconfig_ipv6)
3752 index = __connman_ipconfig_get_index(
3753 service->ipconfig_ipv6);
3757 return connman_inet_ifname(index);
3760 if (service->network == NULL)
3763 index = connman_network_get_index(service->network);
3765 return connman_inet_ifname(index);
3769 * connman_service_get_network:
3770 * @service: service structure
3772 * Get the service network
3774 struct connman_network *
3775 __connman_service_get_network(struct connman_service *service)
3777 if (service == NULL)
3780 return service->network;
3783 struct connman_ipconfig *
3784 __connman_service_get_ip4config(struct connman_service *service)
3786 if (service == NULL)
3789 return service->ipconfig_ipv4;
3792 struct connman_ipconfig *
3793 __connman_service_get_ip6config(struct connman_service *service)
3795 if (service == NULL)
3798 return service->ipconfig_ipv6;
3801 struct connman_ipconfig *
3802 __connman_service_get_ipconfig(struct connman_service *service, int family)
3804 if (family == AF_INET)
3805 return __connman_service_get_ip4config(service);
3806 else if (family == AF_INET6)
3807 return __connman_service_get_ip6config(service);
3813 connman_bool_t __connman_service_is_connected_state(struct connman_service *service,
3814 enum connman_ipconfig_type type)
3816 if (service == NULL)
3820 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
3822 case CONNMAN_IPCONFIG_TYPE_IPV4:
3823 return is_connected_state(service, service->state_ipv4);
3824 case CONNMAN_IPCONFIG_TYPE_IPV6:
3825 return is_connected_state(service, service->state_ipv6);
3830 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3832 if (service == NULL)
3833 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3835 return service->security;
3838 const char *__connman_service_get_phase2(struct connman_service *service)
3840 if (service == NULL)
3843 return service->phase2;
3846 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3848 if (service == NULL)
3851 return service->wps;
3855 * __connman_service_set_favorite:
3856 * @service: service structure
3857 * @favorite: favorite value
3859 * Change the favorite setting of service
3861 int __connman_service_set_favorite(struct connman_service *service,
3862 connman_bool_t favorite)
3864 GSequenceIter *iter;
3866 if (service->hidden == TRUE)
3868 iter = g_hash_table_lookup(service_hash, service->identifier);
3872 if (service->favorite == favorite)
3875 service->favorite = favorite;
3876 service->order = __connman_service_get_order(service);
3878 favorite_changed(service);
3880 g_sequence_sort_changed(iter, service_compare, NULL);
3882 __connman_connection_update_gateway();
3887 int __connman_service_set_immutable(struct connman_service *service,
3888 connman_bool_t immutable)
3890 if (service->hidden == TRUE)
3892 service->immutable = immutable;
3894 immutable_changed(service);
3899 void __connman_service_set_string(struct connman_service *service,
3900 const char *key, const char *value)
3902 if (service->hidden == TRUE)
3904 if (g_str_equal(key, "EAP") == TRUE) {
3905 g_free(service->eap);
3906 service->eap = g_strdup(value);
3907 } else if (g_str_equal(key, "Identity") == TRUE) {
3908 g_free(service->identity);
3909 service->identity = g_strdup(value);
3910 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3911 g_free(service->ca_cert_file);
3912 service->ca_cert_file = g_strdup(value);
3913 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3914 g_free(service->client_cert_file);
3915 service->client_cert_file = g_strdup(value);
3916 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3917 g_free(service->private_key_file);
3918 service->private_key_file = g_strdup(value);
3919 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3920 g_free(service->private_key_passphrase);
3921 service->private_key_passphrase = g_strdup(value);
3922 } else if (g_str_equal(key, "Phase2") == TRUE) {
3923 g_free(service->phase2);
3924 service->phase2 = g_strdup(value);
3925 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3926 g_free(service->passphrase);
3927 service->passphrase = g_strdup(value);
3931 static void service_complete(struct connman_service *service)
3933 reply_pending(service, EIO);
3935 if (service->userconnect == FALSE)
3936 __connman_service_auto_connect();
3938 g_get_current_time(&service->modified);
3939 service_save(service);
3942 static void report_error_cb(struct connman_service *service,
3943 gboolean retry, void *user_data)
3946 __connman_service_connect(service);
3948 service_complete(service);
3949 __connman_connection_update_gateway();
3950 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3954 void __connman_service_add_passphrase(struct connman_service *service,
3955 const gchar *passphrase)
3957 switch (service->security) {
3958 case CONNMAN_SERVICE_SECURITY_WEP:
3959 case CONNMAN_SERVICE_SECURITY_PSK:
3960 __connman_service_set_passphrase(service, passphrase);
3962 case CONNMAN_SERVICE_SECURITY_8021X:
3963 __connman_service_set_agent_passphrase(service,
3966 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3967 case CONNMAN_SERVICE_SECURITY_NONE:
3968 case CONNMAN_SERVICE_SECURITY_WPA:
3969 case CONNMAN_SERVICE_SECURITY_RSN:
3970 DBG("service security '%s' (%d) not handled",
3971 security2string(service->security),
3978 static void request_input_cb (struct connman_service *service,
3979 connman_bool_t values_received,
3980 const char *name, int name_len,
3981 const char *identity, const char *passphrase,
3984 struct connman_device *device;
3986 DBG ("RequestInput return, %p", service);
3988 if (service->hidden == TRUE && name_len > 0 && name_len <= 32) {
3989 device = connman_network_get_device(service->network);
3990 __connman_device_request_hidden_scan(device,
3992 identity, passphrase);
3995 if (values_received == FALSE || service->hidden == TRUE) {
3996 service_complete(service);
3997 __connman_connection_update_gateway();
3998 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4002 if (identity != NULL)
4003 __connman_service_set_agent_identity(service, identity);
4005 if (passphrase != NULL)
4006 __connman_service_add_passphrase(service, passphrase);
4008 __connman_service_connect(service);
4010 /* Never cache agent provided credentials */
4011 __connman_service_set_agent_identity(service, NULL);
4012 __connman_service_set_agent_passphrase(service, NULL);
4015 static void downgrade_connected_services(void)
4017 struct connman_service *up_service;
4018 GSequenceIter *iter;
4020 iter = g_sequence_get_begin_iter(service_list);
4021 while (g_sequence_iter_is_end(iter) == FALSE) {
4022 up_service = g_sequence_get(iter);
4024 if (is_connected(up_service) == FALSE) {
4025 iter = g_sequence_iter_next(iter);
4029 if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE)
4032 downgrade_state(up_service);
4034 iter = g_sequence_iter_next(iter);
4038 static int service_indicate_state(struct connman_service *service)
4040 enum connman_service_state old_state, new_state;
4041 struct connman_service *def_service;
4042 GSequenceIter *iter;
4044 if (service == NULL)
4047 old_state = service->state;
4048 new_state = combine_state(service->state_ipv4, service->state_ipv6);
4050 DBG("service %p old %s - new %s/%s => %s",
4052 state2string(old_state),
4053 state2string(service->state_ipv4),
4054 state2string(service->state_ipv6),
4055 state2string(new_state));
4057 if (old_state == new_state)
4060 def_service = get_default();
4062 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4063 if (def_service != NULL && def_service != service &&
4064 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
4068 service->state = new_state;
4069 state_changed(service);
4071 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
4072 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
4073 reply_pending(service, ECONNABORTED);
4075 __connman_service_disconnect(service);
4078 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
4079 if (__connman_stats_service_register(service) == 0) {
4080 __connman_stats_get(service, FALSE,
4081 &service->stats.data);
4082 __connman_stats_get(service, TRUE,
4083 &service->stats_roaming.data);
4087 if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
4088 __connman_timeserver_sync(service);
4090 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
4091 connman_bool_t reconnect;
4093 reconnect = get_reconnect_state(service);
4094 if (reconnect == TRUE)
4095 __connman_service_auto_connect();
4097 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4100 if (new_state == CONNMAN_SERVICE_STATE_READY) {
4101 enum connman_ipconfig_method method;
4103 set_reconnect_state(service, TRUE);
4105 __connman_service_set_favorite(service, TRUE);
4107 reply_pending(service, 0);
4109 service->userconnect = FALSE;
4111 g_get_current_time(&service->modified);
4112 service_save(service);
4114 update_nameservers(service);
4115 dns_changed(service);
4116 domain_changed(service);
4118 __connman_notifier_connect(service->type);
4120 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4121 connman_network_get_bool(service->network,
4122 "WiFi.UseWPS") == TRUE) {
4125 pass = connman_network_get_string(service->network,
4128 __connman_service_set_passphrase(service, pass);
4130 connman_network_set_bool(service->network,
4131 "WiFi.UseWPS", FALSE);
4136 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4137 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4138 __connman_ipconfig_disable_ipv6(
4139 service->ipconfig_ipv6);
4141 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4142 def_service = get_default();
4144 if (__connman_notifier_count_connected() == 0 &&
4145 def_service != NULL &&
4146 def_service->provider != NULL)
4147 __connman_provider_disconnect(def_service->provider);
4151 __connman_wispr_stop(service);
4153 __connman_wpad_stop(service);
4155 update_nameservers(service);
4156 dns_changed(service);
4157 domain_changed(service);
4159 __connman_timeserver_stop();
4161 __connman_notifier_disconnect(service->type);
4164 * Previous services which are connected and which states
4165 * are set to online should reset relevantly ipconfig_state
4166 * to ready so wispr/portal will be rerun on those
4168 downgrade_connected_services();
4171 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4172 if (service->userconnect == TRUE &&
4173 __connman_agent_report_error(service,
4174 error2string(service->error),
4175 report_error_cb, NULL) == -EIO)
4177 service_complete(service);
4179 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4181 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4183 iter = g_hash_table_lookup(service_hash, service->identifier);
4185 g_sequence_sort_changed(iter, service_compare, NULL);
4187 __connman_connection_update_gateway();
4189 if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
4195 int __connman_service_indicate_error(struct connman_service *service,
4196 enum connman_service_error error)
4198 DBG("service %p error %d", service, error);
4200 if (service == NULL)
4203 service->error = error;
4205 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4206 __connman_service_set_passphrase(service, NULL);
4208 __connman_service_ipconfig_indicate_state(service,
4209 CONNMAN_SERVICE_STATE_FAILURE,
4210 CONNMAN_IPCONFIG_TYPE_IPV4);
4211 __connman_service_ipconfig_indicate_state(service,
4212 CONNMAN_SERVICE_STATE_FAILURE,
4213 CONNMAN_IPCONFIG_TYPE_IPV6);
4217 int __connman_service_clear_error(struct connman_service *service)
4219 DBG("service %p", service);
4221 if (service == NULL)
4224 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4227 service->state_ipv4 = service->state_ipv6 =
4228 CONNMAN_SERVICE_STATE_UNKNOWN;
4229 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
4231 if (service->favorite == TRUE)
4232 set_reconnect_state(service, TRUE);
4234 __connman_service_ipconfig_indicate_state(service,
4235 CONNMAN_SERVICE_STATE_IDLE,
4236 CONNMAN_IPCONFIG_TYPE_IPV6);
4239 * Toggling the IPv6 state to IDLE could trigger the auto connect
4240 * machinery and consequently the IPv4 state.
4242 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4243 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4246 return __connman_service_ipconfig_indicate_state(service,
4247 CONNMAN_SERVICE_STATE_IDLE,
4248 CONNMAN_IPCONFIG_TYPE_IPV4);
4251 int __connman_service_indicate_default(struct connman_service *service)
4253 DBG("service %p", service);
4260 enum connman_service_state __connman_service_ipconfig_get_state(
4261 struct connman_service *service,
4262 enum connman_ipconfig_type type)
4264 if (service == NULL)
4265 return CONNMAN_SERVICE_STATE_UNKNOWN;
4267 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4268 return service->state_ipv4;
4270 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4271 return service->state_ipv6;
4273 return CONNMAN_SERVICE_STATE_UNKNOWN;
4276 static void check_proxy_setup(struct connman_service *service)
4279 * We start WPAD if we haven't got a PAC URL from DHCP and
4280 * if our proxy manual configuration is either empty or set
4281 * to AUTO with an empty URL.
4284 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4287 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4288 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4289 service->pac != NULL))
4292 if (__connman_wpad_start(service) < 0) {
4293 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4294 __connman_notifier_proxy_changed(service);
4301 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
4305 * How many networks are connected at the same time. If more than 1,
4306 * then set the rp_filter setting properly (loose mode routing) so that network
4307 * connectivity works ok. This is only done for IPv4 networks as IPv6
4308 * does not have rp_filter knob.
4310 static int connected_networks_count;
4311 static int original_rp_filter;
4313 static void service_rp_filter(struct connman_service *service,
4316 enum connman_ipconfig_method method;
4318 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4321 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
4322 case CONNMAN_IPCONFIG_METHOD_OFF:
4323 case CONNMAN_IPCONFIG_METHOD_AUTO:
4325 case CONNMAN_IPCONFIG_METHOD_FIXED:
4326 case CONNMAN_IPCONFIG_METHOD_MANUAL:
4327 case CONNMAN_IPCONFIG_METHOD_DHCP:
4331 if (connected == TRUE) {
4332 if (connected_networks_count == 1) {
4334 filter_value = __connman_ipconfig_set_rp_filter();
4335 if (filter_value < 0)
4338 original_rp_filter = filter_value;
4340 connected_networks_count++;
4343 if (connected_networks_count == 2)
4344 __connman_ipconfig_unset_rp_filter(original_rp_filter);
4346 connected_networks_count--;
4347 if (connected_networks_count < 0)
4348 connected_networks_count = 0;
4351 DBG("%s %s ipconfig %p method %d count %d filter %d",
4352 connected ? "connected" : "disconnected", service->identifier,
4353 service->ipconfig_ipv4, method,
4354 connected_networks_count, original_rp_filter);
4357 static gboolean redo_wispr(gpointer user_data)
4359 struct connman_service *service = user_data;
4363 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV6);
4368 int __connman_service_online_check_failed(struct connman_service *service,
4369 enum connman_ipconfig_type type)
4371 DBG("service %p type %d count %d", service, type,
4372 service->online_check_count);
4374 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4375 /* currently we only retry IPv6 stuff */
4378 if (service->online_check_count != 1)
4381 service->online_check_count = 0;
4384 * We set the timeout to 1 sec so that we have a chance to get
4385 * necessary IPv6 router advertisement messages that might have
4388 g_timeout_add_seconds(1, redo_wispr, service);
4393 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
4394 enum connman_service_state new_state,
4395 enum connman_ipconfig_type type)
4397 struct connman_ipconfig *ipconfig = NULL;
4398 enum connman_service_state old_state;
4401 if (service == NULL)
4404 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4405 old_state = service->state_ipv4;
4406 ipconfig = service->ipconfig_ipv4;
4407 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4408 old_state = service->state_ipv6;
4409 ipconfig = service->ipconfig_ipv6;
4412 if (ipconfig == NULL)
4416 if (old_state == new_state)
4419 DBG("service %p (%s) state %d (%s) type %d (%s)",
4420 service, service ? service->identifier : NULL,
4421 new_state, state2string(new_state),
4422 type, __connman_ipconfig_type2string(type));
4424 switch (new_state) {
4425 case CONNMAN_SERVICE_STATE_UNKNOWN:
4426 case CONNMAN_SERVICE_STATE_IDLE:
4427 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
4430 case CONNMAN_SERVICE_STATE_ASSOCIATION:
4432 case CONNMAN_SERVICE_STATE_CONFIGURATION:
4433 __connman_ipconfig_enable(ipconfig);
4435 case CONNMAN_SERVICE_STATE_READY:
4436 update_nameservers(service);
4438 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4439 check_proxy_setup(service);
4440 service_rp_filter(service, TRUE);
4442 service->online_check_count = 1;
4443 __connman_wispr_start(service, type);
4446 case CONNMAN_SERVICE_STATE_ONLINE:
4448 case CONNMAN_SERVICE_STATE_DISCONNECT:
4449 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
4452 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4453 service_rp_filter(service, FALSE);
4456 case CONNMAN_SERVICE_STATE_FAILURE:
4460 /* We keep that state */
4461 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4462 service->state_ipv4 = new_state;
4463 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4464 service->state_ipv6 = new_state;
4466 ret = service_indicate_state(service);
4469 * If the ipconfig method is OFF, then we set the state to IDLE
4470 * so that it will not affect the combined state in the future.
4472 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4473 enum connman_ipconfig_method method;
4474 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4475 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4476 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4477 service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
4478 ret = service_indicate_state(service);
4481 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4482 enum connman_ipconfig_method method;
4483 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4484 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4485 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4486 service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4487 ret = service_indicate_state(service);
4494 static connman_bool_t prepare_network(struct connman_service *service)
4496 enum connman_network_type type;
4497 unsigned int ssid_len;
4499 type = connman_network_get_type(service->network);
4502 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4503 case CONNMAN_NETWORK_TYPE_VENDOR:
4505 case CONNMAN_NETWORK_TYPE_WIFI:
4506 if (connman_network_get_blob(service->network, "WiFi.SSID",
4510 if (service->passphrase != NULL)
4511 connman_network_set_string(service->network,
4512 "WiFi.Passphrase", service->passphrase);
4514 case CONNMAN_NETWORK_TYPE_ETHERNET:
4515 case CONNMAN_NETWORK_TYPE_WIMAX:
4516 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4517 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4518 case CONNMAN_NETWORK_TYPE_CELLULAR:
4525 static void prepare_8021x(struct connman_service *service)
4527 if (service->eap != NULL)
4528 connman_network_set_string(service->network, "WiFi.EAP",
4531 if (service->identity != NULL)
4532 connman_network_set_string(service->network, "WiFi.Identity",
4535 if (service->ca_cert_file != NULL)
4536 connman_network_set_string(service->network, "WiFi.CACertFile",
4537 service->ca_cert_file);
4539 if (service->client_cert_file != NULL)
4540 connman_network_set_string(service->network,
4541 "WiFi.ClientCertFile",
4542 service->client_cert_file);
4544 if (service->private_key_file != NULL)
4545 connman_network_set_string(service->network,
4546 "WiFi.PrivateKeyFile",
4547 service->private_key_file);
4549 if (service->private_key_passphrase != NULL)
4550 connman_network_set_string(service->network,
4551 "WiFi.PrivateKeyPassphrase",
4552 service->private_key_passphrase);
4554 if (service->phase2 != NULL)
4555 connman_network_set_string(service->network, "WiFi.Phase2",
4559 static int service_connect(struct connman_service *service)
4563 if (service->hidden == TRUE)
4566 switch (service->type) {
4567 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4568 case CONNMAN_SERVICE_TYPE_SYSTEM:
4569 case CONNMAN_SERVICE_TYPE_GPS:
4570 case CONNMAN_SERVICE_TYPE_GADGET:
4572 case CONNMAN_SERVICE_TYPE_ETHERNET:
4573 case CONNMAN_SERVICE_TYPE_WIMAX:
4574 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4575 case CONNMAN_SERVICE_TYPE_CELLULAR:
4576 case CONNMAN_SERVICE_TYPE_VPN:
4578 case CONNMAN_SERVICE_TYPE_WIFI:
4579 switch (service->security) {
4580 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4581 case CONNMAN_SERVICE_SECURITY_NONE:
4583 case CONNMAN_SERVICE_SECURITY_WEP:
4584 case CONNMAN_SERVICE_SECURITY_PSK:
4585 case CONNMAN_SERVICE_SECURITY_WPA:
4586 case CONNMAN_SERVICE_SECURITY_RSN:
4587 if (service->passphrase == NULL) {
4588 if (service->network == NULL)
4591 if (service->wps == FALSE ||
4592 connman_network_get_bool(
4594 "WiFi.UseWPS") == FALSE)
4598 case CONNMAN_SERVICE_SECURITY_8021X:
4599 if (service->eap == NULL)
4603 * never request credentials if using EAP-TLS
4604 * (EAP-TLS networks need to be fully provisioned)
4606 if (g_str_equal(service->eap, "tls") == TRUE)
4610 * Return -ENOKEY if either identity or passphrase is
4611 * missing. Agent provided credentials can be used as
4612 * fallback if needed.
4614 if ((service->identity == NULL &&
4615 service->agent_identity == NULL) ||
4616 (service->passphrase == NULL &&
4617 service->agent_passphrase == NULL))
4625 if (service->network != NULL) {
4626 if (prepare_network(service) == FALSE)
4629 switch (service->security) {
4630 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4631 case CONNMAN_SERVICE_SECURITY_NONE:
4632 case CONNMAN_SERVICE_SECURITY_WEP:
4633 case CONNMAN_SERVICE_SECURITY_PSK:
4634 case CONNMAN_SERVICE_SECURITY_WPA:
4635 case CONNMAN_SERVICE_SECURITY_RSN:
4637 case CONNMAN_SERVICE_SECURITY_8021X:
4638 prepare_8021x(service);
4642 if (__connman_stats_service_register(service) == 0) {
4643 __connman_stats_get(service, FALSE,
4644 &service->stats.data);
4645 __connman_stats_get(service, TRUE,
4646 &service->stats_roaming.data);
4649 if (service->ipconfig_ipv4)
4650 __connman_ipconfig_enable(service->ipconfig_ipv4);
4651 if (service->ipconfig_ipv6)
4652 __connman_ipconfig_enable(service->ipconfig_ipv6);
4654 err = __connman_network_connect(service->network);
4655 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4656 service->provider != NULL)
4657 err = __connman_provider_connect(service->provider);
4662 if (err != -EINPROGRESS) {
4663 __connman_ipconfig_disable(service->ipconfig_ipv4);
4664 __connman_ipconfig_disable(service->ipconfig_ipv6);
4665 __connman_stats_service_unregister(service);
4673 int __connman_service_connect(struct connman_service *service)
4677 DBG("service %p state %s", service, state2string(service->state));
4679 if (is_connected(service) == TRUE)
4682 if (is_connecting(service) == TRUE)
4685 switch (service->type) {
4686 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4687 case CONNMAN_SERVICE_TYPE_SYSTEM:
4688 case CONNMAN_SERVICE_TYPE_GPS:
4689 case CONNMAN_SERVICE_TYPE_GADGET:
4692 err = service_connect(service);
4698 if (err == -EINPROGRESS) {
4699 if (service->timeout == 0)
4700 service->timeout = g_timeout_add_seconds(
4701 CONNECT_TIMEOUT, connect_timeout, service);
4703 return -EINPROGRESS;
4706 __connman_service_ipconfig_indicate_state(service,
4707 CONNMAN_SERVICE_STATE_FAILURE,
4708 CONNMAN_IPCONFIG_TYPE_IPV4);
4709 __connman_service_ipconfig_indicate_state(service,
4710 CONNMAN_SERVICE_STATE_FAILURE,
4711 CONNMAN_IPCONFIG_TYPE_IPV6);
4713 if (service->network != NULL)
4714 __connman_network_disconnect(service->network);
4715 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4716 service->provider != NULL)
4717 __connman_provider_disconnect(service->provider);
4719 if (service->userconnect == TRUE) {
4720 if (err == -ENOKEY || err == -EPERM) {
4721 if (__connman_agent_request_passphrase_input(service,
4724 return -EINPROGRESS;
4726 reply_pending(service, -err);
4732 int __connman_service_disconnect(struct connman_service *service)
4736 DBG("service %p", service);
4738 if (service->network != NULL) {
4739 err = __connman_network_disconnect(service->network);
4740 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4741 service->provider != NULL)
4742 err = __connman_provider_disconnect(service->provider);
4746 if (err < 0 && err != -EINPROGRESS)
4749 __connman_6to4_remove(service->ipconfig_ipv4);
4751 if (service->ipconfig_ipv4)
4752 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
4755 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
4758 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
4759 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
4761 __connman_ipconfig_disable(service->ipconfig_ipv4);
4762 __connman_ipconfig_disable(service->ipconfig_ipv6);
4764 __connman_stats_service_unregister(service);
4769 int __connman_service_disconnect_all(void)
4771 GSequenceIter *iter;
4772 GSList *services = NULL, *list;
4776 iter = g_sequence_get_begin_iter(service_list);
4778 while (g_sequence_iter_is_end(iter) == FALSE) {
4779 struct connman_service *service = g_sequence_get(iter);
4781 services = g_slist_prepend(services, service);
4783 iter = g_sequence_iter_next(iter);
4786 for (list = services; list != NULL; list = list->next) {
4787 struct connman_service *service = list->data;
4789 service->ignore = TRUE;
4791 set_reconnect_state(service, FALSE);
4793 __connman_service_disconnect(service);
4803 * lookup_by_identifier:
4804 * @identifier: service identifier
4806 * Look up a service by identifier (reference count will not be increased)
4808 static struct connman_service *lookup_by_identifier(const char *identifier)
4810 GSequenceIter *iter;
4812 iter = g_hash_table_lookup(service_hash, identifier);
4814 return g_sequence_get(iter);
4819 static void provision_changed(gpointer value, gpointer user_data)
4821 struct connman_service *service = value;
4822 char *path = user_data;
4824 __connman_config_provision_service_ident(service, path);
4827 void __connman_service_provision_changed(const char *ident)
4829 g_sequence_foreach(service_list, provision_changed, (void *)ident);
4833 * __connman_service_get:
4834 * @identifier: service identifier
4836 * Look up a service by identifier or create a new one if not found
4838 static struct connman_service *service_get(const char *identifier)
4840 struct connman_service *service;
4841 GSequenceIter *iter;
4843 iter = g_hash_table_lookup(service_hash, identifier);
4845 service = g_sequence_get(iter);
4846 if (service != NULL)
4847 connman_service_ref(service);
4851 service = connman_service_create();
4852 if (service == NULL)
4855 DBG("service %p", service);
4857 service->identifier = g_strdup(identifier);
4859 iter = g_sequence_insert_sorted(service_list, service,
4860 service_compare, NULL);
4862 g_hash_table_insert(service_hash, service->identifier, iter);
4867 static int service_register(struct connman_service *service)
4869 GSequenceIter *iter;
4871 DBG("service %p", service);
4873 if (service->path != NULL)
4876 service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
4877 service->identifier);
4879 DBG("path %s", service->path);
4881 __connman_config_provision_service(service);
4883 service_load(service);
4885 g_dbus_register_interface(connection, service->path,
4886 CONNMAN_SERVICE_INTERFACE,
4887 service_methods, service_signals,
4888 NULL, service, NULL);
4890 iter = g_hash_table_lookup(service_hash, service->identifier);
4892 g_sequence_sort_changed(iter, service_compare, NULL);
4894 __connman_connection_update_gateway();
4899 static void service_up(struct connman_ipconfig *ipconfig)
4901 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
4903 DBG("%s up", __connman_ipconfig_get_ifname(ipconfig));
4905 link_changed(service);
4907 service->stats.valid = FALSE;
4908 service->stats_roaming.valid = FALSE;
4911 static void service_down(struct connman_ipconfig *ipconfig)
4913 DBG("%s down", __connman_ipconfig_get_ifname(ipconfig));
4916 static void service_lower_up(struct connman_ipconfig *ipconfig)
4918 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
4920 DBG("%s lower up", __connman_ipconfig_get_ifname(ipconfig));
4922 stats_start(service);
4925 static void service_lower_down(struct connman_ipconfig *ipconfig)
4927 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
4929 DBG("%s lower down", __connman_ipconfig_get_ifname(ipconfig));
4931 stats_stop(service);
4932 service_save(service);
4935 static void service_ip_bound(struct connman_ipconfig *ipconfig)
4937 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
4938 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4939 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4941 DBG("%s ip bound", __connman_ipconfig_get_ifname(ipconfig));
4943 type = __connman_ipconfig_get_config_type(ipconfig);
4944 method = __connman_ipconfig_get_method(ipconfig);
4946 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4949 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4950 method == CONNMAN_IPCONFIG_METHOD_AUTO)
4951 __connman_service_ipconfig_indicate_state(service,
4952 CONNMAN_SERVICE_STATE_READY,
4953 CONNMAN_IPCONFIG_TYPE_IPV6);
4955 settings_changed(service, ipconfig);
4958 static void service_ip_release(struct connman_ipconfig *ipconfig)
4960 struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
4961 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
4962 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
4964 DBG("%s ip release", __connman_ipconfig_get_ifname(ipconfig));
4966 type = __connman_ipconfig_get_config_type(ipconfig);
4967 method = __connman_ipconfig_get_method(ipconfig);
4969 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
4972 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
4973 method == CONNMAN_IPCONFIG_METHOD_OFF)
4974 __connman_service_ipconfig_indicate_state(service,
4975 CONNMAN_SERVICE_STATE_DISCONNECT,
4976 CONNMAN_IPCONFIG_TYPE_IPV6);
4978 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
4979 method == CONNMAN_IPCONFIG_METHOD_OFF)
4980 __connman_service_ipconfig_indicate_state(service,
4981 CONNMAN_SERVICE_STATE_DISCONNECT,
4982 CONNMAN_IPCONFIG_TYPE_IPV4);
4984 settings_changed(service, ipconfig);
4987 static const struct connman_ipconfig_ops service_ops = {
4989 .down = service_down,
4990 .lower_up = service_lower_up,
4991 .lower_down = service_lower_down,
4992 .ip_bound = service_ip_bound,
4993 .ip_release = service_ip_release,
4996 static void setup_ip4config(struct connman_service *service, int index,
4997 enum connman_ipconfig_method method)
4999 service->ipconfig_ipv4 = __connman_ipconfig_create(index,
5000 CONNMAN_IPCONFIG_TYPE_IPV4);
5001 if (service->ipconfig_ipv4 == NULL)
5004 __connman_ipconfig_set_method(service->ipconfig_ipv4, method);
5006 __connman_ipconfig_set_data(service->ipconfig_ipv4, service);
5008 __connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
5011 static void setup_ip6config(struct connman_service *service, int index)
5013 service->ipconfig_ipv6 = __connman_ipconfig_create(index,
5014 CONNMAN_IPCONFIG_TYPE_IPV6);
5015 if (service->ipconfig_ipv6 == NULL)
5018 __connman_ipconfig_set_data(service->ipconfig_ipv6, service);
5020 __connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
5023 void __connman_service_read_ip4config(struct connman_service *service)
5027 if (service->ipconfig_ipv4 == NULL)
5030 keyfile = connman_storage_load_service(service->identifier);
5031 if (keyfile == NULL)
5034 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5035 service->identifier, "IPv4.");
5037 g_key_file_free(keyfile);
5040 void __connman_service_create_ip4config(struct connman_service *service,
5043 DBG("ipv4 %p", service->ipconfig_ipv4);
5045 if (service->ipconfig_ipv4 != NULL)
5048 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5049 __connman_service_read_ip4config(service);
5052 void __connman_service_read_ip6config(struct connman_service *service)
5056 if (service->ipconfig_ipv6 == NULL)
5059 keyfile = connman_storage_load_service(service->identifier);
5060 if (keyfile == NULL)
5063 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5064 service->identifier, "IPv6.");
5066 g_key_file_free(keyfile);
5069 void __connman_service_create_ip6config(struct connman_service *service,
5072 DBG("ipv6 %p", service->ipconfig_ipv6);
5074 if (service->ipconfig_ipv6 != NULL)
5077 setup_ip6config(service, index);
5079 __connman_service_read_ip6config(service);
5083 * __connman_service_lookup_from_network:
5084 * @network: network structure
5086 * Look up a service by network (reference count will not be increased)
5088 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
5090 struct connman_service *service;
5091 const char *ident, *group;
5094 DBG("network %p", network);
5096 if (network == NULL)
5099 ident = __connman_network_get_ident(network);
5103 group = connman_network_get_group(network);
5107 name = g_strdup_printf("%s_%s_%s",
5108 __connman_network_get_type(network), ident, group);
5109 service = lookup_by_identifier(name);
5115 struct connman_service *__connman_service_lookup_from_index(int index)
5117 struct connman_service *service;
5118 GSequenceIter *iter;
5120 iter = g_sequence_get_begin_iter(service_list);
5122 while (g_sequence_iter_is_end(iter) == FALSE) {
5123 service = g_sequence_get(iter);
5125 if (__connman_ipconfig_get_index(service->ipconfig_ipv4)
5129 if (__connman_ipconfig_get_index(service->ipconfig_ipv6)
5133 iter = g_sequence_iter_next(iter);
5139 const char *__connman_service_get_ident(struct connman_service *service)
5141 return service->identifier;
5144 const char *__connman_service_get_path(struct connman_service *service)
5146 return service->path;
5149 unsigned int __connman_service_get_order(struct connman_service *service)
5151 GSequenceIter *iter;
5153 if (service == NULL)
5156 if (service->favorite == FALSE) {
5161 iter = g_hash_table_lookup(service_hash, service->identifier);
5163 if (g_sequence_iter_get_position(iter) == 0)
5165 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
5166 service->order = 10;
5172 return service->order;
5175 static enum connman_service_type convert_network_type(struct connman_network *network)
5177 enum connman_network_type type = connman_network_get_type(network);
5180 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5181 case CONNMAN_NETWORK_TYPE_VENDOR:
5183 case CONNMAN_NETWORK_TYPE_ETHERNET:
5184 return CONNMAN_SERVICE_TYPE_ETHERNET;
5185 case CONNMAN_NETWORK_TYPE_WIFI:
5186 return CONNMAN_SERVICE_TYPE_WIFI;
5187 case CONNMAN_NETWORK_TYPE_WIMAX:
5188 return CONNMAN_SERVICE_TYPE_WIMAX;
5189 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5190 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5191 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5192 case CONNMAN_NETWORK_TYPE_CELLULAR:
5193 return CONNMAN_SERVICE_TYPE_CELLULAR;
5196 return CONNMAN_SERVICE_TYPE_UNKNOWN;
5199 static enum connman_service_security convert_wifi_security(const char *security)
5201 if (security == NULL)
5202 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5203 else if (g_str_equal(security, "none") == TRUE)
5204 return CONNMAN_SERVICE_SECURITY_NONE;
5205 else if (g_str_equal(security, "wep") == TRUE)
5206 return CONNMAN_SERVICE_SECURITY_WEP;
5207 else if (g_str_equal(security, "psk") == TRUE)
5208 return CONNMAN_SERVICE_SECURITY_PSK;
5209 else if (g_str_equal(security, "ieee8021x") == TRUE)
5210 return CONNMAN_SERVICE_SECURITY_8021X;
5211 else if (g_str_equal(security, "wpa") == TRUE)
5212 return CONNMAN_SERVICE_SECURITY_WPA;
5213 else if (g_str_equal(security, "rsn") == TRUE)
5214 return CONNMAN_SERVICE_SECURITY_RSN;
5216 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5219 static void update_from_network(struct connman_service *service,
5220 struct connman_network *network)
5222 connman_uint8_t strength = service->strength;
5223 GSequenceIter *iter;
5226 DBG("service %p network %p", service, network);
5228 if (is_connected(service) == TRUE)
5231 if (is_connecting(service) == TRUE)
5234 str = connman_network_get_string(network, "Name");
5236 g_free(service->name);
5237 service->name = g_strdup(str);
5238 service->hidden = FALSE;
5240 g_free(service->name);
5241 service->name = NULL;
5242 service->hidden = TRUE;
5245 service->strength = connman_network_get_strength(network);
5246 service->roaming = connman_network_get_bool(network, "Roaming");
5248 if (service->strength == 0) {
5250 * Filter out 0-values; it's unclear what they mean
5251 * and they cause anomalous sorting of the priority list.
5253 service->strength = strength;
5256 str = connman_network_get_string(network, "WiFi.Security");
5257 service->security = convert_wifi_security(str);
5259 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5260 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5262 if (service->strength > strength && service->network != NULL) {
5263 connman_network_unref(service->network);
5264 service->network = connman_network_ref(network);
5266 strength_changed(service);
5269 if (service->network == NULL)
5270 service->network = connman_network_ref(network);
5272 iter = g_hash_table_lookup(service_hash, service->identifier);
5274 g_sequence_sort_changed(iter, service_compare, NULL);
5278 * __connman_service_create_from_network:
5279 * @network: network structure
5281 * Look up service by network and if not found, create one
5283 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5285 struct connman_service *service;
5286 struct connman_device *device;
5287 const char *ident, *group;
5291 DBG("network %p", network);
5293 if (network == NULL)
5296 ident = __connman_network_get_ident(network);
5300 group = connman_network_get_group(network);
5304 name = g_strdup_printf("%s_%s_%s",
5305 __connman_network_get_type(network), ident, group);
5306 service = service_get(name);
5309 if (service == NULL)
5312 if (__connman_network_get_weakness(network) == TRUE)
5315 if (service->path != NULL) {
5316 update_from_network(service, network);
5317 __connman_connection_update_gateway();
5321 service->type = convert_network_type(network);
5323 switch (service->type) {
5324 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5325 case CONNMAN_SERVICE_TYPE_SYSTEM:
5326 case CONNMAN_SERVICE_TYPE_ETHERNET:
5327 case CONNMAN_SERVICE_TYPE_WIMAX:
5328 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5329 case CONNMAN_SERVICE_TYPE_GPS:
5330 case CONNMAN_SERVICE_TYPE_VPN:
5331 case CONNMAN_SERVICE_TYPE_GADGET:
5332 service->autoconnect = FALSE;
5334 case CONNMAN_SERVICE_TYPE_WIFI:
5335 case CONNMAN_SERVICE_TYPE_CELLULAR:
5336 service->autoconnect = TRUE;
5340 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5341 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5343 update_from_network(service, network);
5345 index = connman_network_get_index(network);
5347 if (service->ipconfig_ipv4 == NULL)
5348 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5350 if (service->ipconfig_ipv6 == NULL)
5351 setup_ip6config(service, index);
5353 service_register(service);
5355 if (service->favorite == TRUE) {
5356 device = connman_network_get_device(service->network);
5357 if (device && __connman_device_scanning(device) == FALSE)
5358 __connman_service_auto_connect();
5361 __connman_notifier_service_add(service, service->name);
5362 service_schedule_added(service);
5367 void __connman_service_update_from_network(struct connman_network *network)
5369 connman_bool_t need_sort = FALSE;
5370 struct connman_service *service;
5371 connman_uint8_t strength;
5372 connman_bool_t roaming;
5373 GSequenceIter *iter;
5375 connman_bool_t stats_enable;
5377 DBG("network %p", network);
5379 service = __connman_service_lookup_from_network(network);
5380 if (service == NULL)
5383 if (service->network == NULL)
5386 name = connman_network_get_string(service->network, "Name");
5387 if (g_strcmp0(service->name, name) != 0) {
5388 g_free(service->name);
5389 service->name = g_strdup(name);
5390 connman_dbus_property_changed_basic(service->path,
5391 CONNMAN_SERVICE_INTERFACE, "Name",
5392 DBUS_TYPE_STRING, &service->name);
5395 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5396 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5398 strength = connman_network_get_strength(service->network);
5399 if (strength == service->strength)
5402 service->strength = strength;
5405 strength_changed(service);
5408 roaming = connman_network_get_bool(service->network, "Roaming");
5409 if (roaming == service->roaming)
5412 stats_enable = stats_enabled(service);
5413 if (stats_enable == TRUE)
5414 stats_stop(service);
5416 service->roaming = roaming;
5419 if (stats_enable == TRUE)
5420 stats_start(service);
5422 roaming_changed(service);
5425 if (need_sort == TRUE) {
5426 iter = g_hash_table_lookup(service_hash, service->identifier);
5428 g_sequence_sort_changed(iter, service_compare, NULL);
5432 void __connman_service_remove_from_network(struct connman_network *network)
5434 struct connman_service *service;
5436 DBG("network %p", network);
5438 service = __connman_service_lookup_from_network(network);
5439 if (service == NULL)
5442 __connman_connection_gateway_remove(service,
5443 CONNMAN_IPCONFIG_TYPE_ALL);
5445 connman_service_unref(service);
5449 * __connman_service_create_from_provider:
5450 * @provider: provider structure
5452 * Look up service by provider and if not found, create one
5454 struct connman_service *
5455 __connman_service_create_from_provider(struct connman_provider *provider)
5457 struct connman_service *service;
5458 const char *ident, *str;
5460 int index = connman_provider_get_index(provider);
5462 DBG("provider %p", provider);
5464 ident = __connman_provider_get_ident(provider);
5468 name = g_strdup_printf("vpn_%s", ident);
5469 service = service_get(name);
5472 if (service == NULL)
5475 service->type = CONNMAN_SERVICE_TYPE_VPN;
5476 service->provider = connman_provider_ref(provider);
5477 service->autoconnect = FALSE;
5478 service->userconnect = TRUE;
5480 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5481 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5483 str = connman_provider_get_string(provider, "Name");
5485 g_free(service->name);
5486 service->name = g_strdup(str);
5487 service->hidden = FALSE;
5489 g_free(service->name);
5490 service->name = NULL;
5491 service->hidden = TRUE;
5494 service->strength = 0;
5496 if (service->ipconfig_ipv4 == NULL)
5497 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5499 if (service->ipconfig_ipv6 == NULL)
5500 setup_ip6config(service, index);
5502 service_register(service);
5504 __connman_notifier_service_add(service, service->name);
5505 service_schedule_added(service);
5510 int __connman_service_init(void)
5514 connection = connman_dbus_get_connection();
5516 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5519 service_list = g_sequence_new(service_free);
5521 services_notify = g_new0(struct _services_notify, 1);
5522 services_notify->remove = g_hash_table_new_full(g_str_hash,
5523 g_str_equal, g_free, NULL);
5524 services_notify->add = g_hash_table_new(g_str_hash, g_str_equal);
5529 void __connman_service_cleanup(void)
5535 list = service_list;
5536 service_list = NULL;
5537 g_sequence_free(list);
5539 g_hash_table_destroy(service_hash);
5540 service_hash = NULL;
5542 g_slist_free(counter_list);
5543 counter_list = NULL;
5545 if (services_notify->id != 0) {
5546 g_source_remove(services_notify->id);
5547 service_send_signals(NULL);
5548 g_hash_table_destroy(services_notify->remove);
5549 g_hash_table_destroy(services_notify->add);
5551 g_free(services_notify);
5553 dbus_connection_unref(connection);