5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <connman/storage.h>
36 #define CONNECT_TIMEOUT 120
38 static DBusConnection *connection = NULL;
40 static GSequence *service_list = NULL;
41 static GHashTable *service_hash = NULL;
42 static GSList *counter_list = NULL;
44 struct connman_stats {
46 connman_bool_t enabled;
47 struct connman_stats_data data_last;
48 struct connman_stats_data data;
52 struct connman_stats_counter {
53 connman_bool_t append_all;
54 struct connman_stats stats;
55 struct connman_stats stats_roaming;
58 struct connman_service {
60 int session_usage_count;
63 enum connman_service_type type;
64 enum connman_service_security security;
65 enum connman_service_state state;
66 enum connman_service_state state_ipv4;
67 enum connman_service_state state_ipv6;
68 enum connman_service_error error;
69 connman_uint8_t strength;
70 connman_bool_t favorite;
71 connman_bool_t immutable;
72 connman_bool_t hidden;
73 connman_bool_t ignore;
74 connman_bool_t autoconnect;
75 connman_bool_t userconnect;
80 char *agent_passphrase;
81 connman_bool_t roaming;
82 connman_bool_t login_required;
83 connman_bool_t network_created;
84 struct connman_ipconfig *ipconfig_ipv4;
85 struct connman_ipconfig *ipconfig_ipv6;
86 struct connman_network *network;
87 struct connman_provider *provider;
89 char **nameservers_config;
90 char **nameservers_auto;
94 /* 802.1x settings from the config files */
99 char *client_cert_file;
100 char *private_key_file;
101 char *private_key_passphrase;
103 DBusMessage *pending;
105 struct connman_stats stats;
106 struct connman_stats stats_roaming;
107 GHashTable *counter_table;
108 enum connman_service_proxy_method proxy;
109 enum connman_service_proxy_method proxy_config;
116 static void append_path(gpointer value, gpointer user_data)
118 struct connman_service *service = value;
119 DBusMessageIter *iter = user_data;
121 if (service->path == NULL || service->hidden == TRUE)
124 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
128 void __connman_service_list(DBusMessageIter *iter, void *user_data)
130 if (service_list == NULL)
133 g_sequence_foreach(service_list, append_path, iter);
138 struct connman_service *service;
141 static void compare_path(gpointer value, gpointer user_data)
143 struct connman_service *service = value;
144 struct find_data *data = user_data;
146 if (data->service != NULL)
149 if (g_strcmp0(service->path, data->path) == 0)
150 data->service = service;
153 static struct connman_service *find_service(const char *path)
155 struct find_data data = { .path = path, .service = NULL };
157 DBG("path %s", path);
159 g_sequence_foreach(service_list, compare_path, &data);
164 const char *__connman_service_type2string(enum connman_service_type type)
167 case CONNMAN_SERVICE_TYPE_UNKNOWN:
169 case CONNMAN_SERVICE_TYPE_SYSTEM:
171 case CONNMAN_SERVICE_TYPE_ETHERNET:
173 case CONNMAN_SERVICE_TYPE_WIFI:
175 case CONNMAN_SERVICE_TYPE_WIMAX:
177 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
179 case CONNMAN_SERVICE_TYPE_CELLULAR:
181 case CONNMAN_SERVICE_TYPE_GPS:
183 case CONNMAN_SERVICE_TYPE_VPN:
185 case CONNMAN_SERVICE_TYPE_GADGET:
192 static const char *security2string(enum connman_service_security security)
195 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
197 case CONNMAN_SERVICE_SECURITY_NONE:
199 case CONNMAN_SERVICE_SECURITY_WEP:
201 case CONNMAN_SERVICE_SECURITY_PSK:
203 case CONNMAN_SERVICE_SECURITY_8021X:
205 case CONNMAN_SERVICE_SECURITY_WPA:
207 case CONNMAN_SERVICE_SECURITY_RSN:
214 static const char *state2string(enum connman_service_state state)
217 case CONNMAN_SERVICE_STATE_UNKNOWN:
219 case CONNMAN_SERVICE_STATE_IDLE:
221 case CONNMAN_SERVICE_STATE_ASSOCIATION:
222 return "association";
223 case CONNMAN_SERVICE_STATE_CONFIGURATION:
224 return "configuration";
225 case CONNMAN_SERVICE_STATE_READY:
227 case CONNMAN_SERVICE_STATE_ONLINE:
229 case CONNMAN_SERVICE_STATE_DISCONNECT:
231 case CONNMAN_SERVICE_STATE_FAILURE:
238 static const char *error2string(enum connman_service_error error)
241 case CONNMAN_SERVICE_ERROR_UNKNOWN:
243 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
244 return "out-of-range";
245 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
246 return "pin-missing";
247 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
248 return "dhcp-failed";
249 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
250 return "connect-failed";
251 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
252 return "login-failed";
253 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
254 return "auth-failed";
255 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
256 return "invalid-key";
262 static enum connman_service_error string2error(const char *error)
264 if (g_strcmp0(error, "dhcp-failed") == 0)
265 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
266 else if (g_strcmp0(error, "pin-missing") == 0)
267 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
268 else if (g_strcmp0(error, "invalid-key") == 0)
269 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
271 return CONNMAN_SERVICE_ERROR_UNKNOWN;
274 static const char *proxymethod2string(enum connman_service_proxy_method method)
277 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
279 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
281 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
283 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
290 static enum connman_service_proxy_method string2proxymethod(const char *method)
292 if (g_strcmp0(method, "direct") == 0)
293 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
294 else if (g_strcmp0(method, "auto") == 0)
295 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
296 else if (g_strcmp0(method, "manual") == 0)
297 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
299 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
302 static int service_load(struct connman_service *service)
305 GError *error = NULL;
308 connman_bool_t autoconnect;
309 unsigned int ssid_len;
312 DBG("service %p", service);
314 keyfile = connman_storage_load_service(service->identifier);
318 switch (service->type) {
319 case CONNMAN_SERVICE_TYPE_UNKNOWN:
320 case CONNMAN_SERVICE_TYPE_SYSTEM:
321 case CONNMAN_SERVICE_TYPE_ETHERNET:
322 case CONNMAN_SERVICE_TYPE_GPS:
323 case CONNMAN_SERVICE_TYPE_VPN:
324 case CONNMAN_SERVICE_TYPE_GADGET:
326 case CONNMAN_SERVICE_TYPE_WIFI:
327 if (service->name == NULL) {
330 name = g_key_file_get_string(keyfile,
331 service->identifier, "Name", NULL);
333 g_free(service->name);
334 service->name = name;
337 if (service->network != NULL)
338 connman_network_set_name(service->network,
342 if (service->network &&
343 connman_network_get_blob(service->network,
344 "WiFi.SSID", &ssid_len) == NULL) {
347 hex_ssid = g_key_file_get_string(keyfile,
351 if (hex_ssid != NULL) {
353 unsigned int i, j = 0, hex;
354 size_t hex_ssid_len = strlen(hex_ssid);
356 ssid = g_try_malloc0(hex_ssid_len / 2);
363 for (i = 0; i < hex_ssid_len; i += 2) {
364 sscanf(hex_ssid + i, "%02x", &hex);
368 connman_network_set_blob(service->network,
369 "WiFi.SSID", ssid, hex_ssid_len / 2);
376 case CONNMAN_SERVICE_TYPE_WIMAX:
377 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
378 case CONNMAN_SERVICE_TYPE_CELLULAR:
379 service->favorite = g_key_file_get_boolean(keyfile,
380 service->identifier, "Favorite", NULL);
382 autoconnect = g_key_file_get_boolean(keyfile,
383 service->identifier, "AutoConnect", &error);
385 service->autoconnect = autoconnect;
386 g_clear_error(&error);
388 str = g_key_file_get_string(keyfile,
389 service->identifier, "Failure", NULL);
391 if (service->favorite == FALSE)
392 service->state_ipv4 = service->state_ipv6 =
393 CONNMAN_SERVICE_STATE_FAILURE;
394 service->error = string2error(str);
400 str = g_key_file_get_string(keyfile,
401 service->identifier, "Modified", NULL);
403 g_time_val_from_iso8601(str, &service->modified);
407 str = g_key_file_get_string(keyfile,
408 service->identifier, "Passphrase", NULL);
410 g_free(service->passphrase);
411 service->passphrase = str;
414 if (service->ipconfig_ipv4 != NULL)
415 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
416 service->identifier, "IPv4.");
418 if (service->ipconfig_ipv6 != NULL)
419 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
420 service->identifier, "IPv6.");
422 service->nameservers_config = g_key_file_get_string_list(keyfile,
423 service->identifier, "Nameservers", &length, NULL);
424 if (service->nameservers_config != NULL && length == 0) {
425 g_strfreev(service->nameservers_config);
426 service->nameservers_config = NULL;
429 service->domains = g_key_file_get_string_list(keyfile,
430 service->identifier, "Domains", &length, NULL);
431 if (service->domains != NULL && length == 0) {
432 g_strfreev(service->domains);
433 service->domains = NULL;
436 str = g_key_file_get_string(keyfile,
437 service->identifier, "Proxy.Method", NULL);
439 service->proxy_config = string2proxymethod(str);
443 service->proxies = g_key_file_get_string_list(keyfile,
444 service->identifier, "Proxy.Servers", &length, NULL);
445 if (service->proxies != NULL && length == 0) {
446 g_strfreev(service->proxies);
447 service->proxies = NULL;
450 service->excludes = g_key_file_get_string_list(keyfile,
451 service->identifier, "Proxy.Excludes", &length, NULL);
452 if (service->excludes != NULL && length == 0) {
453 g_strfreev(service->excludes);
454 service->excludes = NULL;
457 str = g_key_file_get_string(keyfile,
458 service->identifier, "Proxy.URL", NULL);
460 g_free(service->pac);
465 g_key_file_free(keyfile);
470 static int service_save(struct connman_service *service)
475 const char *cst_str = NULL;
478 DBG("service %p", service);
480 keyfile = __connman_storage_open_service(service->identifier);
484 if (service->name != NULL)
485 g_key_file_set_string(keyfile, service->identifier,
486 "Name", service->name);
488 switch (service->type) {
489 case CONNMAN_SERVICE_TYPE_UNKNOWN:
490 case CONNMAN_SERVICE_TYPE_SYSTEM:
491 case CONNMAN_SERVICE_TYPE_ETHERNET:
492 case CONNMAN_SERVICE_TYPE_GPS:
493 case CONNMAN_SERVICE_TYPE_VPN:
494 case CONNMAN_SERVICE_TYPE_GADGET:
496 case CONNMAN_SERVICE_TYPE_WIFI:
497 if (service->network) {
498 const unsigned char *ssid;
499 unsigned int ssid_len = 0;
501 ssid = connman_network_get_blob(service->network,
502 "WiFi.SSID", &ssid_len);
504 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
505 char *identifier = service->identifier;
509 str = g_string_sized_new(ssid_len * 2);
515 for (i = 0; i < ssid_len; i++)
516 g_string_append_printf(str,
519 g_key_file_set_string(keyfile, identifier,
522 g_string_free(str, TRUE);
525 freq = connman_network_get_frequency(service->network);
526 g_key_file_set_integer(keyfile, service->identifier,
531 case CONNMAN_SERVICE_TYPE_WIMAX:
532 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
533 case CONNMAN_SERVICE_TYPE_CELLULAR:
534 g_key_file_set_boolean(keyfile, service->identifier,
535 "Favorite", service->favorite);
537 if (service->favorite == TRUE)
538 g_key_file_set_boolean(keyfile, service->identifier,
539 "AutoConnect", service->autoconnect);
541 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
542 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
543 const char *failure = error2string(service->error);
545 g_key_file_set_string(keyfile,
549 g_key_file_remove_key(keyfile, service->identifier,
555 str = g_time_val_to_iso8601(&service->modified);
557 g_key_file_set_string(keyfile, service->identifier,
562 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
563 g_key_file_set_string(keyfile, service->identifier,
564 "Passphrase", service->passphrase);
566 g_key_file_remove_key(keyfile, service->identifier,
569 if (service->ipconfig_ipv4 != NULL)
570 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
571 service->identifier, "IPv4.");
573 if (service->ipconfig_ipv6 != NULL)
574 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
575 service->identifier, "IPv6.");
577 if (service->nameservers_config != NULL) {
578 guint len = g_strv_length(service->nameservers_config);
580 g_key_file_set_string_list(keyfile, service->identifier,
582 (const gchar **) service->nameservers_config, len);
584 g_key_file_remove_key(keyfile, service->identifier,
585 "Nameservers", NULL);
587 if (service->domains != NULL) {
588 guint len = g_strv_length(service->domains);
590 g_key_file_set_string_list(keyfile, service->identifier,
592 (const gchar **) service->domains, len);
594 g_key_file_remove_key(keyfile, service->identifier,
597 cst_str = proxymethod2string(service->proxy_config);
599 g_key_file_set_string(keyfile, service->identifier,
600 "Proxy.Method", cst_str);
602 if (service->proxies != NULL) {
603 guint len = g_strv_length(service->proxies);
605 g_key_file_set_string_list(keyfile, service->identifier,
607 (const gchar **) service->proxies, len);
609 g_key_file_remove_key(keyfile, service->identifier,
610 "Proxy.Servers", NULL);
612 if (service->excludes != NULL) {
613 guint len = g_strv_length(service->excludes);
615 g_key_file_set_string_list(keyfile, service->identifier,
617 (const gchar **) service->excludes, len);
619 g_key_file_remove_key(keyfile, service->identifier,
620 "Proxy.Excludes", NULL);
622 if (service->pac != NULL && strlen(service->pac) > 0)
623 g_key_file_set_string(keyfile, service->identifier,
624 "Proxy.URL", service->pac);
626 g_key_file_remove_key(keyfile, service->identifier,
630 __connman_storage_save_service(keyfile, service->identifier);
632 g_key_file_free(keyfile);
637 static guint changed_timeout = 0;
639 static gboolean notify_services_changed(gpointer user_data)
643 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
644 CONNMAN_MANAGER_INTERFACE, "Services",
645 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
651 static void services_changed(gboolean delayed)
655 if (changed_timeout > 0) {
656 g_source_remove(changed_timeout);
660 if (__connman_connection_update_gateway() == TRUE) {
661 notify_services_changed(NULL);
665 if (delayed == FALSE) {
666 notify_services_changed(NULL);
670 changed_timeout = g_timeout_add_seconds(1, notify_services_changed,
674 static enum connman_service_state combine_state(
675 enum connman_service_state state_a,
676 enum connman_service_state state_b)
678 enum connman_service_state result;
680 if (state_a == state_b) {
685 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
690 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
695 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
700 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
705 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
710 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
715 if (state_a == CONNMAN_SERVICE_STATE_READY) {
720 if (state_b == CONNMAN_SERVICE_STATE_READY) {
725 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
730 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
735 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
740 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
745 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
750 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
755 result = CONNMAN_SERVICE_STATE_FAILURE;
761 static connman_bool_t is_connecting_state(struct connman_service *service,
762 enum connman_service_state state)
765 case CONNMAN_SERVICE_STATE_UNKNOWN:
766 case CONNMAN_SERVICE_STATE_IDLE:
767 case CONNMAN_SERVICE_STATE_FAILURE:
768 if (service->network != NULL)
769 return connman_network_get_connecting(service->network);
770 case CONNMAN_SERVICE_STATE_DISCONNECT:
771 case CONNMAN_SERVICE_STATE_READY:
772 case CONNMAN_SERVICE_STATE_ONLINE:
774 case CONNMAN_SERVICE_STATE_ASSOCIATION:
775 case CONNMAN_SERVICE_STATE_CONFIGURATION:
782 static connman_bool_t is_connected_state(const struct connman_service *service,
783 enum connman_service_state state)
786 case CONNMAN_SERVICE_STATE_UNKNOWN:
787 case CONNMAN_SERVICE_STATE_IDLE:
788 case CONNMAN_SERVICE_STATE_ASSOCIATION:
789 case CONNMAN_SERVICE_STATE_CONFIGURATION:
790 case CONNMAN_SERVICE_STATE_DISCONNECT:
791 case CONNMAN_SERVICE_STATE_FAILURE:
793 case CONNMAN_SERVICE_STATE_READY:
794 case CONNMAN_SERVICE_STATE_ONLINE:
801 static connman_bool_t is_connecting(struct connman_service *service)
803 return is_connecting_state(service, service->state);
806 static connman_bool_t is_connected(struct connman_service *service)
808 return is_connected_state(service, service->state);
811 static void update_nameservers(struct connman_service *service)
815 if (service->ipconfig_ipv4)
816 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
817 else if (service->ipconfig_ipv6)
818 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
825 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
826 case CONNMAN_SERVICE_STATE_UNKNOWN:
827 case CONNMAN_SERVICE_STATE_IDLE:
828 case CONNMAN_SERVICE_STATE_ASSOCIATION:
829 case CONNMAN_SERVICE_STATE_CONFIGURATION:
831 case CONNMAN_SERVICE_STATE_FAILURE:
832 case CONNMAN_SERVICE_STATE_DISCONNECT:
833 connman_resolver_remove_all(ifname);
835 case CONNMAN_SERVICE_STATE_READY:
836 case CONNMAN_SERVICE_STATE_ONLINE:
840 if (service->nameservers_config != NULL) {
843 for (i = 0; service->nameservers_config[i] != NULL; i++) {
844 connman_resolver_append(ifname, NULL,
845 service->nameservers_config[i]);
847 } else if (service->nameservers != NULL) {
850 for (i = 0; service->nameservers[i] != NULL; i++) {
851 connman_resolver_append(ifname, NULL,
852 service->nameservers[i]);
856 if (service->domains != NULL) {
859 for (i = 0; service->domains[i]; i++)
860 connman_resolver_append(ifname, service->domains[i],
862 } else if (service->domainname != NULL)
863 connman_resolver_append(ifname, service->domainname, NULL);
865 connman_resolver_flush();
869 * The is_auto variable is set to true when IPv6 autoconf nameservers are
870 * inserted to resolver via netlink message (see rtnl.c:rtnl_newnduseropt()
871 * for details) and not through service.c
873 int __connman_service_nameserver_append(struct connman_service *service,
874 const char *nameserver, gboolean is_auto)
879 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
881 if (nameserver == NULL)
885 nameservers = service->nameservers_auto;
887 nameservers = service->nameservers;
889 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
890 if (g_strcmp0(nameservers[i], nameserver) == 0)
893 if (nameservers != NULL) {
894 len = g_strv_length(nameservers);
895 nameservers = g_try_renew(char *, nameservers, len + 2);
898 nameservers = g_try_new0(char *, len + 2);
901 if (nameservers == NULL)
904 nameservers[len] = g_strdup(nameserver);
905 if (nameservers[len] == NULL)
908 nameservers[len + 1] = NULL;
910 if (is_auto == TRUE) {
911 service->nameservers_auto = nameservers;
913 service->nameservers = nameservers;
914 update_nameservers(service);
920 int __connman_service_nameserver_remove(struct connman_service *service,
921 const char *nameserver, gboolean is_auto)
923 char **servers, **nameservers;
924 gboolean found = FALSE;
927 DBG("service %p nameserver %s auto %d", service, nameserver, is_auto);
929 if (nameserver == NULL)
933 nameservers = service->nameservers_auto;
935 nameservers = service->nameservers;
937 if (nameservers == NULL)
940 for (i = 0; nameservers != NULL && nameservers[i] != NULL; i++)
941 if (g_strcmp0(nameservers[i], nameserver) == 0) {
949 len = g_strv_length(nameservers);
952 g_strfreev(nameservers);
954 service->nameservers_auto = NULL;
956 service->nameservers = NULL;
961 servers = g_try_new0(char *, len);
965 for (i = 0, j = 0; i < len; i++) {
966 if (g_strcmp0(nameservers[i], nameserver) != 0) {
967 servers[j] = g_strdup(nameservers[i]);
968 if (servers[j] == NULL)
973 servers[len - 1] = NULL;
975 g_strfreev(nameservers);
976 nameservers = servers;
978 if (is_auto == TRUE) {
979 service->nameservers_auto = nameservers;
981 service->nameservers = nameservers;
982 update_nameservers(service);
988 void __connman_service_nameserver_clear(struct connman_service *service)
990 g_strfreev(service->nameservers);
991 service->nameservers = NULL;
993 update_nameservers(service);
996 static void nameserver_add_routes(int index, char **nameservers,
1000 struct addrinfo hints;
1001 struct addrinfo *addr;
1003 for (i = 0; nameservers[i] != NULL; i++) {
1004 memset(&hints, 0, sizeof(struct addrinfo));
1005 hints.ai_flags = AI_NUMERICHOST;
1008 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1009 if (ret == EAI_NONAME)
1010 family = AF_INET; /* use the IPv4 as a default */
1014 family = addr->ai_family;
1016 if (family == AF_INET) {
1017 if (connman_inet_compare_subnet(index,
1018 nameservers[i]) != TRUE)
1019 connman_inet_add_host_route(index,
1020 nameservers[i], gw);
1021 } else if (family == AF_INET6)
1022 connman_inet_add_ipv6_host_route(index,
1023 nameservers[i], gw);
1029 static void nameserver_del_routes(int index, char **nameservers)
1032 struct addrinfo hints;
1033 struct addrinfo *addr;
1035 for (i = 0; nameservers[i] != NULL; i++) {
1036 memset(&hints, 0, sizeof(struct addrinfo));
1037 hints.ai_flags = AI_NUMERICHOST;
1040 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1041 if (ret == EAI_NONAME)
1042 family = AF_INET; /* use the IPv4 as a default */
1046 family = addr->ai_family;
1048 if (family == AF_INET)
1049 connman_inet_del_host_route(index, nameservers[i]);
1050 else if (family == AF_INET6)
1051 connman_inet_del_ipv6_host_route(index,
1058 void __connman_service_nameserver_add_routes(struct connman_service *service,
1063 if (service == NULL)
1066 if (service->network != NULL)
1067 index = connman_network_get_index(service->network);
1068 else if (service->provider != NULL)
1069 index = connman_provider_get_index(service->provider);
1071 if (service->nameservers_config != NULL) {
1073 * Configured nameserver takes preference over the
1074 * discoverd nameserver gathered from DHCP, VPN, etc.
1076 nameserver_add_routes(index, service->nameservers_config, gw);
1077 } else if (service->nameservers != NULL) {
1079 * We add nameservers host routes for nameservers that
1080 * are not on our subnet. For those who are, the subnet
1081 * route will be installed by the time the dns proxy code
1082 * tries to reach them. The subnet route is installed
1083 * when setting the interface IP address.
1085 nameserver_add_routes(index, service->nameservers, gw);
1089 void __connman_service_nameserver_del_routes(struct connman_service *service)
1093 if (service == NULL)
1096 if (service->network != NULL)
1097 index = connman_network_get_index(service->network);
1098 else if (service->provider != NULL)
1099 index = connman_provider_get_index(service->provider);
1101 if (service->nameservers_config != NULL)
1102 nameserver_del_routes(index, service->nameservers_config);
1103 else if (service->nameservers != NULL)
1104 nameserver_del_routes(index, service->nameservers);
1107 static struct connman_stats *stats_get(struct connman_service *service)
1109 if (service->roaming == TRUE)
1110 return &service->stats_roaming;
1112 return &service->stats;
1115 static connman_bool_t stats_enabled(struct connman_service *service)
1117 struct connman_stats *stats = stats_get(service);
1119 return stats->enabled;
1122 static void stats_start(struct connman_service *service)
1124 struct connman_stats *stats = stats_get(service);
1126 DBG("service %p", service);
1128 if (stats->timer == NULL)
1131 stats->enabled = TRUE;
1132 stats->data_last.time = stats->data.time;
1134 g_timer_start(stats->timer);
1137 static void stats_stop(struct connman_service *service)
1139 struct connman_stats *stats = stats_get(service);
1140 unsigned int seconds;
1142 DBG("service %p", service);
1144 if (stats->timer == NULL)
1147 if (stats->enabled == FALSE)
1150 g_timer_stop(stats->timer);
1152 seconds = g_timer_elapsed(stats->timer, NULL);
1153 stats->data.time = stats->data_last.time + seconds;
1155 stats->enabled = FALSE;
1158 static void reset_stats(struct connman_service *service)
1160 DBG("service %p", service);
1163 service->stats.valid = FALSE;
1165 service->stats.data.rx_packets = 0;
1166 service->stats.data.tx_packets = 0;
1167 service->stats.data.rx_bytes = 0;
1168 service->stats.data.tx_bytes = 0;
1169 service->stats.data.rx_errors = 0;
1170 service->stats.data.tx_errors = 0;
1171 service->stats.data.rx_dropped = 0;
1172 service->stats.data.tx_dropped = 0;
1173 service->stats.data.time = 0;
1174 service->stats.data_last.time = 0;
1176 g_timer_reset(service->stats.timer);
1179 service->stats_roaming.valid = FALSE;
1181 service->stats_roaming.data.rx_packets = 0;
1182 service->stats_roaming.data.tx_packets = 0;
1183 service->stats_roaming.data.rx_bytes = 0;
1184 service->stats_roaming.data.tx_bytes = 0;
1185 service->stats_roaming.data.rx_errors = 0;
1186 service->stats_roaming.data.tx_errors = 0;
1187 service->stats_roaming.data.rx_dropped = 0;
1188 service->stats_roaming.data.tx_dropped = 0;
1189 service->stats_roaming.data.time = 0;
1190 service->stats_roaming.data_last.time = 0;
1192 g_timer_reset(service->stats_roaming.timer);
1195 static struct connman_service *get_default(void)
1197 struct connman_service *service;
1198 GSequenceIter *iter;
1200 iter = g_sequence_get_begin_iter(service_list);
1202 if (g_sequence_iter_is_end(iter) == TRUE)
1205 service = g_sequence_get(iter);
1207 if (is_connected(service) == FALSE)
1213 static void default_changed(void)
1215 struct connman_service *service = get_default();
1217 __connman_notifier_default_changed(service);
1220 const char *__connman_service_default(void)
1222 struct connman_service *service;
1224 service = get_default();
1225 if (service == NULL)
1228 return __connman_service_type2string(service->type);
1231 static void state_changed(struct connman_service *service)
1235 __connman_notifier_service_state_changed(service, service->state);
1237 str = state2string(service->state);
1241 connman_dbus_property_changed_basic(service->path,
1242 CONNMAN_SERVICE_INTERFACE, "State",
1243 DBUS_TYPE_STRING, &str);
1246 static void strength_changed(struct connman_service *service)
1248 if (service->strength == 0)
1251 connman_dbus_property_changed_basic(service->path,
1252 CONNMAN_SERVICE_INTERFACE, "Strength",
1253 DBUS_TYPE_BYTE, &service->strength);
1256 static void favorite_changed(struct connman_service *service)
1258 if (service->path == NULL)
1261 connman_dbus_property_changed_basic(service->path,
1262 CONNMAN_SERVICE_INTERFACE, "Favorite",
1263 DBUS_TYPE_BOOLEAN, &service->favorite);
1266 static void immutable_changed(struct connman_service *service)
1268 if (service->path == NULL)
1271 connman_dbus_property_changed_basic(service->path,
1272 CONNMAN_SERVICE_INTERFACE, "Immutable",
1273 DBUS_TYPE_BOOLEAN, &service->immutable);
1276 static void roaming_changed(struct connman_service *service)
1278 if (service->path == NULL)
1281 connman_dbus_property_changed_basic(service->path,
1282 CONNMAN_SERVICE_INTERFACE, "Roaming",
1283 DBUS_TYPE_BOOLEAN, &service->roaming);
1286 static void autoconnect_changed(struct connman_service *service)
1288 if (service->path == NULL)
1291 connman_dbus_property_changed_basic(service->path,
1292 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
1293 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1296 static void passphrase_changed(struct connman_service *service)
1298 dbus_bool_t required;
1300 switch (service->type) {
1301 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1302 case CONNMAN_SERVICE_TYPE_SYSTEM:
1303 case CONNMAN_SERVICE_TYPE_ETHERNET:
1304 case CONNMAN_SERVICE_TYPE_WIMAX:
1305 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1306 case CONNMAN_SERVICE_TYPE_CELLULAR:
1307 case CONNMAN_SERVICE_TYPE_GPS:
1308 case CONNMAN_SERVICE_TYPE_VPN:
1309 case CONNMAN_SERVICE_TYPE_GADGET:
1311 case CONNMAN_SERVICE_TYPE_WIFI:
1314 switch (service->security) {
1315 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1316 case CONNMAN_SERVICE_SECURITY_NONE:
1318 case CONNMAN_SERVICE_SECURITY_WEP:
1319 case CONNMAN_SERVICE_SECURITY_PSK:
1320 case CONNMAN_SERVICE_SECURITY_WPA:
1321 case CONNMAN_SERVICE_SECURITY_RSN:
1322 if (service->passphrase == NULL)
1325 case CONNMAN_SERVICE_SECURITY_8021X:
1331 connman_dbus_property_changed_basic(service->path,
1332 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
1333 DBUS_TYPE_BOOLEAN, &required);
1336 static void login_changed(struct connman_service *service)
1338 dbus_bool_t required = service->login_required;
1340 if (service->path == NULL)
1343 connman_dbus_property_changed_basic(service->path,
1344 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
1345 DBUS_TYPE_BOOLEAN, &required);
1348 static void append_security(DBusMessageIter *iter, void *user_data)
1350 struct connman_service *service = user_data;
1353 str = security2string(service->security);
1355 dbus_message_iter_append_basic(iter,
1356 DBUS_TYPE_STRING, &str);
1359 * Some access points incorrectly advertise WPS even when they
1360 * are configured as open or no security, so filter
1363 if (service->wps == TRUE) {
1364 switch (service->security) {
1365 case CONNMAN_SERVICE_SECURITY_PSK:
1366 case CONNMAN_SERVICE_SECURITY_WPA:
1367 case CONNMAN_SERVICE_SECURITY_RSN:
1369 dbus_message_iter_append_basic(iter,
1370 DBUS_TYPE_STRING, &str);
1372 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1373 case CONNMAN_SERVICE_SECURITY_NONE:
1374 case CONNMAN_SERVICE_SECURITY_WEP:
1375 case CONNMAN_SERVICE_SECURITY_8021X:
1381 static void append_ethernet(DBusMessageIter *iter, void *user_data)
1383 struct connman_service *service = user_data;
1385 if (service->ipconfig_ipv4 != NULL)
1386 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
1388 else if (service->ipconfig_ipv6 != NULL)
1389 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
1393 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1395 struct connman_service *service = user_data;
1397 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1398 state2string(service->state_ipv4));
1400 if (is_connected_state(service, service->state_ipv4) == FALSE)
1403 if (service->ipconfig_ipv4 != NULL)
1404 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1407 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1409 struct connman_service *service = user_data;
1411 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1412 state2string(service->state_ipv6));
1414 if (is_connected_state(service, service->state_ipv6) == FALSE)
1417 if (service->ipconfig_ipv6 != NULL)
1418 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1419 service->ipconfig_ipv4);
1422 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1424 struct connman_service *service = user_data;
1426 if (service->ipconfig_ipv4 != NULL)
1427 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1431 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1433 struct connman_service *service = user_data;
1435 if (service->ipconfig_ipv6 != NULL)
1436 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1440 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1445 servers = *nameservers;
1447 for (i = 0; servers[i] != NULL; i++) {
1448 dbus_message_iter_append_basic(iter,
1449 DBUS_TYPE_STRING, &servers[i]);
1453 static void append_dns(DBusMessageIter *iter, void *user_data)
1455 struct connman_service *service = user_data;
1457 if (is_connected(service) == FALSE)
1460 if (service->nameservers_config != NULL) {
1461 append_nameserver(iter, &service->nameservers_config);
1464 if (service->nameservers != NULL)
1465 append_nameserver(iter, &service->nameservers);
1467 if (service->nameservers_auto != NULL)
1468 append_nameserver(iter, &service->nameservers_auto);
1472 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1474 struct connman_service *service = user_data;
1477 if (service->nameservers_config == NULL)
1480 for (i = 0; service->nameservers_config[i]; i++) {
1481 dbus_message_iter_append_basic(iter,
1483 &service->nameservers_config[i]);
1487 static void append_domain(DBusMessageIter *iter, void *user_data)
1489 struct connman_service *service = user_data;
1491 if (is_connected(service) == FALSE &&
1492 is_connecting(service) == FALSE)
1495 if (service->domainname == NULL)
1498 dbus_message_iter_append_basic(iter,
1499 DBUS_TYPE_STRING, &service->domainname);
1502 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1504 struct connman_service *service = user_data;
1507 if (service->domains == NULL)
1510 for (i = 0; service->domains[i]; i++)
1511 dbus_message_iter_append_basic(iter,
1512 DBUS_TYPE_STRING, &service->domains[i]);
1515 static void append_proxies(DBusMessageIter *iter, void *user_data)
1517 struct connman_service *service = user_data;
1520 if (service->proxies == NULL)
1523 for (i = 0; service->proxies[i]; i++)
1524 dbus_message_iter_append_basic(iter,
1525 DBUS_TYPE_STRING, &service->proxies[i]);
1528 static void append_excludes(DBusMessageIter *iter, void *user_data)
1530 struct connman_service *service = user_data;
1533 if (service->excludes == NULL)
1536 for (i = 0; service->excludes[i]; i++)
1537 dbus_message_iter_append_basic(iter,
1538 DBUS_TYPE_STRING, &service->excludes[i]);
1541 static void append_proxy(DBusMessageIter *iter, void *user_data)
1543 struct connman_service *service = user_data;
1544 enum connman_service_proxy_method proxy;
1545 const char *pac = NULL;
1546 const char *method = proxymethod2string(
1547 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1551 if (is_connected(service) == FALSE)
1554 proxy = connman_service_get_proxy_method(service);
1557 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1559 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1561 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1562 connman_dbus_dict_append_array(iter, "Servers",
1563 DBUS_TYPE_STRING, append_proxies,
1566 connman_dbus_dict_append_array(iter, "Excludes",
1567 DBUS_TYPE_STRING, append_excludes,
1570 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1571 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1572 if (service->ipconfig_ipv4 != NULL)
1573 pac = __connman_ipconfig_get_proxy_autoconfig(
1574 service->ipconfig_ipv4);
1575 else if (service->ipconfig_ipv6 != NULL)
1576 pac = __connman_ipconfig_get_proxy_autoconfig(
1577 service->ipconfig_ipv6);
1579 if (service->pac == NULL && pac == NULL)
1582 if (service->pac != NULL)
1585 connman_dbus_dict_append_basic(iter, "URL",
1586 DBUS_TYPE_STRING, &pac);
1590 method = proxymethod2string(proxy);
1593 connman_dbus_dict_append_basic(iter, "Method",
1594 DBUS_TYPE_STRING, &method);
1597 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1599 struct connman_service *service = user_data;
1602 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1605 switch (service->proxy_config) {
1606 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1608 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1610 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1611 if (service->proxies != NULL)
1612 connman_dbus_dict_append_array(iter, "Servers",
1614 append_proxies, service);
1616 if (service->excludes != NULL)
1617 connman_dbus_dict_append_array(iter, "Excludes",
1619 append_excludes, service);
1621 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1622 if (service->pac != NULL)
1623 connman_dbus_dict_append_basic(iter, "URL",
1624 DBUS_TYPE_STRING, &service->pac);
1628 method = proxymethod2string(service->proxy_config);
1630 connman_dbus_dict_append_basic(iter, "Method",
1631 DBUS_TYPE_STRING, &method);
1634 static void append_provider(DBusMessageIter *iter, void *user_data)
1636 struct connman_service *service = user_data;
1638 DBG("%p %p", service, service->provider);
1640 if (is_connected(service) == FALSE)
1643 if (service->provider != NULL)
1644 __connman_provider_append_properties(service->provider, iter);
1648 static void settings_changed(struct connman_service *service,
1649 struct connman_ipconfig *ipconfig)
1651 connman_dbus_property_changed_dict(service->path,
1652 CONNMAN_SERVICE_INTERFACE, "IPv4",
1653 append_ipv4, service);
1655 connman_dbus_property_changed_dict(service->path,
1656 CONNMAN_SERVICE_INTERFACE, "IPv6",
1657 append_ipv6, service);
1659 __connman_notifier_ipconfig_changed(service, ipconfig);
1662 static void ipv4_configuration_changed(struct connman_service *service)
1664 connman_dbus_property_changed_dict(service->path,
1665 CONNMAN_SERVICE_INTERFACE,
1666 "IPv4.Configuration",
1671 static void ipv6_configuration_changed(struct connman_service *service)
1673 connman_dbus_property_changed_dict(service->path,
1674 CONNMAN_SERVICE_INTERFACE,
1675 "IPv6.Configuration",
1680 static void dns_changed(struct connman_service *service)
1682 connman_dbus_property_changed_array(service->path,
1683 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1684 DBUS_TYPE_STRING, append_dns, service);
1687 static void dns_configuration_changed(struct connman_service *service)
1689 connman_dbus_property_changed_array(service->path,
1690 CONNMAN_SERVICE_INTERFACE,
1691 "Nameservers.Configuration",
1692 DBUS_TYPE_STRING, append_dnsconfig, service);
1694 dns_changed(service);
1697 static void domain_changed(struct connman_service *service)
1699 connman_dbus_property_changed_array(service->path,
1700 CONNMAN_SERVICE_INTERFACE, "Domains",
1701 DBUS_TYPE_STRING, append_domain, service);
1704 static void domain_configuration_changed(struct connman_service *service)
1706 connman_dbus_property_changed_array(service->path,
1707 CONNMAN_SERVICE_INTERFACE,
1708 "Domains.Configuration",
1709 DBUS_TYPE_STRING, append_domainconfig, service);
1712 static void proxy_changed(struct connman_service *service)
1714 connman_dbus_property_changed_dict(service->path,
1715 CONNMAN_SERVICE_INTERFACE, "Proxy",
1716 append_proxy, service);
1719 static void proxy_configuration_changed(struct connman_service *service)
1721 connman_dbus_property_changed_dict(service->path,
1722 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1723 append_proxyconfig, service);
1725 proxy_changed(service);
1728 static void link_changed(struct connman_service *service)
1730 connman_dbus_property_changed_dict(service->path,
1731 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1732 append_ethernet, service);
1735 static void stats_append_counters(DBusMessageIter *dict,
1736 struct connman_stats_data *stats,
1737 struct connman_stats_data *counters,
1738 connman_bool_t append_all)
1740 if (counters->rx_packets != stats->rx_packets || append_all) {
1741 counters->rx_packets = stats->rx_packets;
1742 connman_dbus_dict_append_basic(dict, "RX.Packets",
1743 DBUS_TYPE_UINT32, &stats->rx_packets);
1746 if (counters->tx_packets != stats->tx_packets || append_all) {
1747 counters->tx_packets = stats->tx_packets;
1748 connman_dbus_dict_append_basic(dict, "TX.Packets",
1749 DBUS_TYPE_UINT32, &stats->tx_packets);
1752 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1753 counters->rx_bytes = stats->rx_bytes;
1754 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1755 DBUS_TYPE_UINT32, &stats->rx_bytes);
1758 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1759 counters->tx_bytes = stats->tx_bytes;
1760 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1761 DBUS_TYPE_UINT32, &stats->tx_bytes);
1764 if (counters->rx_errors != stats->rx_errors || append_all) {
1765 counters->rx_errors = stats->rx_errors;
1766 connman_dbus_dict_append_basic(dict, "RX.Errors",
1767 DBUS_TYPE_UINT32, &stats->rx_errors);
1770 if (counters->tx_errors != stats->tx_errors || append_all) {
1771 counters->tx_errors = stats->tx_errors;
1772 connman_dbus_dict_append_basic(dict, "TX.Errors",
1773 DBUS_TYPE_UINT32, &stats->tx_errors);
1776 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1777 counters->rx_dropped = stats->rx_dropped;
1778 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1779 DBUS_TYPE_UINT32, &stats->rx_dropped);
1782 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1783 counters->tx_dropped = stats->tx_dropped;
1784 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1785 DBUS_TYPE_UINT32, &stats->tx_dropped);
1788 if (counters->time != stats->time || append_all) {
1789 counters->time = stats->time;
1790 connman_dbus_dict_append_basic(dict, "Time",
1791 DBUS_TYPE_UINT32, &stats->time);
1795 static void stats_append(struct connman_service *service,
1796 const char *counter,
1797 struct connman_stats_counter *counters,
1798 connman_bool_t append_all)
1800 DBusMessageIter array, dict;
1803 DBG("service %p counter %s", service, counter);
1805 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1809 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1810 &service->path, DBUS_TYPE_INVALID);
1812 dbus_message_iter_init_append(msg, &array);
1815 connman_dbus_dict_open(&array, &dict);
1817 stats_append_counters(&dict, &service->stats.data,
1818 &counters->stats.data, append_all);
1820 connman_dbus_dict_close(&array, &dict);
1822 /* roaming counter */
1823 connman_dbus_dict_open(&array, &dict);
1825 stats_append_counters(&dict, &service->stats_roaming.data,
1826 &counters->stats_roaming.data, append_all);
1828 connman_dbus_dict_close(&array, &dict);
1830 __connman_counter_send_usage(counter, msg);
1833 static void stats_update(struct connman_service *service,
1834 unsigned int rx_packets, unsigned int tx_packets,
1835 unsigned int rx_bytes, unsigned int tx_bytes,
1836 unsigned int rx_errors, unsigned int tx_errors,
1837 unsigned int rx_dropped, unsigned int tx_dropped)
1839 struct connman_stats *stats = stats_get(service);
1840 struct connman_stats_data *data_last = &stats->data_last;
1841 struct connman_stats_data *data = &stats->data;
1842 unsigned int seconds;
1844 DBG("service %p", service);
1846 if (stats->valid == TRUE) {
1848 rx_packets - data_last->rx_packets;
1850 tx_packets - data_last->tx_packets;
1852 rx_bytes - data_last->rx_bytes;
1854 tx_bytes - data_last->tx_bytes;
1856 rx_errors - data_last->rx_errors;
1858 tx_errors - data_last->tx_errors;
1860 rx_dropped - data_last->rx_dropped;
1862 tx_dropped - data_last->tx_dropped;
1864 stats->valid = TRUE;
1867 data_last->rx_packets = rx_packets;
1868 data_last->tx_packets = tx_packets;
1869 data_last->rx_bytes = rx_bytes;
1870 data_last->tx_bytes = tx_bytes;
1871 data_last->rx_errors = rx_errors;
1872 data_last->tx_errors = tx_errors;
1873 data_last->rx_dropped = rx_dropped;
1874 data_last->tx_dropped = tx_dropped;
1876 seconds = g_timer_elapsed(stats->timer, NULL);
1877 stats->data.time = stats->data_last.time + seconds;
1880 static char *wifi_build_group_name(const unsigned char *ssid,
1881 unsigned int ssid_len,
1883 const char *security)
1888 /* the last 3 is for the 2 '_' and '\0' */
1889 str = g_string_sized_new((ssid_len * 2) + strlen(mode)
1890 + strlen(security) + 3);
1894 for (i = 0; i < ssid_len; i++)
1895 g_string_append_printf(str, "%02x", ssid[i]);
1897 g_string_append_printf(str, "_%s_%s", mode, security);
1899 return g_string_free(str, FALSE);
1902 void __connman_service_notify(struct connman_service *service,
1903 unsigned int rx_packets, unsigned int tx_packets,
1904 unsigned int rx_bytes, unsigned int tx_bytes,
1905 unsigned int rx_errors, unsigned int tx_errors,
1906 unsigned int rx_dropped, unsigned int tx_dropped)
1908 GHashTableIter iter;
1909 gpointer key, value;
1910 const char *counter;
1911 struct connman_stats_counter *counters;
1912 struct connman_stats_data *data;
1915 if (service == NULL)
1918 if (is_connected(service) == FALSE)
1921 stats_update(service,
1922 rx_packets, tx_packets,
1924 rx_errors, tx_errors,
1925 rx_dropped, tx_dropped);
1927 data = &stats_get(service)->data;
1928 err = __connman_stats_update(service, service->roaming, data);
1930 connman_error("Failed to store statistics for %s",
1931 service->identifier);
1933 g_hash_table_iter_init(&iter, service->counter_table);
1934 while (g_hash_table_iter_next(&iter, &key, &value)) {
1938 stats_append(service, counter, counters, counters->append_all);
1939 counters->append_all = FALSE;
1943 int __connman_service_counter_register(const char *counter)
1945 struct connman_service *service;
1946 GSequenceIter *iter;
1947 struct connman_stats_counter *counters;
1949 DBG("counter %s", counter);
1951 counter_list = g_slist_append(counter_list, (gpointer)counter);
1953 iter = g_sequence_get_begin_iter(service_list);
1955 while (g_sequence_iter_is_end(iter) == FALSE) {
1956 service = g_sequence_get(iter);
1958 counters = g_try_new0(struct connman_stats_counter, 1);
1959 if (counters == NULL)
1962 counters->append_all = TRUE;
1964 g_hash_table_replace(service->counter_table, (gpointer)counter,
1967 iter = g_sequence_iter_next(iter);
1973 void __connman_service_counter_unregister(const char *counter)
1975 struct connman_service *service;
1976 GSequenceIter *iter;
1978 DBG("counter %s", counter);
1980 iter = g_sequence_get_begin_iter(service_list);
1982 while (g_sequence_iter_is_end(iter) == FALSE) {
1983 service = g_sequence_get(iter);
1985 g_hash_table_remove(service->counter_table, counter);
1987 iter = g_sequence_iter_next(iter);
1990 counter_list = g_slist_remove(counter_list, counter);
1993 GSequence *__connman_service_get_list(struct connman_session *session,
1994 service_match_cb service_match,
1995 create_service_entry_cb create_service_entry,
1996 GDestroyNotify destroy_service_entry)
1999 GSequenceIter *iter;
2000 struct connman_service *service;
2001 struct service_entry *entry;
2003 list = g_sequence_new(destroy_service_entry);
2007 iter = g_sequence_get_begin_iter(service_list);
2009 while (g_sequence_iter_is_end(iter) == FALSE) {
2010 service = g_sequence_get(iter);
2012 if (service_match(session, service) == TRUE) {
2013 entry = create_service_entry(service, service->name,
2018 g_sequence_append(list, entry);
2021 iter = g_sequence_iter_next(iter);
2027 void __connman_service_session_inc(struct connman_service *service)
2029 DBG("service %p ref count %d", service,
2030 service->session_usage_count + 1);
2032 __sync_fetch_and_add(&service->session_usage_count, 1);
2035 connman_bool_t __connman_service_session_dec(struct connman_service *service)
2037 DBG("service %p ref count %d", service,
2038 service->session_usage_count - 1);
2040 if (__sync_fetch_and_sub(&service->session_usage_count, 1) != 1)
2046 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
2047 struct connman_service *service)
2049 dbus_bool_t required;
2052 str = __connman_service_type2string(service->type);
2054 connman_dbus_dict_append_basic(dict, "Type",
2055 DBUS_TYPE_STRING, &str);
2057 connman_dbus_dict_append_array(dict, "Security",
2058 DBUS_TYPE_STRING, append_security, service);
2060 str = state2string(service->state);
2062 connman_dbus_dict_append_basic(dict, "State",
2063 DBUS_TYPE_STRING, &str);
2065 str = error2string(service->error);
2067 connman_dbus_dict_append_basic(dict, "Error",
2068 DBUS_TYPE_STRING, &str);
2070 if (service->strength > 0)
2071 connman_dbus_dict_append_basic(dict, "Strength",
2072 DBUS_TYPE_BYTE, &service->strength);
2074 connman_dbus_dict_append_basic(dict, "Favorite",
2075 DBUS_TYPE_BOOLEAN, &service->favorite);
2077 connman_dbus_dict_append_basic(dict, "Immutable",
2078 DBUS_TYPE_BOOLEAN, &service->immutable);
2080 if (service->favorite == TRUE)
2081 connman_dbus_dict_append_basic(dict, "AutoConnect",
2082 DBUS_TYPE_BOOLEAN, &service->autoconnect);
2084 connman_dbus_dict_append_basic(dict, "AutoConnect",
2085 DBUS_TYPE_BOOLEAN, &service->favorite);
2087 if (service->name != NULL)
2088 connman_dbus_dict_append_basic(dict, "Name",
2089 DBUS_TYPE_STRING, &service->name);
2091 connman_dbus_dict_append_basic(dict, "LoginRequired",
2092 DBUS_TYPE_BOOLEAN, &service->login_required);
2094 switch (service->type) {
2095 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2096 case CONNMAN_SERVICE_TYPE_SYSTEM:
2097 case CONNMAN_SERVICE_TYPE_GPS:
2098 case CONNMAN_SERVICE_TYPE_VPN:
2099 case CONNMAN_SERVICE_TYPE_GADGET:
2101 case CONNMAN_SERVICE_TYPE_CELLULAR:
2102 connman_dbus_dict_append_basic(dict, "Roaming",
2103 DBUS_TYPE_BOOLEAN, &service->roaming);
2105 connman_dbus_dict_append_dict(dict, "Ethernet",
2106 append_ethernet, service);
2108 case CONNMAN_SERVICE_TYPE_WIFI:
2109 if (service->passphrase != NULL && limited == FALSE)
2110 connman_dbus_dict_append_basic(dict, "Passphrase",
2111 DBUS_TYPE_STRING, &service->passphrase);
2115 switch (service->security) {
2116 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2117 case CONNMAN_SERVICE_SECURITY_NONE:
2119 case CONNMAN_SERVICE_SECURITY_WEP:
2120 case CONNMAN_SERVICE_SECURITY_PSK:
2121 case CONNMAN_SERVICE_SECURITY_WPA:
2122 case CONNMAN_SERVICE_SECURITY_RSN:
2123 if (service->passphrase == NULL)
2126 case CONNMAN_SERVICE_SECURITY_8021X:
2130 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
2131 DBUS_TYPE_BOOLEAN, &required);
2134 case CONNMAN_SERVICE_TYPE_ETHERNET:
2135 case CONNMAN_SERVICE_TYPE_WIMAX:
2136 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2137 connman_dbus_dict_append_dict(dict, "Ethernet",
2138 append_ethernet, service);
2142 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
2144 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
2145 append_ipv4config, service);
2147 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
2149 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
2150 append_ipv6config, service);
2152 connman_dbus_dict_append_array(dict, "Nameservers",
2153 DBUS_TYPE_STRING, append_dns, service);
2155 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
2156 DBUS_TYPE_STRING, append_dnsconfig, service);
2158 connman_dbus_dict_append_array(dict, "Domains",
2159 DBUS_TYPE_STRING, append_domain, service);
2161 connman_dbus_dict_append_array(dict, "Domains.Configuration",
2162 DBUS_TYPE_STRING, append_domainconfig, service);
2164 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
2166 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
2167 append_proxyconfig, service);
2169 connman_dbus_dict_append_dict(dict, "Provider",
2170 append_provider, service);
2173 static void append_struct(gpointer value, gpointer user_data)
2175 struct connman_service *service = value;
2176 DBusMessageIter *iter = user_data;
2177 DBusMessageIter entry, dict;
2179 if (service->path == NULL || service->hidden == TRUE)
2182 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
2184 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2187 connman_dbus_dict_open(&entry, &dict);
2188 append_properties(&dict, TRUE, service);
2189 connman_dbus_dict_close(&entry, &dict);
2191 dbus_message_iter_close_container(iter, &entry);
2194 void __connman_service_list_struct(DBusMessageIter *iter)
2196 g_sequence_foreach(service_list, append_struct, iter);
2199 int __connman_service_get_index(struct connman_service *service)
2201 if (service == NULL)
2204 if (service->network != NULL)
2205 return connman_network_get_index(service->network);
2206 else if (service->provider != NULL)
2207 return connman_provider_get_index(service->provider);
2212 void __connman_service_set_domainname(struct connman_service *service,
2213 const char *domainname)
2215 if (service == NULL)
2218 g_free(service->domainname);
2219 service->domainname = g_strdup(domainname);
2221 domain_changed(service);
2224 const char *connman_service_get_domainname(struct connman_service *service)
2226 if (service == NULL)
2229 if (service->domains != NULL)
2230 return service->domains[0];
2232 return service->domainname;
2235 char **connman_service_get_nameservers(struct connman_service *service)
2237 if (service == NULL)
2240 if (service->nameservers_config != NULL)
2241 return service->nameservers_config;
2242 else if (service->nameservers != NULL)
2243 return service->nameservers;
2248 void connman_service_set_proxy_method(struct connman_service *service,
2249 enum connman_service_proxy_method method)
2251 if (service == NULL)
2254 service->proxy = method;
2256 proxy_changed(service);
2258 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
2259 __connman_notifier_proxy_changed(service);
2262 enum connman_service_proxy_method connman_service_get_proxy_method(
2263 struct connman_service *service)
2265 if (service == NULL)
2266 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2268 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
2269 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
2270 service->pac == NULL)
2271 return service->proxy;
2273 return service->proxy_config;
2276 return service->proxy;
2279 char **connman_service_get_proxy_servers(struct connman_service *service)
2281 return g_strdupv(service->proxies);
2284 char **connman_service_get_proxy_excludes(struct connman_service *service)
2286 return g_strdupv(service->excludes);
2289 const char *connman_service_get_proxy_url(struct connman_service *service)
2291 if (service == NULL)
2294 return service->pac;
2297 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
2300 if (service == NULL)
2303 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
2305 if (service->ipconfig_ipv4) {
2306 if (__connman_ipconfig_set_proxy_autoconfig(
2307 service->ipconfig_ipv4, url) < 0)
2309 } else if (service->ipconfig_ipv6) {
2310 if (__connman_ipconfig_set_proxy_autoconfig(
2311 service->ipconfig_ipv6, url) < 0)
2316 proxy_changed(service);
2318 __connman_notifier_proxy_changed(service);
2321 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
2323 if (service == NULL)
2326 if (service->ipconfig_ipv4)
2327 return __connman_ipconfig_get_proxy_autoconfig(
2328 service->ipconfig_ipv4);
2329 else if (service->ipconfig_ipv6)
2330 return __connman_ipconfig_get_proxy_autoconfig(
2331 service->ipconfig_ipv6);
2335 static void update_timeservers(struct connman_service *service)
2339 if (service->timeservers == NULL)
2342 switch (service->state) {
2343 case CONNMAN_SERVICE_STATE_UNKNOWN:
2344 case CONNMAN_SERVICE_STATE_IDLE:
2345 case CONNMAN_SERVICE_STATE_ASSOCIATION:
2346 case CONNMAN_SERVICE_STATE_CONFIGURATION:
2348 case CONNMAN_SERVICE_STATE_FAILURE:
2349 case CONNMAN_SERVICE_STATE_DISCONNECT:
2350 for (i = 0; service->timeservers[i] != NULL; i++)
2351 connman_timeserver_remove(service->timeservers[i]);
2353 case CONNMAN_SERVICE_STATE_READY:
2354 case CONNMAN_SERVICE_STATE_ONLINE:
2358 for (i = 0; service->timeservers[i] != NULL; i++)
2359 connman_timeserver_append(service->timeservers[i]);
2362 int __connman_service_timeserver_append(struct connman_service *service,
2363 const char *timeserver)
2367 DBG("service %p timeserver %s", service, timeserver);
2369 if (timeserver == NULL)
2372 if (service->timeservers != NULL) {
2375 for (i = 0; service->timeservers[i] != NULL; i++)
2376 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2379 len = g_strv_length(service->timeservers);
2380 service->timeservers = g_try_renew(char *, service->timeservers,
2384 service->timeservers = g_try_new0(char *, len + 2);
2387 if (service->timeservers == NULL)
2390 service->timeservers[len] = g_strdup(timeserver);
2391 service->timeservers[len + 1] = NULL;
2393 update_timeservers(service);
2398 int __connman_service_timeserver_remove(struct connman_service *service,
2399 const char *timeserver)
2404 DBG("service %p timeserver %s", service, timeserver);
2406 if (timeserver == NULL)
2409 if (service->timeservers == NULL)
2412 len = g_strv_length(service->timeservers);
2414 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
2417 g_strfreev(service->timeservers);
2418 service->timeservers = NULL;
2423 servers = g_try_new0(char *, len - 1);
2424 if (servers == NULL)
2427 for (i = 0, j = 0; i < len; i++) {
2428 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2429 servers[j] = g_strdup(service->timeservers[i]);
2433 servers[len - 2] = NULL;
2435 g_strfreev(service->timeservers);
2436 service->timeservers = servers;
2438 update_timeservers(service);
2443 void __connman_service_set_pac(struct connman_service *service,
2446 g_free(service->pac);
2447 service->pac = g_strdup(pac);
2449 proxy_changed(service);
2452 void __connman_service_set_identity(struct connman_service *service,
2453 const char *identity)
2455 if (service->immutable)
2458 g_free(service->identity);
2459 service->identity = g_strdup(identity);
2461 if (service->network != NULL)
2462 connman_network_set_string(service->network,
2467 void __connman_service_set_agent_identity(struct connman_service *service,
2468 const char *agent_identity)
2470 g_free(service->agent_identity);
2471 service->agent_identity = g_strdup(agent_identity);
2473 if (service->network != NULL)
2474 connman_network_set_string(service->network,
2475 "WiFi.AgentIdentity",
2476 service->agent_identity);
2479 void __connman_service_set_passphrase(struct connman_service *service,
2480 const char* passphrase)
2482 if (service->immutable == TRUE)
2485 g_free(service->passphrase);
2486 service->passphrase = g_strdup(passphrase);
2488 passphrase_changed(service);
2490 if (service->network != NULL)
2491 connman_network_set_string(service->network,
2493 service->passphrase);
2495 service_save(service);
2498 void __connman_service_set_agent_passphrase(struct connman_service *service,
2499 const char *agent_passphrase)
2501 g_free(service->agent_passphrase);
2502 service->agent_passphrase = g_strdup(agent_passphrase);
2504 if (service->network != NULL)
2505 connman_network_set_string(service->network,
2506 "WiFi.AgentPassphrase",
2507 service->agent_passphrase);
2510 static DBusMessage *get_properties(DBusConnection *conn,
2511 DBusMessage *msg, void *user_data)
2513 struct connman_service *service = user_data;
2515 DBusMessageIter array, dict;
2517 DBG("service %p", service);
2519 reply = dbus_message_new_method_return(msg);
2523 dbus_message_iter_init_append(reply, &array);
2525 connman_dbus_dict_open(&array, &dict);
2526 append_properties(&dict, FALSE, service);
2527 connman_dbus_dict_close(&array, &dict);
2532 static int update_proxy_configuration(struct connman_service *service,
2533 DBusMessageIter *array)
2535 DBusMessageIter dict;
2536 enum connman_service_proxy_method method;
2537 GString *servers_str = NULL;
2538 GString *excludes_str = NULL;
2539 const char *url = NULL;
2541 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2543 dbus_message_iter_recurse(array, &dict);
2545 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2546 DBusMessageIter entry, variant;
2550 dbus_message_iter_recurse(&dict, &entry);
2552 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2555 dbus_message_iter_get_basic(&entry, &key);
2556 dbus_message_iter_next(&entry);
2558 if (dbus_message_iter_get_arg_type(&entry) !=
2562 dbus_message_iter_recurse(&entry, &variant);
2563 type = dbus_message_iter_get_arg_type(&variant);
2565 if (g_str_equal(key, "Method") == TRUE) {
2568 if (type != DBUS_TYPE_STRING)
2571 dbus_message_iter_get_basic(&variant, &val);
2572 method = string2proxymethod(val);
2573 } else if (g_str_equal(key, "URL") == TRUE) {
2574 if (type != DBUS_TYPE_STRING)
2577 dbus_message_iter_get_basic(&variant, &url);
2578 } else if (g_str_equal(key, "Servers") == TRUE) {
2579 DBusMessageIter str_array;
2581 if (type != DBUS_TYPE_ARRAY)
2584 servers_str = g_string_new(NULL);
2585 if (servers_str == NULL)
2588 dbus_message_iter_recurse(&variant, &str_array);
2590 while (dbus_message_iter_get_arg_type(&str_array) ==
2594 dbus_message_iter_get_basic(&str_array, &val);
2596 if (servers_str->len > 0)
2597 g_string_append_printf(servers_str,
2600 g_string_append(servers_str, val);
2602 dbus_message_iter_next(&str_array);
2604 } else if (g_str_equal(key, "Excludes") == TRUE) {
2605 DBusMessageIter str_array;
2607 if (type != DBUS_TYPE_ARRAY)
2610 excludes_str = g_string_new(NULL);
2611 if (excludes_str == NULL)
2614 dbus_message_iter_recurse(&variant, &str_array);
2616 while (dbus_message_iter_get_arg_type(&str_array) ==
2620 dbus_message_iter_get_basic(&str_array, &val);
2622 if (excludes_str->len > 0)
2623 g_string_append_printf(excludes_str,
2626 g_string_append(excludes_str, val);
2628 dbus_message_iter_next(&str_array);
2632 dbus_message_iter_next(&dict);
2636 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2638 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2639 if (servers_str == NULL && service->proxies == NULL)
2642 if (servers_str != NULL) {
2643 g_strfreev(service->proxies);
2645 if (servers_str->len > 0)
2646 service->proxies = g_strsplit_set(
2647 servers_str->str, " ", 0);
2649 service->proxies = NULL;
2652 if (excludes_str != NULL) {
2653 g_strfreev(service->excludes);
2655 if (excludes_str->len > 0)
2656 service->excludes = g_strsplit_set(
2657 excludes_str->str, " ", 0);
2659 service->excludes = NULL;
2662 if (service->proxies == NULL)
2663 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2666 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2667 g_free(service->pac);
2669 if (url != NULL && strlen(url) > 0)
2670 service->pac = g_strdup(url);
2672 service->pac = NULL;
2674 /* if we are connected:
2675 - if service->pac == NULL
2676 - if __connman_ipconfig_get_proxy_autoconfig(
2677 service->ipconfig) == NULL
2678 --> We should start WPAD */
2681 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2685 if (servers_str != NULL)
2686 g_string_free(servers_str, TRUE);
2688 if (excludes_str != NULL)
2689 g_string_free(excludes_str, TRUE);
2691 service->proxy_config = method;
2696 if (servers_str != NULL)
2697 g_string_free(servers_str, TRUE);
2699 if (excludes_str != NULL)
2700 g_string_free(excludes_str, TRUE);
2705 static int set_ipconfig(struct connman_service *service,
2706 struct connman_ipconfig *ipconfig,
2707 DBusMessageIter *array,
2708 enum connman_service_state state,
2709 enum connman_service_state *new_state)
2711 enum connman_ipconfig_method old_method;
2712 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2713 enum connman_ipconfig_type type;
2716 if (ipconfig == NULL)
2719 old_method = __connman_ipconfig_get_method(ipconfig);
2721 if (is_connecting_state(service, state) ||
2722 is_connected_state(service, state))
2723 __connman_network_clear_ipconfig(service->network, ipconfig);
2725 err = __connman_ipconfig_set_config(ipconfig, array);
2726 method = __connman_ipconfig_get_method(ipconfig);
2727 type = __connman_ipconfig_get_config_type(ipconfig);
2729 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2730 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2731 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2732 *new_state = service->state_ipv4 =
2733 CONNMAN_SERVICE_STATE_CONFIGURATION;
2734 __connman_ipconfig_enable(ipconfig);
2737 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2738 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2739 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2740 *new_state = service->state_ipv6;
2741 __connman_ipconfig_enable(ipconfig);
2745 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2746 type, method, state2string(*new_state));
2751 static DBusMessage *set_property(DBusConnection *conn,
2752 DBusMessage *msg, void *user_data)
2754 struct connman_service *service = user_data;
2755 DBusMessageIter iter, value;
2759 DBG("service %p", service);
2761 if (dbus_message_iter_init(msg, &iter) == FALSE)
2762 return __connman_error_invalid_arguments(msg);
2764 dbus_message_iter_get_basic(&iter, &name);
2765 dbus_message_iter_next(&iter);
2766 dbus_message_iter_recurse(&iter, &value);
2768 type = dbus_message_iter_get_arg_type(&value);
2770 if (g_str_equal(name, "AutoConnect") == TRUE) {
2771 connman_bool_t autoconnect;
2773 if (type != DBUS_TYPE_BOOLEAN)
2774 return __connman_error_invalid_arguments(msg);
2776 if (service->favorite == FALSE)
2777 return __connman_error_invalid_service(msg);
2779 dbus_message_iter_get_basic(&value, &autoconnect);
2781 if (service->autoconnect == autoconnect)
2782 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2784 service->autoconnect = autoconnect;
2786 autoconnect_changed(service);
2788 service_save(service);
2789 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2790 const char *passphrase;
2792 if (type != DBUS_TYPE_STRING)
2793 return __connman_error_invalid_arguments(msg);
2795 if (service->immutable == TRUE)
2796 return __connman_error_not_supported(msg);
2798 dbus_message_iter_get_basic(&value, &passphrase);
2800 __connman_service_set_passphrase(service, passphrase);
2801 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2802 DBusMessageIter entry;
2807 if (type != DBUS_TYPE_ARRAY)
2808 return __connman_error_invalid_arguments(msg);
2810 str = g_string_new(NULL);
2812 return __connman_error_invalid_arguments(msg);
2814 index = connman_network_get_index(service->network);
2815 gw = __connman_ipconfig_get_gateway_from_index(index);
2817 if (gw && strlen(gw))
2818 __connman_service_nameserver_del_routes(service);
2820 dbus_message_iter_recurse(&value, &entry);
2822 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2824 dbus_message_iter_get_basic(&entry, &val);
2825 dbus_message_iter_next(&entry);
2827 g_string_append_printf(str, " %s", val);
2829 g_string_append(str, val);
2832 g_strfreev(service->nameservers_config);
2835 service->nameservers_config =
2836 g_strsplit_set(str->str, " ", 0);
2838 service->nameservers_config = NULL;
2841 g_string_free(str, TRUE);
2843 if (gw && strlen(gw))
2844 __connman_service_nameserver_add_routes(service, gw);
2846 update_nameservers(service);
2847 dns_configuration_changed(service);
2849 service_save(service);
2850 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2851 DBusMessageIter entry;
2854 if (type != DBUS_TYPE_ARRAY)
2855 return __connman_error_invalid_arguments(msg);
2857 str = g_string_new(NULL);
2859 return __connman_error_invalid_arguments(msg);
2861 dbus_message_iter_recurse(&value, &entry);
2863 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2865 dbus_message_iter_get_basic(&entry, &val);
2866 dbus_message_iter_next(&entry);
2868 g_string_append_printf(str, " %s", val);
2870 g_string_append(str, val);
2873 g_strfreev(service->domains);
2876 service->domains = g_strsplit_set(str->str, " ", 0);
2878 service->domains = NULL;
2880 g_string_free(str, TRUE);
2882 update_nameservers(service);
2883 domain_configuration_changed(service);
2885 service_save(service);
2886 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2889 if (type != DBUS_TYPE_ARRAY)
2890 return __connman_error_invalid_arguments(msg);
2892 err = update_proxy_configuration(service, &value);
2895 return __connman_error_failed(msg, -err);
2897 proxy_configuration_changed(service);
2899 __connman_notifier_proxy_changed(service);
2901 service_save(service);
2902 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2903 g_str_equal(name, "IPv6.Configuration")) {
2905 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2906 enum connman_service_state state =
2907 CONNMAN_SERVICE_STATE_UNKNOWN;
2912 if (service->ipconfig_ipv4 == NULL &&
2913 service->ipconfig_ipv6 == NULL)
2914 return __connman_error_invalid_property(msg);
2916 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2917 ipv4 = service->ipconfig_ipv4;
2918 err = set_ipconfig(service, ipv4, &value,
2919 service->state_ipv4, &state);
2921 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2922 ipv6 = service->ipconfig_ipv6;
2923 err = set_ipconfig(service, ipv6, &value,
2924 service->state_ipv6, &state);
2928 if (is_connected_state(service, state) ||
2929 is_connecting_state(service, state))
2930 __connman_network_set_ipconfig(service->network,
2932 return __connman_error_failed(msg, -err);
2936 ipv4_configuration_changed(service);
2938 ipv6_configuration_changed(service);
2940 if (is_connecting(service) || is_connected(service))
2941 __connman_network_set_ipconfig(service->network,
2944 service_save(service);
2946 return __connman_error_invalid_property(msg);
2948 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2951 static void set_idle(struct connman_service *service)
2953 service->state = service->state_ipv4 = service->state_ipv6 =
2954 CONNMAN_SERVICE_STATE_IDLE;
2955 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2956 state_changed(service);
2959 static DBusMessage *clear_property(DBusConnection *conn,
2960 DBusMessage *msg, void *user_data)
2962 struct connman_service *service = user_data;
2965 DBG("service %p", service);
2967 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2970 if (g_str_equal(name, "Error") == TRUE) {
2973 g_get_current_time(&service->modified);
2974 service_save(service);
2975 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2976 if (service->immutable == TRUE)
2977 return __connman_error_not_supported(msg);
2979 g_free(service->passphrase);
2980 service->passphrase = NULL;
2982 passphrase_changed(service);
2984 service_save(service);
2986 return __connman_error_invalid_property(msg);
2988 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2991 static connman_bool_t is_ignore(struct connman_service *service)
2993 if (service->autoconnect == FALSE)
2996 if (service->roaming == TRUE)
2999 if (service->ignore == TRUE)
3002 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
3008 void __connman_service_auto_connect(void)
3010 struct connman_service *service = NULL;
3011 GSequenceIter *iter;
3015 if (__connman_session_mode() == TRUE) {
3016 DBG("Session mode enabled: auto connect disabled");
3020 iter = g_sequence_get_begin_iter(service_list);
3022 while (g_sequence_iter_is_end(iter) == FALSE) {
3023 service = g_sequence_get(iter);
3025 if (service->pending != NULL)
3028 if (is_connecting(service) == TRUE)
3031 if (service->favorite == FALSE)
3034 if (is_connected(service) == TRUE)
3037 if (is_ignore(service) == FALSE && service->state ==
3038 CONNMAN_SERVICE_STATE_IDLE)
3043 iter = g_sequence_iter_next(iter);
3046 if (service != NULL) {
3047 service->userconnect = FALSE;
3048 __connman_service_connect(service);
3052 static void remove_timeout(struct connman_service *service)
3054 if (service->timeout > 0) {
3055 g_source_remove(service->timeout);
3056 service->timeout = 0;
3060 static void reply_pending(struct connman_service *service, int error)
3062 remove_timeout(service);
3064 if (service->pending != NULL) {
3068 reply = __connman_error_failed(service->pending,
3071 g_dbus_send_message(connection, reply);
3075 sender = dbus_message_get_interface(service->pending);
3077 DBG("sender %s", sender);
3079 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
3080 g_dbus_send_reply(connection, service->pending,
3081 DBUS_TYPE_OBJECT_PATH, &service->path,
3084 g_dbus_send_reply(connection, service->pending,
3088 dbus_message_unref(service->pending);
3089 service->pending = NULL;
3093 static gboolean connect_timeout(gpointer user_data)
3095 struct connman_service *service = user_data;
3096 connman_bool_t autoconnect = FALSE;
3098 DBG("service %p", service);
3100 service->timeout = 0;
3102 if (service->network != NULL)
3103 __connman_network_disconnect(service->network);
3105 __connman_ipconfig_disable(service->ipconfig_ipv4);
3106 __connman_ipconfig_disable(service->ipconfig_ipv6);
3108 __connman_stats_service_unregister(service);
3110 if (service->pending != NULL) {
3113 reply = __connman_error_operation_timeout(service->pending);
3115 g_dbus_send_message(connection, reply);
3117 dbus_message_unref(service->pending);
3118 service->pending = NULL;
3122 __connman_service_ipconfig_indicate_state(service,
3123 CONNMAN_SERVICE_STATE_FAILURE,
3124 CONNMAN_IPCONFIG_TYPE_IPV4);
3125 __connman_service_ipconfig_indicate_state(service,
3126 CONNMAN_SERVICE_STATE_FAILURE,
3127 CONNMAN_IPCONFIG_TYPE_IPV6);
3129 if (autoconnect == TRUE && service->userconnect == FALSE)
3130 __connman_service_auto_connect();
3135 static void set_reconnect_state(struct connman_service *service,
3136 connman_bool_t reconnect)
3138 struct connman_device *device;
3140 if (service->network == NULL)
3143 device = connman_network_get_device(service->network);
3147 __connman_device_set_reconnect(device, reconnect);
3150 static connman_bool_t get_reconnect_state(struct connman_service *service)
3152 struct connman_device *device;
3154 if (service->network == NULL)
3157 device = connman_network_get_device(service->network);
3161 return __connman_device_get_reconnect(device);
3164 static DBusMessage *connect_service(DBusConnection *conn,
3165 DBusMessage *msg, void *user_data)
3167 struct connman_service *service = user_data;
3168 GSequenceIter *iter;
3171 DBG("service %p", service);
3173 if (service->pending != NULL)
3174 return __connman_error_in_progress(msg);
3176 iter = g_sequence_get_begin_iter(service_list);
3178 while (g_sequence_iter_is_end(iter) == FALSE) {
3179 struct connman_service *temp = g_sequence_get(iter);
3181 if (service->type == temp->type && is_connecting(temp) == TRUE)
3182 return __connman_error_in_progress(msg);
3184 iter = g_sequence_iter_next(iter);
3187 service->ignore = FALSE;
3189 service->userconnect = TRUE;
3191 service->pending = dbus_message_ref(msg);
3193 set_reconnect_state(service, FALSE);
3195 err = __connman_service_connect(service);
3197 if (service->pending == NULL)
3200 if (err != -EINPROGRESS) {
3201 dbus_message_unref(service->pending);
3202 service->pending = NULL;
3204 return __connman_error_failed(msg, -err);
3210 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3213 static DBusMessage *disconnect_service(DBusConnection *conn,
3214 DBusMessage *msg, void *user_data)
3216 struct connman_service *service = user_data;
3219 DBG("service %p", service);
3221 reply_pending(service, ECONNABORTED);
3223 service->ignore = TRUE;
3225 set_reconnect_state(service, FALSE);
3227 err = __connman_service_disconnect(service);
3229 if (err != -EINPROGRESS)
3230 return __connman_error_failed(msg, -err);
3235 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3238 static DBusMessage *remove_service(DBusConnection *conn,
3239 DBusMessage *msg, void *user_data)
3241 struct connman_service *service = user_data;
3243 DBG("service %p", service);
3245 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
3246 return __connman_error_not_supported(msg);
3248 if (service->immutable == TRUE)
3249 return __connman_error_not_supported(msg);
3251 if (service->favorite == FALSE && service->state !=
3252 CONNMAN_SERVICE_STATE_FAILURE)
3253 return __connman_error_not_supported(msg);
3255 set_reconnect_state(service, FALSE);
3257 __connman_service_disconnect(service);
3259 g_free(service->passphrase);
3260 service->passphrase = NULL;
3262 passphrase_changed(service);
3266 __connman_service_set_favorite(service, FALSE);
3267 service_save(service);
3269 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3272 static gboolean check_suitable_state(enum connman_service_state a,
3273 enum connman_service_state b)
3276 * Special check so that "ready" service can be moved before
3279 if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
3280 b == CONNMAN_SERVICE_STATE_READY) ||
3281 (b == CONNMAN_SERVICE_STATE_ONLINE &&
3282 a == CONNMAN_SERVICE_STATE_READY))
3288 static void downgrade_state(struct connman_service *service)
3290 if (service == NULL)
3293 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
3294 service->state_ipv6);
3296 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
3297 __connman_service_ipconfig_indicate_state(service,
3298 CONNMAN_SERVICE_STATE_READY,
3299 CONNMAN_IPCONFIG_TYPE_IPV4);
3301 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
3302 __connman_service_ipconfig_indicate_state(service,
3303 CONNMAN_SERVICE_STATE_READY,
3304 CONNMAN_IPCONFIG_TYPE_IPV6);
3307 static void apply_relevant_default_downgrade(struct connman_service *service)
3309 struct connman_service *def_service;
3311 def_service = get_default();
3312 if (def_service == NULL)
3315 if (def_service == service &&
3316 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
3317 def_service->state = CONNMAN_SERVICE_STATE_READY;
3320 static DBusMessage *move_service(DBusConnection *conn,
3321 DBusMessage *msg, void *user_data,
3324 struct connman_service *service = user_data;
3325 struct connman_service *target;
3327 GSequenceIter *src, *dst;
3328 enum connman_ipconfig_method target4, target6;
3329 enum connman_ipconfig_method service4, service6;
3331 DBG("service %p", service);
3333 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3336 if (service->favorite == FALSE)
3337 return __connman_error_not_supported(msg);
3339 target = find_service(path);
3340 if (target == NULL || target->favorite == FALSE || target == service ||
3341 target->type == CONNMAN_SERVICE_TYPE_VPN)
3342 return __connman_error_invalid_service(msg);
3344 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3345 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3346 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3347 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3349 DBG("target %s method %d/%d state %d/%d", target->identifier,
3351 target->state_ipv4, target->state_ipv6);
3353 DBG("service %s method %d/%d state %d/%d", service->identifier,
3355 service->state_ipv4, service->state_ipv6);
3358 * If method is OFF, then we do not need to check the corresponding
3361 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3362 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3363 if (check_suitable_state(target->state_ipv6,
3364 service->state_ipv6) == FALSE)
3365 return __connman_error_invalid_service(msg);
3369 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3370 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3371 if (check_suitable_state(target->state_ipv4,
3372 service->state_ipv4) == FALSE)
3373 return __connman_error_invalid_service(msg);
3377 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3378 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3379 if (check_suitable_state(target->state_ipv6,
3380 service->state_ipv6) == FALSE)
3381 return __connman_error_invalid_service(msg);
3385 if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3386 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3387 if (check_suitable_state(target->state_ipv4,
3388 service->state_ipv4) == FALSE)
3389 return __connman_error_invalid_service(msg);
3393 g_get_current_time(&service->modified);
3394 service_save(service);
3396 src = g_hash_table_lookup(service_hash, service->identifier);
3397 dst = g_hash_table_lookup(service_hash, target->identifier);
3400 * If the service which goes down is the default service and is
3401 * online, we downgrade directly its state to ready so:
3402 * the service which goes up, needs to recompute its state which
3403 * is triggered via downgrading it - if relevant - to state ready.
3405 if (before == TRUE) {
3406 apply_relevant_default_downgrade(target);
3407 g_sequence_move(src, dst);
3408 downgrade_state(service);
3410 apply_relevant_default_downgrade(service);
3411 g_sequence_move(dst, src);
3412 downgrade_state(target);
3415 services_changed(FALSE);
3417 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3420 static DBusMessage *move_before(DBusConnection *conn,
3421 DBusMessage *msg, void *user_data)
3423 return move_service(conn, msg, user_data, TRUE);
3426 static DBusMessage *move_after(DBusConnection *conn,
3427 DBusMessage *msg, void *user_data)
3429 return move_service(conn, msg, user_data, FALSE);
3432 static DBusMessage *reset_counters(DBusConnection *conn,
3433 DBusMessage *msg, void *user_data)
3435 struct connman_service *service = user_data;
3437 reset_stats(service);
3439 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3442 static GDBusMethodTable service_methods[] = {
3443 { "GetProperties", "", "a{sv}", get_properties },
3444 { "SetProperty", "sv", "", set_property },
3445 { "ClearProperty", "s", "", clear_property },
3446 { "Connect", "", "", connect_service,
3447 G_DBUS_METHOD_FLAG_ASYNC },
3448 { "Disconnect", "", "", disconnect_service },
3449 { "Remove", "", "", remove_service },
3450 { "MoveBefore", "o", "", move_before },
3451 { "MoveAfter", "o", "", move_after },
3452 { "ResetCounters", "", "", reset_counters },
3456 static GDBusSignalTable service_signals[] = {
3457 { "PropertyChanged", "sv" },
3461 static void service_free(gpointer user_data)
3463 struct connman_service *service = user_data;
3464 char *path = service->path;
3466 DBG("service %p", service);
3468 reply_pending(service, ENOENT);
3470 g_hash_table_remove(service_hash, service->identifier);
3472 __connman_notifier_service_remove(service);
3474 stats_stop(service);
3476 service->path = NULL;
3479 services_changed(FALSE);
3481 g_dbus_unregister_interface(connection, path,
3482 CONNMAN_SERVICE_INTERFACE);
3486 g_hash_table_destroy(service->counter_table);
3488 if (service->network != NULL) {
3489 if (service->network_created == TRUE)
3490 connman_network_unref(service->network);
3493 if (service->provider != NULL)
3494 connman_provider_unref(service->provider);
3496 if (service->ipconfig_ipv4 != NULL) {
3497 connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3498 connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3499 connman_ipconfig_unref(service->ipconfig_ipv4);
3500 service->ipconfig_ipv4 = NULL;
3503 if (service->ipconfig_ipv6 != NULL) {
3504 connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3505 connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3506 connman_ipconfig_unref(service->ipconfig_ipv6);
3507 service->ipconfig_ipv6 = NULL;
3510 g_strfreev(service->nameservers);
3511 g_strfreev(service->nameservers_config);
3512 g_strfreev(service->nameservers_auto);
3513 g_strfreev(service->domains);
3514 g_strfreev(service->proxies);
3515 g_strfreev(service->excludes);
3517 g_free(service->domainname);
3518 g_free(service->pac);
3519 g_free(service->name);
3520 g_free(service->passphrase);
3521 g_free(service->agent_passphrase);
3522 g_free(service->identifier);
3523 g_free(service->eap);
3524 g_free(service->identity);
3525 g_free(service->agent_identity);
3526 g_free(service->ca_cert_file);
3527 g_free(service->client_cert_file);
3528 g_free(service->private_key_file);
3529 g_free(service->private_key_passphrase);
3530 g_free(service->phase2);
3532 if (service->stats.timer != NULL)
3533 g_timer_destroy(service->stats.timer);
3534 if (service->stats_roaming.timer != NULL)
3535 g_timer_destroy(service->stats_roaming.timer);
3541 * __connman_service_put:
3542 * @service: service structure
3544 * Release service if no longer needed
3546 void __connman_service_put(struct connman_service *service)
3548 GSequenceIter *iter;
3550 DBG("service %p", service);
3552 if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
3555 iter = g_hash_table_lookup(service_hash, service->identifier);
3557 reply_pending(service, ECONNABORTED);
3559 __connman_service_disconnect(service);
3561 g_sequence_remove(iter);
3563 service_free(service);
3567 static void stats_init(struct connman_service *service)
3570 service->stats.valid = FALSE;
3571 service->stats.enabled = FALSE;
3572 service->stats.timer = g_timer_new();
3575 service->stats_roaming.valid = FALSE;
3576 service->stats_roaming.enabled = FALSE;
3577 service->stats_roaming.timer = g_timer_new();
3580 static void service_initialize(struct connman_service *service)
3582 DBG("service %p", service);
3584 service->refcount = 1;
3585 service->session_usage_count = 0;
3587 service->network_created = FALSE;
3589 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3590 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3592 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3593 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3594 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3596 service->favorite = FALSE;
3597 service->immutable = FALSE;
3598 service->hidden = FALSE;
3600 service->ignore = FALSE;
3602 service->userconnect = FALSE;
3606 stats_init(service);
3608 service->provider = NULL;
3610 service->wps = FALSE;
3614 * connman_service_create:
3616 * Allocate a new service.
3618 * Returns: a newly-allocated #connman_service structure
3620 struct connman_service *connman_service_create(void)
3623 struct connman_stats_counter *counters;
3624 const char *counter;
3626 struct connman_service *service;
3628 service = g_try_new0(struct connman_service, 1);
3629 if (service == NULL)
3632 DBG("service %p", service);
3634 service->counter_table = g_hash_table_new_full(g_str_hash,
3635 g_str_equal, NULL, g_free);
3637 for (list = counter_list; list; list = list->next) {
3638 counter = list->data;
3640 counters = g_try_new0(struct connman_stats_counter, 1);
3641 if (counters == NULL) {
3642 g_hash_table_destroy(service->counter_table);
3647 counters->append_all = TRUE;
3649 g_hash_table_replace(service->counter_table, (gpointer)counter,
3653 service_initialize(service);
3659 * connman_service_ref:
3660 * @service: service structure
3662 * Increase reference counter of service
3664 struct connman_service *connman_service_ref(struct connman_service *service)
3668 __sync_fetch_and_add(&service->refcount, 1);
3674 * connman_service_unref:
3675 * @service: service structure
3677 * Decrease reference counter of service
3679 void connman_service_unref(struct connman_service *service)
3681 __connman_service_put(service);
3684 static gint service_compare(gconstpointer a, gconstpointer b,
3687 struct connman_service *service_a = (void *) a;
3688 struct connman_service *service_b = (void *) b;
3689 enum connman_service_state state_a, state_b;
3691 state_a = service_a->state;
3692 state_b = service_b->state;
3694 if (state_a != state_b) {
3695 gboolean a_connected = is_connected(service_a);
3696 gboolean b_connected = is_connected(service_b);
3698 if (a_connected == TRUE && b_connected == TRUE) {
3699 /* We prefer online over ready state */
3700 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
3703 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
3707 if (a_connected == TRUE)
3709 if (b_connected == TRUE)
3712 if (is_connecting(service_a) == TRUE)
3714 if (is_connecting(service_b) == TRUE)
3718 if (service_a->order > service_b->order)
3721 if (service_a->order < service_b->order)
3724 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3727 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3730 if (service_a->type != service_b->type) {
3731 switch (service_a->type) {
3732 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3733 case CONNMAN_SERVICE_TYPE_SYSTEM:
3734 case CONNMAN_SERVICE_TYPE_ETHERNET:
3735 case CONNMAN_SERVICE_TYPE_GPS:
3736 case CONNMAN_SERVICE_TYPE_VPN:
3737 case CONNMAN_SERVICE_TYPE_GADGET:
3739 case CONNMAN_SERVICE_TYPE_WIFI:
3741 case CONNMAN_SERVICE_TYPE_WIMAX:
3742 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3743 case CONNMAN_SERVICE_TYPE_CELLULAR:
3748 return (gint) service_b->strength - (gint) service_a->strength;
3752 * connman_service_get_type:
3753 * @service: service structure
3755 * Get the type of service
3757 enum connman_service_type connman_service_get_type(struct connman_service *service)
3759 if (service == NULL)
3760 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3762 return service->type;
3766 * connman_service_get_interface:
3767 * @service: service structure
3769 * Get network interface of service
3771 char *connman_service_get_interface(struct connman_service *service)
3775 if (service == NULL)
3778 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3779 if (service->ipconfig_ipv4)
3780 index = connman_ipconfig_get_index(
3781 service->ipconfig_ipv4);
3782 else if (service->ipconfig_ipv6)
3783 index = connman_ipconfig_get_index(
3784 service->ipconfig_ipv6);
3788 return connman_inet_ifname(index);
3791 if (service->network == NULL)
3794 index = connman_network_get_index(service->network);
3796 return connman_inet_ifname(index);
3800 * connman_service_get_network:
3801 * @service: service structure
3803 * Get the service network
3805 struct connman_network *
3806 __connman_service_get_network(struct connman_service *service)
3808 if (service == NULL)
3811 return service->network;
3814 struct connman_ipconfig *
3815 __connman_service_get_ip4config(struct connman_service *service)
3817 if (service == NULL)
3820 return service->ipconfig_ipv4;
3823 struct connman_ipconfig *
3824 __connman_service_get_ip6config(struct connman_service *service)
3826 if (service == NULL)
3829 return service->ipconfig_ipv6;
3832 struct connman_ipconfig *
3833 __connman_service_get_ipconfig(struct connman_service *service, int family)
3835 if (family == AF_INET)
3836 return __connman_service_get_ip4config(service);
3837 else if (family == AF_INET6)
3838 return __connman_service_get_ip6config(service);
3844 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3846 if (service == NULL)
3847 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3849 return service->security;
3852 const char *__connman_service_get_phase2(struct connman_service *service)
3854 if (service == NULL)
3857 return service->phase2;
3860 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3862 if (service == NULL)
3865 return service->wps;
3869 * __connman_service_set_favorite:
3870 * @service: service structure
3871 * @favorite: favorite value
3873 * Change the favorite setting of service
3875 int __connman_service_set_favorite(struct connman_service *service,
3876 connman_bool_t favorite)
3878 GSequenceIter *iter;
3880 iter = g_hash_table_lookup(service_hash, service->identifier);
3884 if (service->favorite == favorite)
3887 service->favorite = favorite;
3888 service->order = __connman_service_get_order(service);
3890 favorite_changed(service);
3892 g_sequence_sort_changed(iter, service_compare, NULL);
3894 services_changed(FALSE);
3899 int __connman_service_set_immutable(struct connman_service *service,
3900 connman_bool_t immutable)
3902 service->immutable = immutable;
3904 immutable_changed(service);
3909 void __connman_service_set_string(struct connman_service *service,
3910 const char *key, const char *value)
3912 if (g_str_equal(key, "EAP") == TRUE) {
3913 g_free(service->eap);
3914 service->eap = g_strdup(value);
3915 } else if (g_str_equal(key, "Identity") == TRUE) {
3916 g_free(service->identity);
3917 service->identity = g_strdup(value);
3918 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3919 g_free(service->ca_cert_file);
3920 service->ca_cert_file = g_strdup(value);
3921 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3922 g_free(service->client_cert_file);
3923 service->client_cert_file = g_strdup(value);
3924 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3925 g_free(service->private_key_file);
3926 service->private_key_file = g_strdup(value);
3927 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3928 g_free(service->private_key_passphrase);
3929 service->private_key_passphrase = g_strdup(value);
3930 } else if (g_str_equal(key, "Phase2") == TRUE) {
3931 g_free(service->phase2);
3932 service->phase2 = g_strdup(value);
3933 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3934 g_free(service->passphrase);
3935 service->passphrase = g_strdup(value);
3939 static void service_complete(struct connman_service *service)
3941 reply_pending(service, EIO);
3943 if (service->userconnect == FALSE)
3944 __connman_service_auto_connect();
3946 g_get_current_time(&service->modified);
3947 service_save(service);
3950 static void report_error_cb(struct connman_service *service,
3951 gboolean retry, void *user_data)
3954 __connman_service_connect(service);
3956 service_complete(service);
3957 services_changed(FALSE);
3958 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3962 static void request_input_cb (struct connman_service *service,
3963 const char *identity, const char *passphrase,
3966 DBG ("RequestInput return, %p", service);
3968 if (identity == NULL && passphrase == NULL && service->wps == FALSE) {
3969 service_complete(service);
3970 services_changed(FALSE);
3971 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3975 if (identity != NULL)
3976 __connman_service_set_agent_identity(service, identity);
3978 if (passphrase != NULL) {
3979 switch (service->security) {
3980 case CONNMAN_SERVICE_SECURITY_WEP:
3981 case CONNMAN_SERVICE_SECURITY_PSK:
3982 __connman_service_set_passphrase(service, passphrase);
3984 case CONNMAN_SERVICE_SECURITY_8021X:
3985 __connman_service_set_agent_passphrase(service,
3988 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3989 case CONNMAN_SERVICE_SECURITY_NONE:
3990 case CONNMAN_SERVICE_SECURITY_WPA:
3991 case CONNMAN_SERVICE_SECURITY_RSN:
3992 DBG("service security '%s' not handled",
3993 security2string(service->security));
3998 __connman_service_connect(service);
4000 /* Never cache agent provided credentials */
4001 __connman_service_set_agent_identity(service, NULL);
4002 __connman_service_set_agent_passphrase(service, NULL);
4005 static void downgrade_connected_services(void)
4007 struct connman_service *up_service;
4008 GSequenceIter *iter;
4010 iter = g_sequence_get_begin_iter(service_list);
4011 while (g_sequence_iter_is_end(iter) == FALSE) {
4012 up_service = g_sequence_get(iter);
4014 if (is_connected(up_service) == FALSE) {
4015 iter = g_sequence_iter_next(iter);
4019 if (up_service->state == CONNMAN_SERVICE_STATE_ONLINE)
4022 downgrade_state(up_service);
4024 iter = g_sequence_iter_next(iter);
4028 static int service_indicate_state(struct connman_service *service)
4030 enum connman_service_state old_state, new_state;
4031 struct connman_service *def_service;
4032 GSequenceIter *iter;
4034 if (service == NULL)
4037 old_state = service->state;
4038 new_state = combine_state(service->state_ipv4, service->state_ipv6);
4040 DBG("service %p old %s - new %s/%s => %s",
4042 state2string(old_state),
4043 state2string(service->state_ipv4),
4044 state2string(service->state_ipv6),
4045 state2string(new_state));
4047 if (old_state == new_state)
4050 def_service = get_default();
4052 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4053 if (def_service != NULL && def_service != service &&
4054 def_service->state == CONNMAN_SERVICE_STATE_ONLINE)
4058 service->state = new_state;
4059 state_changed(service);
4061 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
4062 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
4063 reply_pending(service, ECONNABORTED);
4065 __connman_service_disconnect(service);
4068 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
4069 if (__connman_stats_service_register(service) == 0) {
4070 __connman_stats_get(service, FALSE,
4071 &service->stats.data);
4072 __connman_stats_get(service, TRUE,
4073 &service->stats_roaming.data);
4077 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4078 if (service->login_required == TRUE) {
4079 service->login_required = FALSE;
4080 login_changed(service);
4083 connman_timeserver_sync();
4086 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
4087 connman_bool_t reconnect;
4089 reconnect = get_reconnect_state(service);
4090 if (reconnect == TRUE)
4091 __connman_service_auto_connect();
4093 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4096 if (new_state == CONNMAN_SERVICE_STATE_READY) {
4097 enum connman_ipconfig_method method;
4099 set_reconnect_state(service, TRUE);
4101 __connman_service_set_favorite(service, TRUE);
4103 reply_pending(service, 0);
4105 service->userconnect = FALSE;
4107 g_get_current_time(&service->modified);
4108 service_save(service);
4110 update_nameservers(service);
4111 dns_changed(service);
4112 domain_changed(service);
4114 __connman_notifier_connect(service->type);
4116 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4117 connman_network_get_bool(service->network,
4118 "WiFi.UseWPS") == TRUE) {
4121 pass = connman_network_get_string(service->network,
4124 __connman_service_set_passphrase(service, pass);
4126 connman_network_set_bool(service->network,
4127 "WiFi.UseWPS", FALSE);
4132 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4133 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4134 __connman_ipconfig_disable_ipv6(
4135 service->ipconfig_ipv6);
4137 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4138 def_service = get_default();
4140 if (__connman_notifier_count_connected() == 0 &&
4141 def_service != NULL &&
4142 def_service->provider != NULL)
4143 __connman_provider_disconnect(def_service->provider);
4147 __connman_wispr_stop(service);
4149 __connman_wpad_stop(service);
4151 update_nameservers(service);
4152 dns_changed(service);
4153 domain_changed(service);
4155 __connman_notifier_disconnect(service->type);
4158 * Previous services which are connected and which states
4159 * are set to online should reset relevantly ipconfig_state
4160 * to ready so wispr/portal will be rerun on those
4162 downgrade_connected_services();
4165 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4166 if (service->userconnect == TRUE &&
4167 __connman_agent_report_error(service,
4168 error2string(service->error),
4169 report_error_cb, NULL) == -EIO)
4171 service_complete(service);
4173 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4175 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4177 iter = g_hash_table_lookup(service_hash, service->identifier);
4179 g_sequence_sort_changed(iter, service_compare, NULL);
4181 services_changed(FALSE);
4183 if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
4189 int __connman_service_indicate_error(struct connman_service *service,
4190 enum connman_service_error error)
4192 DBG("service %p error %d", service, error);
4194 if (service == NULL)
4197 service->error = error;
4199 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4200 __connman_service_set_passphrase(service, NULL);
4202 __connman_service_ipconfig_indicate_state(service,
4203 CONNMAN_SERVICE_STATE_FAILURE,
4204 CONNMAN_IPCONFIG_TYPE_IPV4);
4205 __connman_service_ipconfig_indicate_state(service,
4206 CONNMAN_SERVICE_STATE_FAILURE,
4207 CONNMAN_IPCONFIG_TYPE_IPV6);
4211 int __connman_service_clear_error(struct connman_service *service)
4213 DBG("service %p", service);
4215 if (service == NULL)
4218 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4221 service->state_ipv4 = service->state_ipv6 =
4222 CONNMAN_SERVICE_STATE_UNKNOWN;
4223 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
4225 if (service->favorite == TRUE)
4226 set_reconnect_state(service, TRUE);
4228 __connman_service_ipconfig_indicate_state(service,
4229 CONNMAN_SERVICE_STATE_IDLE,
4230 CONNMAN_IPCONFIG_TYPE_IPV6);
4233 * Toggling the IPv6 state to IDLE could trigger the auto connect
4234 * machinery and consequently the IPv4 state.
4236 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4237 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4240 return __connman_service_ipconfig_indicate_state(service,
4241 CONNMAN_SERVICE_STATE_IDLE,
4242 CONNMAN_IPCONFIG_TYPE_IPV4);
4245 int __connman_service_indicate_default(struct connman_service *service)
4247 DBG("service %p", service);
4254 enum connman_service_state __connman_service_ipconfig_get_state(
4255 struct connman_service *service,
4256 enum connman_ipconfig_type type)
4258 if (service == NULL)
4259 return CONNMAN_SERVICE_STATE_UNKNOWN;
4261 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4262 return service->state_ipv4;
4264 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4265 return service->state_ipv6;
4267 return CONNMAN_SERVICE_STATE_UNKNOWN;
4270 static void check_proxy_setup(struct connman_service *service)
4273 * We start WPAD if we haven't got a PAC URL from DHCP and
4274 * if our proxy manual configuration is either empty or set
4275 * to AUTO with an empty URL.
4278 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4281 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4282 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4283 service->pac != NULL))
4286 if (__connman_wpad_start(service) < 0) {
4287 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4288 __connman_notifier_proxy_changed(service);
4295 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
4299 * How many networks are connected at the same time. If more than 1,
4300 * then set the rp_filter setting properly (loose mode routing) so that network
4301 * connectivity works ok. This is only done for IPv4 networks as IPv6
4302 * does not have rp_filter knob.
4304 static int connected_networks_count;
4305 static int original_rp_filter;
4307 static void service_rp_filter(struct connman_service *service,
4310 enum connman_ipconfig_method method;
4312 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4315 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
4316 case CONNMAN_IPCONFIG_METHOD_OFF:
4317 case CONNMAN_IPCONFIG_METHOD_AUTO:
4319 case CONNMAN_IPCONFIG_METHOD_FIXED:
4320 case CONNMAN_IPCONFIG_METHOD_MANUAL:
4321 case CONNMAN_IPCONFIG_METHOD_DHCP:
4325 if (connected == TRUE) {
4326 if (connected_networks_count == 1) {
4328 filter_value = __connman_ipconfig_set_rp_filter();
4329 if (filter_value < 0)
4332 original_rp_filter = filter_value;
4334 connected_networks_count++;
4337 if (connected_networks_count == 2)
4338 __connman_ipconfig_unset_rp_filter(original_rp_filter);
4340 connected_networks_count--;
4341 if (connected_networks_count < 0)
4342 connected_networks_count = 0;
4345 DBG("%s %s ipconfig %p method %d count %d filter %d",
4346 connected ? "connected" : "disconnected", service->identifier,
4347 service->ipconfig_ipv4, method,
4348 connected_networks_count, original_rp_filter);
4351 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
4352 enum connman_service_state new_state,
4353 enum connman_ipconfig_type type)
4355 struct connman_ipconfig *ipconfig = NULL;
4356 enum connman_service_state old_state;
4359 if (service == NULL)
4362 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4363 old_state = service->state_ipv4;
4364 ipconfig = service->ipconfig_ipv4;
4365 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4366 old_state = service->state_ipv6;
4367 ipconfig = service->ipconfig_ipv6;
4370 if (ipconfig == NULL)
4374 if (old_state == new_state)
4377 DBG("service %p (%s) state %d (%s) type %d (%s)",
4378 service, service ? service->identifier : NULL,
4379 new_state, state2string(new_state),
4380 type, __connman_ipconfig_type2string(type));
4382 switch (new_state) {
4383 case CONNMAN_SERVICE_STATE_UNKNOWN:
4384 case CONNMAN_SERVICE_STATE_IDLE:
4385 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
4388 case CONNMAN_SERVICE_STATE_ASSOCIATION:
4390 case CONNMAN_SERVICE_STATE_CONFIGURATION:
4391 __connman_ipconfig_enable(ipconfig);
4393 case CONNMAN_SERVICE_STATE_READY:
4394 update_nameservers(service);
4396 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4397 check_proxy_setup(service);
4398 service_rp_filter(service, TRUE);
4400 __connman_wispr_start(service, type);
4402 case CONNMAN_SERVICE_STATE_ONLINE:
4404 case CONNMAN_SERVICE_STATE_DISCONNECT:
4405 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
4408 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4409 service_rp_filter(service, FALSE);
4412 case CONNMAN_SERVICE_STATE_FAILURE:
4416 /* We keep that state */
4417 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4418 service->state_ipv4 = new_state;
4419 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4420 service->state_ipv6 = new_state;
4422 ret = service_indicate_state(service);
4425 * If the ipconfig method is OFF, then we set the state to IDLE
4426 * so that it will not affect the combined state in the future.
4428 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4429 enum connman_ipconfig_method method;
4430 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4431 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4432 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4433 service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
4434 ret = service_indicate_state(service);
4437 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4438 enum connman_ipconfig_method method;
4439 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4440 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4441 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4442 service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4443 ret = service_indicate_state(service);
4450 int __connman_service_request_login(struct connman_service *service)
4452 DBG("service %p", service);
4454 if (service == NULL)
4457 service->login_required = TRUE;
4458 login_changed(service);
4463 static connman_bool_t prepare_network(struct connman_service *service)
4465 enum connman_network_type type;
4466 unsigned int ssid_len;
4468 type = connman_network_get_type(service->network);
4471 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4472 case CONNMAN_NETWORK_TYPE_VENDOR:
4474 case CONNMAN_NETWORK_TYPE_WIFI:
4475 if (connman_network_get_blob(service->network, "WiFi.SSID",
4479 if (service->passphrase != NULL)
4480 connman_network_set_string(service->network,
4481 "WiFi.Passphrase", service->passphrase);
4483 case CONNMAN_NETWORK_TYPE_ETHERNET:
4484 case CONNMAN_NETWORK_TYPE_WIMAX:
4485 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4486 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4487 case CONNMAN_NETWORK_TYPE_CELLULAR:
4494 static void prepare_8021x(struct connman_service *service)
4496 if (service->eap != NULL)
4497 connman_network_set_string(service->network, "WiFi.EAP",
4500 if (service->identity != NULL)
4501 connman_network_set_string(service->network, "WiFi.Identity",
4504 if (service->ca_cert_file != NULL)
4505 connman_network_set_string(service->network, "WiFi.CACertFile",
4506 service->ca_cert_file);
4508 if (service->client_cert_file != NULL)
4509 connman_network_set_string(service->network,
4510 "WiFi.ClientCertFile",
4511 service->client_cert_file);
4513 if (service->private_key_file != NULL)
4514 connman_network_set_string(service->network,
4515 "WiFi.PrivateKeyFile",
4516 service->private_key_file);
4518 if (service->private_key_passphrase != NULL)
4519 connman_network_set_string(service->network,
4520 "WiFi.PrivateKeyPassphrase",
4521 service->private_key_passphrase);
4523 if (service->phase2 != NULL)
4524 connman_network_set_string(service->network, "WiFi.Phase2",
4528 static int service_connect(struct connman_service *service)
4532 switch (service->type) {
4533 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4534 case CONNMAN_SERVICE_TYPE_SYSTEM:
4535 case CONNMAN_SERVICE_TYPE_GPS:
4536 case CONNMAN_SERVICE_TYPE_GADGET:
4538 case CONNMAN_SERVICE_TYPE_ETHERNET:
4539 case CONNMAN_SERVICE_TYPE_WIMAX:
4540 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4541 case CONNMAN_SERVICE_TYPE_CELLULAR:
4542 case CONNMAN_SERVICE_TYPE_VPN:
4544 case CONNMAN_SERVICE_TYPE_WIFI:
4545 switch (service->security) {
4546 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4547 case CONNMAN_SERVICE_SECURITY_NONE:
4549 case CONNMAN_SERVICE_SECURITY_WEP:
4550 case CONNMAN_SERVICE_SECURITY_PSK:
4551 case CONNMAN_SERVICE_SECURITY_WPA:
4552 case CONNMAN_SERVICE_SECURITY_RSN:
4553 if (service->passphrase == NULL) {
4554 if (service->network == NULL)
4557 if (service->wps == FALSE ||
4558 connman_network_get_bool(
4560 "WiFi.UseWPS") == FALSE)
4564 case CONNMAN_SERVICE_SECURITY_8021X:
4565 if (service->eap == NULL)
4569 * never request credentials if using EAP-TLS
4570 * (EAP-TLS networks need to be fully provisioned)
4572 if (g_str_equal(service->eap, "tls") == TRUE)
4576 * Return -ENOKEY if either identity or passphrase is
4577 * missing. Agent provided credentials can be used as
4578 * fallback if needed.
4580 if ((service->identity == NULL &&
4581 service->agent_identity == NULL) ||
4582 (service->passphrase == NULL &&
4583 service->agent_passphrase == NULL))
4591 if (service->network != NULL) {
4592 if (prepare_network(service) == FALSE)
4595 switch (service->security) {
4596 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4597 case CONNMAN_SERVICE_SECURITY_NONE:
4598 case CONNMAN_SERVICE_SECURITY_WEP:
4599 case CONNMAN_SERVICE_SECURITY_PSK:
4600 case CONNMAN_SERVICE_SECURITY_WPA:
4601 case CONNMAN_SERVICE_SECURITY_RSN:
4603 case CONNMAN_SERVICE_SECURITY_8021X:
4604 prepare_8021x(service);
4608 if (__connman_stats_service_register(service) == 0) {
4609 __connman_stats_get(service, FALSE,
4610 &service->stats.data);
4611 __connman_stats_get(service, TRUE,
4612 &service->stats_roaming.data);
4615 if (service->ipconfig_ipv4)
4616 __connman_ipconfig_enable(service->ipconfig_ipv4);
4617 if (service->ipconfig_ipv6)
4618 __connman_ipconfig_enable(service->ipconfig_ipv6);
4620 err = __connman_network_connect(service->network);
4621 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4622 service->provider != NULL)
4623 err = __connman_provider_connect(service->provider);
4628 if (err != -EINPROGRESS) {
4629 __connman_ipconfig_disable(service->ipconfig_ipv4);
4630 __connman_ipconfig_disable(service->ipconfig_ipv6);
4631 __connman_stats_service_unregister(service);
4639 int __connman_service_connect(struct connman_service *service)
4643 DBG("service %p state %s", service, state2string(service->state));
4645 if (is_connected(service) == TRUE)
4648 if (is_connecting(service) == TRUE)
4651 switch (service->type) {
4652 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4653 case CONNMAN_SERVICE_TYPE_SYSTEM:
4654 case CONNMAN_SERVICE_TYPE_GPS:
4655 case CONNMAN_SERVICE_TYPE_GADGET:
4658 err = service_connect(service);
4664 if (err == -EINPROGRESS) {
4665 if (service->timeout == 0)
4666 service->timeout = g_timeout_add_seconds(
4667 CONNECT_TIMEOUT, connect_timeout, service);
4669 return -EINPROGRESS;
4672 __connman_service_ipconfig_indicate_state(service,
4673 CONNMAN_SERVICE_STATE_FAILURE,
4674 CONNMAN_IPCONFIG_TYPE_IPV4);
4675 __connman_service_ipconfig_indicate_state(service,
4676 CONNMAN_SERVICE_STATE_FAILURE,
4677 CONNMAN_IPCONFIG_TYPE_IPV6);
4679 if (service->network != NULL)
4680 __connman_network_disconnect(service->network);
4681 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4682 service->provider != NULL)
4683 __connman_provider_disconnect(service->provider);
4685 if (service->userconnect == TRUE) {
4686 if (err == -ENOKEY) {
4687 if (__connman_agent_request_passphrase_input(service,
4690 return -EINPROGRESS;
4692 reply_pending(service, err);
4698 int __connman_service_disconnect(struct connman_service *service)
4702 DBG("service %p", service);
4704 if (service->network != NULL) {
4705 err = __connman_network_disconnect(service->network);
4706 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4707 service->provider != NULL)
4708 err = __connman_provider_disconnect(service->provider);
4712 if (err < 0 && err != -EINPROGRESS)
4715 __connman_6to4_remove(service->ipconfig_ipv4);
4717 if (service->ipconfig_ipv4)
4718 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
4721 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
4724 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
4725 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
4727 __connman_ipconfig_disable(service->ipconfig_ipv4);
4728 __connman_ipconfig_disable(service->ipconfig_ipv6);
4730 __connman_stats_service_unregister(service);
4735 int __connman_service_disconnect_all(void)
4737 GSequenceIter *iter;
4738 GSList *services = NULL, *list;
4742 iter = g_sequence_get_begin_iter(service_list);
4744 while (g_sequence_iter_is_end(iter) == FALSE) {
4745 struct connman_service *service = g_sequence_get(iter);
4747 services = g_slist_prepend(services, service);
4749 iter = g_sequence_iter_next(iter);
4752 for (list = services; list != NULL; list = list->next) {
4753 struct connman_service *service = list->data;
4755 service->ignore = TRUE;
4757 set_reconnect_state(service, FALSE);
4759 __connman_service_disconnect(service);
4769 * __connman_service_lookup:
4770 * @pattern: search pattern
4771 * @path: return object path
4773 * Look up a service path from a search pattern
4775 int __connman_service_lookup(const char *pattern, const char **path)
4777 GHashTableIter iter;
4778 gpointer key, value;
4779 struct connman_device *device;
4782 g_hash_table_iter_init(&iter, service_hash);
4784 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4785 GSequenceIter *iter = value;
4786 struct connman_service *service = g_sequence_get(iter);
4788 if (g_strcmp0(service->identifier, pattern) == 0 ||
4789 g_strcmp0(service->name, pattern) == 0) {
4790 *path = (const char *) service->path;
4794 if (service->network == NULL)
4797 device = connman_network_get_device(service->network);
4801 ifname = connman_device_get_string(device, "Interface");
4802 if (ifname != NULL && g_strcmp0(ifname, pattern) == 0) {
4803 *path = (const char *) service->path;
4813 * lookup_by_identifier:
4814 * @identifier: service identifier
4816 * Look up a service by identifier (reference count will not be increased)
4818 static struct connman_service *lookup_by_identifier(const char *identifier)
4820 GSequenceIter *iter;
4822 iter = g_hash_table_lookup(service_hash, identifier);
4824 return g_sequence_get(iter);
4829 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4830 const char *ssid, const char *mode, const char *security,
4833 struct connman_network *network;
4836 unsigned int i, ssid_len;
4838 ssid_len = strlen(ssid);
4842 network = connman_network_create(group, CONNMAN_NETWORK_TYPE_WIFI);
4843 if (network == NULL)
4846 connman_network_set_blob(network, "WiFi.SSID",
4847 (unsigned char *) ssid, ssid_len);
4849 connman_network_set_string(network, "WiFi.Mode", mode);
4850 connman_network_set_string(network, "WiFi.Security", security);
4852 name = g_try_malloc0(ssid_len + 1);
4854 connman_network_unref(network);
4858 for (i = 0; i < ssid_len; i++) {
4859 if (g_ascii_isprint(ssid[i]))
4865 connman_network_set_name(network, name);
4869 index = connman_device_get_index(device);
4870 connman_network_set_index(network, index);
4872 if (connman_device_add_network(device, network) < 0) {
4873 connman_network_unref(network);
4877 connman_network_set_available(network, TRUE);
4882 int __connman_service_create_and_connect(DBusMessage *msg)
4884 struct connman_service *service;
4885 struct connman_network *network;
4886 struct connman_device *device;
4887 DBusMessageIter iter, array;
4888 const char *mode = "managed", *security = "none", *group_security;
4889 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4890 connman_bool_t network_created = FALSE;
4891 unsigned int ssid_len = 0;
4896 dbus_message_iter_init(msg, &iter);
4897 dbus_message_iter_recurse(&iter, &array);
4899 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4900 DBusMessageIter entry, value;
4903 dbus_message_iter_recurse(&array, &entry);
4904 dbus_message_iter_get_basic(&entry, &key);
4906 dbus_message_iter_next(&entry);
4907 dbus_message_iter_recurse(&entry, &value);
4909 switch (dbus_message_iter_get_arg_type(&value)) {
4910 case DBUS_TYPE_STRING:
4911 if (g_str_equal(key, "Type") == TRUE)
4912 dbus_message_iter_get_basic(&value, &type);
4913 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4914 g_str_equal(key, "Mode") == TRUE)
4915 dbus_message_iter_get_basic(&value, &mode);
4916 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4917 g_str_equal(key, "Security") == TRUE)
4918 dbus_message_iter_get_basic(&value, &security);
4919 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4920 g_str_equal(key, "Passphrase") == TRUE)
4921 dbus_message_iter_get_basic(&value, &passphrase);
4922 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4923 g_str_equal(key, "SSID") == TRUE)
4924 dbus_message_iter_get_basic(&value, &ssid);
4927 dbus_message_iter_next(&array);
4933 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4939 ssid_len = strlen(ssid);
4943 if (g_strcmp0(security, "none") != 0 &&
4944 g_strcmp0(security, "wep") != 0 &&
4945 g_strcmp0(security, "psk") != 0 &&
4946 g_strcmp0(security, "wpa") != 0 &&
4947 g_strcmp0(security, "rsn") != 0 &&
4948 g_strcmp0(security, "ieee8021x") != 0)
4951 device = __connman_device_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4955 ident = connman_device_get_ident(device);
4960 if (!g_strcmp0(security, "wpa") ||
4961 !g_strcmp0(security, "rsn"))
4962 group_security = "psk";
4964 group_security = security;
4966 group = wifi_build_group_name((unsigned char *) ssid,
4967 ssid_len, mode, group_security);
4971 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4973 service = lookup_by_identifier(name);
4975 if (service == NULL) {
4976 network = create_hidden_wifi(device, ssid,
4977 mode, security, group);
4978 if (network != NULL) {
4979 connman_network_set_group(network, group);
4980 network_created = TRUE;
4983 service = lookup_by_identifier(name);
4989 if (service == NULL) {
4994 service->network_created = network_created;
4996 if (is_connected(service) == TRUE) {
5001 if (is_connecting(service) == TRUE) {
5006 set_reconnect_state(service, FALSE);
5008 __connman_device_disconnect(device);
5010 if (passphrase != NULL) {
5011 g_free(service->passphrase);
5012 service->passphrase = g_strdup(passphrase);
5015 service->userconnect = TRUE;
5017 err = __connman_service_connect(service);
5018 if (err < 0 && err != -EINPROGRESS)
5021 service->pending = dbus_message_ref(msg);
5026 if (service != NULL && service->network_created == TRUE) {
5027 struct connman_network *network = service->network;
5029 if (network != NULL) {
5030 connman_network_set_available(network, FALSE);
5031 __connman_device_cleanup_networks(device);
5033 __connman_service_put(service);
5039 static void provision_changed(gpointer value, gpointer user_data)
5041 struct connman_service *service = value;
5042 char *path = user_data;
5044 __connman_config_provision_service_ident(service, path);
5047 void __connman_service_provision_changed(const char *ident)
5049 g_sequence_foreach(service_list, provision_changed, (void *)ident);
5052 int __connman_service_provision(DBusMessage *msg)
5054 GKeyFile *keyfile = NULL;
5055 const char *config_str = NULL;
5056 char *group = NULL, *ident = NULL;
5058 struct connman_service *service;
5062 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
5065 if (config_str == NULL || strlen(config_str) == 0)
5068 keyfile = g_key_file_new();
5070 /* populate GKeyFile with config_str */
5071 if (g_key_file_load_from_data(keyfile, config_str,
5072 strlen(config_str), 0, NULL) == FALSE) {
5078 * read only one group of settings (only one service supported, no
5081 group = g_key_file_get_start_group(keyfile);
5083 if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
5088 err = __connman_config_load_service(keyfile, group, TRUE);
5092 ident = group + strlen("service_");
5094 /* trigger service provisioning if service exists */
5095 service = lookup_by_identifier(ident);
5096 if (service != NULL)
5097 __connman_config_provision_service(service);
5099 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
5105 if (keyfile != NULL)
5106 g_key_file_free(keyfile);
5112 * __connman_service_get:
5113 * @identifier: service identifier
5115 * Look up a service by identifier or create a new one if not found
5117 static struct connman_service *service_get(const char *identifier)
5119 struct connman_service *service;
5120 GSequenceIter *iter;
5122 iter = g_hash_table_lookup(service_hash, identifier);
5124 service = g_sequence_get(iter);
5125 if (service != NULL)
5126 connman_service_ref(service);
5130 service = connman_service_create();
5131 if (service == NULL)
5134 DBG("service %p", service);
5136 service->identifier = g_strdup(identifier);
5138 iter = g_sequence_insert_sorted(service_list, service,
5139 service_compare, NULL);
5141 g_hash_table_insert(service_hash, service->identifier, iter);
5146 static int service_register(struct connman_service *service)
5148 GSequenceIter *iter;
5150 DBG("service %p", service);
5152 if (service->path != NULL)
5155 service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
5156 service->identifier);
5158 DBG("path %s", service->path);
5160 __connman_config_provision_service(service);
5162 service_load(service);
5164 g_dbus_register_interface(connection, service->path,
5165 CONNMAN_SERVICE_INTERFACE,
5166 service_methods, service_signals,
5167 NULL, service, NULL);
5169 iter = g_hash_table_lookup(service_hash, service->identifier);
5171 g_sequence_sort_changed(iter, service_compare, NULL);
5173 services_changed(TRUE);
5178 static void service_up(struct connman_ipconfig *ipconfig)
5180 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5182 DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
5184 link_changed(service);
5186 service->stats.valid = FALSE;
5187 service->stats_roaming.valid = FALSE;
5190 static void service_down(struct connman_ipconfig *ipconfig)
5192 DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
5195 static void service_lower_up(struct connman_ipconfig *ipconfig)
5197 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5199 DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
5201 stats_start(service);
5204 static void service_lower_down(struct connman_ipconfig *ipconfig)
5206 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5208 DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
5210 stats_stop(service);
5211 service_save(service);
5214 static void service_ip_bound(struct connman_ipconfig *ipconfig)
5216 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5217 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5218 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5220 DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
5222 type = __connman_ipconfig_get_config_type(ipconfig);
5223 method = __connman_ipconfig_get_method(ipconfig);
5225 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5228 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5229 method == CONNMAN_IPCONFIG_METHOD_AUTO)
5230 __connman_service_ipconfig_indicate_state(service,
5231 CONNMAN_SERVICE_STATE_READY,
5232 CONNMAN_IPCONFIG_TYPE_IPV6);
5234 settings_changed(service, ipconfig);
5237 static void service_ip_release(struct connman_ipconfig *ipconfig)
5239 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5240 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5241 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5243 DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
5245 type = __connman_ipconfig_get_config_type(ipconfig);
5246 method = __connman_ipconfig_get_method(ipconfig);
5248 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5251 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5252 method == CONNMAN_IPCONFIG_METHOD_OFF)
5253 __connman_service_ipconfig_indicate_state(service,
5254 CONNMAN_SERVICE_STATE_DISCONNECT,
5255 CONNMAN_IPCONFIG_TYPE_IPV6);
5257 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
5258 method == CONNMAN_IPCONFIG_METHOD_OFF)
5259 __connman_service_ipconfig_indicate_state(service,
5260 CONNMAN_SERVICE_STATE_DISCONNECT,
5261 CONNMAN_IPCONFIG_TYPE_IPV4);
5263 settings_changed(service, ipconfig);
5266 static const struct connman_ipconfig_ops service_ops = {
5268 .down = service_down,
5269 .lower_up = service_lower_up,
5270 .lower_down = service_lower_down,
5271 .ip_bound = service_ip_bound,
5272 .ip_release = service_ip_release,
5275 static void setup_ip4config(struct connman_service *service, int index,
5276 enum connman_ipconfig_method method)
5281 service->ipconfig_ipv4 = connman_ipconfig_create(index,
5282 CONNMAN_IPCONFIG_TYPE_IPV4);
5283 if (service->ipconfig_ipv4 == NULL)
5286 connman_ipconfig_set_method(service->ipconfig_ipv4, method);
5288 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
5290 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
5293 static void setup_ip6config(struct connman_service *service, int index)
5298 service->ipconfig_ipv6 = connman_ipconfig_create(index,
5299 CONNMAN_IPCONFIG_TYPE_IPV6);
5300 if (service->ipconfig_ipv6 == NULL)
5303 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
5305 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
5308 void __connman_service_read_ip4config(struct connman_service *service)
5312 if (service->ipconfig_ipv4 == NULL)
5315 keyfile = connman_storage_load_service(service->identifier);
5316 if (keyfile == NULL)
5319 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5320 service->identifier, "IPv4.");
5322 g_key_file_free(keyfile);
5325 void __connman_service_create_ip4config(struct connman_service *service,
5328 DBG("ipv4 %p", service->ipconfig_ipv4);
5330 if (service->ipconfig_ipv4 != NULL)
5333 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5334 __connman_service_read_ip4config(service);
5337 void __connman_service_read_ip6config(struct connman_service *service)
5341 if (service->ipconfig_ipv6 == NULL)
5344 keyfile = connman_storage_load_service(service->identifier);
5345 if (keyfile == NULL)
5348 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5349 service->identifier, "IPv6.");
5351 g_key_file_free(keyfile);
5354 void __connman_service_create_ip6config(struct connman_service *service,
5357 DBG("ipv6 %p", service->ipconfig_ipv6);
5359 if (service->ipconfig_ipv6 != NULL)
5362 setup_ip6config(service, index);
5364 __connman_service_read_ip6config(service);
5368 * __connman_service_lookup_from_network:
5369 * @network: network structure
5371 * Look up a service by network (reference count will not be increased)
5373 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
5375 struct connman_service *service;
5376 const char *ident, *group;
5379 DBG("network %p", network);
5381 if (network == NULL)
5384 ident = __connman_network_get_ident(network);
5388 group = connman_network_get_group(network);
5392 name = g_strdup_printf("%s_%s_%s",
5393 __connman_network_get_type(network), ident, group);
5394 service = lookup_by_identifier(name);
5400 struct connman_service *__connman_service_lookup_from_index(int index)
5402 struct connman_service *service;
5403 GSequenceIter *iter;
5405 iter = g_sequence_get_begin_iter(service_list);
5407 while (g_sequence_iter_is_end(iter) == FALSE) {
5408 service = g_sequence_get(iter);
5410 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
5414 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
5418 iter = g_sequence_iter_next(iter);
5424 const char *__connman_service_get_ident(struct connman_service *service)
5426 return service->identifier;
5429 const char *__connman_service_get_path(struct connman_service *service)
5431 return service->path;
5434 unsigned int __connman_service_get_order(struct connman_service *service)
5436 GSequenceIter *iter;
5438 if (service == NULL)
5441 if (service->favorite == FALSE) {
5446 iter = g_hash_table_lookup(service_hash, service->identifier);
5448 if (g_sequence_iter_get_position(iter) == 0)
5450 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
5451 service->order = 10;
5457 return service->order;
5460 static enum connman_service_type convert_network_type(struct connman_network *network)
5462 enum connman_network_type type = connman_network_get_type(network);
5465 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5466 case CONNMAN_NETWORK_TYPE_VENDOR:
5468 case CONNMAN_NETWORK_TYPE_ETHERNET:
5469 return CONNMAN_SERVICE_TYPE_ETHERNET;
5470 case CONNMAN_NETWORK_TYPE_WIFI:
5471 return CONNMAN_SERVICE_TYPE_WIFI;
5472 case CONNMAN_NETWORK_TYPE_WIMAX:
5473 return CONNMAN_SERVICE_TYPE_WIMAX;
5474 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5475 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5476 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5477 case CONNMAN_NETWORK_TYPE_CELLULAR:
5478 return CONNMAN_SERVICE_TYPE_CELLULAR;
5481 return CONNMAN_SERVICE_TYPE_UNKNOWN;
5484 static enum connman_service_security convert_wifi_security(const char *security)
5486 if (security == NULL)
5487 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5488 else if (g_str_equal(security, "none") == TRUE)
5489 return CONNMAN_SERVICE_SECURITY_NONE;
5490 else if (g_str_equal(security, "wep") == TRUE)
5491 return CONNMAN_SERVICE_SECURITY_WEP;
5492 else if (g_str_equal(security, "psk") == TRUE)
5493 return CONNMAN_SERVICE_SECURITY_PSK;
5494 else if (g_str_equal(security, "ieee8021x") == TRUE)
5495 return CONNMAN_SERVICE_SECURITY_8021X;
5496 else if (g_str_equal(security, "wpa") == TRUE)
5497 return CONNMAN_SERVICE_SECURITY_WPA;
5498 else if (g_str_equal(security, "rsn") == TRUE)
5499 return CONNMAN_SERVICE_SECURITY_RSN;
5501 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5504 static void update_from_network(struct connman_service *service,
5505 struct connman_network *network)
5507 connman_uint8_t strength = service->strength;
5508 GSequenceIter *iter;
5511 DBG("service %p network %p", service, network);
5513 if (is_connected(service) == TRUE)
5516 if (is_connecting(service) == TRUE)
5519 str = connman_network_get_string(network, "Name");
5521 g_free(service->name);
5522 service->name = g_strdup(str);
5523 service->hidden = FALSE;
5525 g_free(service->name);
5526 service->name = NULL;
5527 service->hidden = TRUE;
5530 service->strength = connman_network_get_strength(network);
5531 service->roaming = connman_network_get_bool(network, "Roaming");
5533 if (service->strength == 0) {
5535 * Filter out 0-values; it's unclear what they mean
5536 * and they cause anomalous sorting of the priority list.
5538 service->strength = strength;
5541 str = connman_network_get_string(network, "WiFi.Security");
5542 service->security = convert_wifi_security(str);
5544 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5545 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5547 if (service->strength > strength && service->network != NULL) {
5548 service->network = network;
5550 strength_changed(service);
5553 if (service->network == NULL)
5554 service->network = network;
5556 iter = g_hash_table_lookup(service_hash, service->identifier);
5558 g_sequence_sort_changed(iter, service_compare, NULL);
5562 * __connman_service_create_from_network:
5563 * @network: network structure
5565 * Look up service by network and if not found, create one
5567 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5569 struct connman_service *service;
5570 struct connman_device *device;
5571 const char *ident, *group;
5575 DBG("network %p", network);
5577 if (network == NULL)
5580 ident = __connman_network_get_ident(network);
5584 group = connman_network_get_group(network);
5588 name = g_strdup_printf("%s_%s_%s",
5589 __connman_network_get_type(network), ident, group);
5590 service = service_get(name);
5593 if (service == NULL)
5596 if (__connman_network_get_weakness(network) == TRUE)
5599 if (service->path != NULL) {
5600 update_from_network(service, network);
5601 services_changed(TRUE);
5605 service->type = convert_network_type(network);
5607 switch (service->type) {
5608 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5609 case CONNMAN_SERVICE_TYPE_SYSTEM:
5610 case CONNMAN_SERVICE_TYPE_ETHERNET:
5611 case CONNMAN_SERVICE_TYPE_WIMAX:
5612 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5613 case CONNMAN_SERVICE_TYPE_GPS:
5614 case CONNMAN_SERVICE_TYPE_VPN:
5615 case CONNMAN_SERVICE_TYPE_GADGET:
5616 service->autoconnect = FALSE;
5618 case CONNMAN_SERVICE_TYPE_WIFI:
5619 case CONNMAN_SERVICE_TYPE_CELLULAR:
5620 service->autoconnect = TRUE;
5624 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5625 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5627 update_from_network(service, network);
5629 index = connman_network_get_index(network);
5631 if (service->ipconfig_ipv4 == NULL)
5632 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5634 if (service->ipconfig_ipv6 == NULL)
5635 setup_ip6config(service, index);
5637 service_register(service);
5639 if (service->favorite == TRUE) {
5640 device = connman_network_get_device(service->network);
5641 if (device && __connman_device_scanning(device) == FALSE)
5642 __connman_service_auto_connect();
5645 __connman_notifier_service_add(service, service->name);
5650 void __connman_service_update_from_network(struct connman_network *network)
5652 connman_bool_t need_sort = FALSE;
5653 struct connman_service *service;
5654 connman_uint8_t strength;
5655 connman_bool_t roaming;
5656 GSequenceIter *iter;
5658 connman_bool_t stats_enable;
5660 DBG("network %p", network);
5662 service = __connman_service_lookup_from_network(network);
5663 if (service == NULL)
5666 if (service->network == NULL)
5669 name = connman_network_get_string(service->network, "Name");
5670 if (g_strcmp0(service->name, name) != 0) {
5671 g_free(service->name);
5672 service->name = g_strdup(name);
5673 connman_dbus_property_changed_basic(service->path,
5674 CONNMAN_SERVICE_INTERFACE, "Name",
5675 DBUS_TYPE_STRING, &service->name);
5678 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5679 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5681 strength = connman_network_get_strength(service->network);
5682 if (strength == service->strength)
5685 service->strength = strength;
5688 strength_changed(service);
5691 roaming = connman_network_get_bool(service->network, "Roaming");
5692 if (roaming == service->roaming)
5695 stats_enable = stats_enabled(service);
5696 if (stats_enable == TRUE)
5697 stats_stop(service);
5699 service->roaming = roaming;
5702 if (stats_enable == TRUE)
5703 stats_start(service);
5705 roaming_changed(service);
5708 if (need_sort == TRUE) {
5709 iter = g_hash_table_lookup(service_hash, service->identifier);
5711 g_sequence_sort_changed(iter, service_compare, NULL);
5715 void __connman_service_remove_from_network(struct connman_network *network)
5717 struct connman_service *service;
5719 DBG("network %p", network);
5721 service = __connman_service_lookup_from_network(network);
5722 if (service == NULL)
5725 __connman_connection_gateway_remove(service,
5726 CONNMAN_IPCONFIG_TYPE_ALL);
5728 __connman_service_put(service);
5732 * __connman_service_create_from_provider:
5733 * @provider: provider structure
5735 * Look up service by provider and if not found, create one
5737 struct connman_service *
5738 __connman_service_create_from_provider(struct connman_provider *provider)
5740 struct connman_service *service;
5741 const char *ident, *str;
5743 int index = connman_provider_get_index(provider);
5745 DBG("provider %p", provider);
5747 ident = __connman_provider_get_ident(provider);
5751 name = g_strdup_printf("vpn_%s", ident);
5752 service = service_get(name);
5755 if (service == NULL)
5758 service->type = CONNMAN_SERVICE_TYPE_VPN;
5759 service->provider = connman_provider_ref(provider);
5760 service->autoconnect = FALSE;
5761 service->userconnect = TRUE;
5763 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5765 str = connman_provider_get_string(provider, "Name");
5767 g_free(service->name);
5768 service->name = g_strdup(str);
5769 service->hidden = FALSE;
5771 g_free(service->name);
5772 service->name = NULL;
5773 service->hidden = TRUE;
5776 service->strength = 0;
5778 if (service->ipconfig_ipv4 == NULL)
5779 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5781 if (service->ipconfig_ipv6 == NULL)
5782 setup_ip6config(service, index);
5784 service_register(service);
5786 __connman_notifier_service_add(service, service->name);
5791 int __connman_service_init(void)
5795 connection = connman_dbus_get_connection();
5797 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5800 service_list = g_sequence_new(service_free);
5805 void __connman_service_cleanup(void)
5811 list = service_list;
5812 service_list = NULL;
5813 g_sequence_free(list);
5815 g_hash_table_destroy(service_hash);
5816 service_hash = NULL;
5818 g_slist_free(counter_list);
5819 counter_list = NULL;
5821 dbus_connection_unref(connection);