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 DBusMessage *move_service(DBusConnection *conn,
3289 DBusMessage *msg, void *user_data,
3292 struct connman_service *service = user_data;
3293 struct connman_service *target;
3295 GSequenceIter *src, *dst;
3296 enum connman_ipconfig_method target4, target6;
3297 enum connman_ipconfig_method service4, service6;
3299 DBG("service %p", service);
3301 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3304 if (service->favorite == FALSE)
3305 return __connman_error_not_supported(msg);
3307 target = find_service(path);
3308 if (target == NULL || target->favorite == FALSE || target == service ||
3309 target->type == CONNMAN_SERVICE_TYPE_VPN)
3310 return __connman_error_invalid_service(msg);
3312 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3313 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3314 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3315 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3317 DBG("target %s method %d/%d state %d/%d", target->identifier,
3319 target->state_ipv4, target->state_ipv6);
3321 DBG("service %s method %d/%d state %d/%d", service->identifier,
3323 service->state_ipv4, service->state_ipv6);
3326 * If method is OFF, then we do not need to check the corresponding
3329 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3330 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3331 if (check_suitable_state(target->state_ipv6,
3332 service->state_ipv6) == FALSE)
3333 return __connman_error_invalid_service(msg);
3337 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3338 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3339 if (check_suitable_state(target->state_ipv4,
3340 service->state_ipv4) == FALSE)
3341 return __connman_error_invalid_service(msg);
3345 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3346 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3347 if (check_suitable_state(target->state_ipv6,
3348 service->state_ipv6) == FALSE)
3349 return __connman_error_invalid_service(msg);
3353 if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3354 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3355 if (check_suitable_state(target->state_ipv4,
3356 service->state_ipv4) == FALSE)
3357 return __connman_error_invalid_service(msg);
3361 g_get_current_time(&service->modified);
3362 service_save(service);
3364 src = g_hash_table_lookup(service_hash, service->identifier);
3365 dst = g_hash_table_lookup(service_hash, target->identifier);
3367 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
3369 services_changed(FALSE);
3371 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3374 static DBusMessage *move_before(DBusConnection *conn,
3375 DBusMessage *msg, void *user_data)
3377 return move_service(conn, msg, user_data, TRUE);
3380 static DBusMessage *move_after(DBusConnection *conn,
3381 DBusMessage *msg, void *user_data)
3383 return move_service(conn, msg, user_data, FALSE);
3386 static DBusMessage *reset_counters(DBusConnection *conn,
3387 DBusMessage *msg, void *user_data)
3389 struct connman_service *service = user_data;
3391 reset_stats(service);
3393 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3396 static GDBusMethodTable service_methods[] = {
3397 { "GetProperties", "", "a{sv}", get_properties },
3398 { "SetProperty", "sv", "", set_property },
3399 { "ClearProperty", "s", "", clear_property },
3400 { "Connect", "", "", connect_service,
3401 G_DBUS_METHOD_FLAG_ASYNC },
3402 { "Disconnect", "", "", disconnect_service },
3403 { "Remove", "", "", remove_service },
3404 { "MoveBefore", "o", "", move_before },
3405 { "MoveAfter", "o", "", move_after },
3406 { "ResetCounters", "", "", reset_counters },
3410 static GDBusSignalTable service_signals[] = {
3411 { "PropertyChanged", "sv" },
3415 static void service_free(gpointer user_data)
3417 struct connman_service *service = user_data;
3418 char *path = service->path;
3420 DBG("service %p", service);
3422 reply_pending(service, ENOENT);
3424 g_hash_table_remove(service_hash, service->identifier);
3426 __connman_notifier_service_remove(service);
3428 stats_stop(service);
3430 service->path = NULL;
3433 services_changed(FALSE);
3435 g_dbus_unregister_interface(connection, path,
3436 CONNMAN_SERVICE_INTERFACE);
3440 g_hash_table_destroy(service->counter_table);
3442 if (service->network != NULL) {
3443 if (service->network_created == TRUE)
3444 connman_network_unref(service->network);
3447 if (service->provider != NULL)
3448 connman_provider_unref(service->provider);
3450 if (service->ipconfig_ipv4 != NULL) {
3451 connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3452 connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3453 connman_ipconfig_unref(service->ipconfig_ipv4);
3454 service->ipconfig_ipv4 = NULL;
3457 if (service->ipconfig_ipv6 != NULL) {
3458 connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3459 connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3460 connman_ipconfig_unref(service->ipconfig_ipv6);
3461 service->ipconfig_ipv6 = NULL;
3464 g_strfreev(service->nameservers);
3465 g_strfreev(service->nameservers_config);
3466 g_strfreev(service->nameservers_auto);
3467 g_strfreev(service->domains);
3468 g_strfreev(service->proxies);
3469 g_strfreev(service->excludes);
3471 g_free(service->domainname);
3472 g_free(service->pac);
3473 g_free(service->name);
3474 g_free(service->passphrase);
3475 g_free(service->agent_passphrase);
3476 g_free(service->identifier);
3477 g_free(service->eap);
3478 g_free(service->identity);
3479 g_free(service->agent_identity);
3480 g_free(service->ca_cert_file);
3481 g_free(service->client_cert_file);
3482 g_free(service->private_key_file);
3483 g_free(service->private_key_passphrase);
3484 g_free(service->phase2);
3486 if (service->stats.timer != NULL)
3487 g_timer_destroy(service->stats.timer);
3488 if (service->stats_roaming.timer != NULL)
3489 g_timer_destroy(service->stats_roaming.timer);
3495 * __connman_service_put:
3496 * @service: service structure
3498 * Release service if no longer needed
3500 void __connman_service_put(struct connman_service *service)
3502 GSequenceIter *iter;
3504 DBG("service %p", service);
3506 if (__sync_fetch_and_sub(&service->refcount, 1) != 1)
3509 iter = g_hash_table_lookup(service_hash, service->identifier);
3511 reply_pending(service, ECONNABORTED);
3513 __connman_service_disconnect(service);
3515 g_sequence_remove(iter);
3517 service_free(service);
3521 static void stats_init(struct connman_service *service)
3524 service->stats.valid = FALSE;
3525 service->stats.enabled = FALSE;
3526 service->stats.timer = g_timer_new();
3529 service->stats_roaming.valid = FALSE;
3530 service->stats_roaming.enabled = FALSE;
3531 service->stats_roaming.timer = g_timer_new();
3534 static void service_initialize(struct connman_service *service)
3536 DBG("service %p", service);
3538 service->refcount = 1;
3539 service->session_usage_count = 0;
3541 service->network_created = FALSE;
3543 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3544 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3546 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3547 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3548 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3550 service->favorite = FALSE;
3551 service->immutable = FALSE;
3552 service->hidden = FALSE;
3554 service->ignore = FALSE;
3556 service->userconnect = FALSE;
3560 stats_init(service);
3562 service->provider = NULL;
3564 service->wps = FALSE;
3568 * connman_service_create:
3570 * Allocate a new service.
3572 * Returns: a newly-allocated #connman_service structure
3574 struct connman_service *connman_service_create(void)
3577 struct connman_stats_counter *counters;
3578 const char *counter;
3580 struct connman_service *service;
3582 service = g_try_new0(struct connman_service, 1);
3583 if (service == NULL)
3586 DBG("service %p", service);
3588 service->counter_table = g_hash_table_new_full(g_str_hash,
3589 g_str_equal, NULL, g_free);
3591 for (list = counter_list; list; list = list->next) {
3592 counter = list->data;
3594 counters = g_try_new0(struct connman_stats_counter, 1);
3595 if (counters == NULL) {
3596 g_hash_table_destroy(service->counter_table);
3601 counters->append_all = TRUE;
3603 g_hash_table_replace(service->counter_table, (gpointer)counter,
3607 service_initialize(service);
3613 * connman_service_ref:
3614 * @service: service structure
3616 * Increase reference counter of service
3618 struct connman_service *connman_service_ref(struct connman_service *service)
3622 __sync_fetch_and_add(&service->refcount, 1);
3628 * connman_service_unref:
3629 * @service: service structure
3631 * Decrease reference counter of service
3633 void connman_service_unref(struct connman_service *service)
3635 __connman_service_put(service);
3638 static gint service_compare(gconstpointer a, gconstpointer b,
3641 struct connman_service *service_a = (void *) a;
3642 struct connman_service *service_b = (void *) b;
3643 enum connman_service_state state_a, state_b;
3645 state_a = service_a->state;
3646 state_b = service_b->state;
3648 if (state_a != state_b) {
3649 gboolean a_connected = is_connected(service_a);
3650 gboolean b_connected = is_connected(service_b);
3652 if (a_connected == TRUE && b_connected == TRUE) {
3653 /* We prefer online over ready state */
3654 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
3657 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
3661 if (a_connected == TRUE)
3663 if (b_connected == TRUE)
3666 if (is_connecting(service_a) == TRUE)
3668 if (is_connecting(service_b) == TRUE)
3672 if (service_a->order > service_b->order)
3675 if (service_a->order < service_b->order)
3678 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3681 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3684 if (service_a->type != service_b->type) {
3685 switch (service_a->type) {
3686 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3687 case CONNMAN_SERVICE_TYPE_SYSTEM:
3688 case CONNMAN_SERVICE_TYPE_ETHERNET:
3689 case CONNMAN_SERVICE_TYPE_GPS:
3690 case CONNMAN_SERVICE_TYPE_VPN:
3691 case CONNMAN_SERVICE_TYPE_GADGET:
3693 case CONNMAN_SERVICE_TYPE_WIFI:
3695 case CONNMAN_SERVICE_TYPE_WIMAX:
3696 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3697 case CONNMAN_SERVICE_TYPE_CELLULAR:
3702 return (gint) service_b->strength - (gint) service_a->strength;
3706 * connman_service_get_type:
3707 * @service: service structure
3709 * Get the type of service
3711 enum connman_service_type connman_service_get_type(struct connman_service *service)
3713 if (service == NULL)
3714 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3716 return service->type;
3720 * connman_service_get_interface:
3721 * @service: service structure
3723 * Get network interface of service
3725 char *connman_service_get_interface(struct connman_service *service)
3729 if (service == NULL)
3732 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3733 if (service->ipconfig_ipv4)
3734 index = connman_ipconfig_get_index(
3735 service->ipconfig_ipv4);
3736 else if (service->ipconfig_ipv6)
3737 index = connman_ipconfig_get_index(
3738 service->ipconfig_ipv6);
3742 return connman_inet_ifname(index);
3745 if (service->network == NULL)
3748 index = connman_network_get_index(service->network);
3750 return connman_inet_ifname(index);
3754 * connman_service_get_network:
3755 * @service: service structure
3757 * Get the service network
3759 struct connman_network *
3760 __connman_service_get_network(struct connman_service *service)
3762 if (service == NULL)
3765 return service->network;
3768 struct connman_ipconfig *
3769 __connman_service_get_ip4config(struct connman_service *service)
3771 if (service == NULL)
3774 return service->ipconfig_ipv4;
3777 struct connman_ipconfig *
3778 __connman_service_get_ip6config(struct connman_service *service)
3780 if (service == NULL)
3783 return service->ipconfig_ipv6;
3786 struct connman_ipconfig *
3787 __connman_service_get_ipconfig(struct connman_service *service, int family)
3789 if (family == AF_INET)
3790 return __connman_service_get_ip4config(service);
3791 else if (family == AF_INET6)
3792 return __connman_service_get_ip6config(service);
3798 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3800 if (service == NULL)
3801 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3803 return service->security;
3806 const char *__connman_service_get_phase2(struct connman_service *service)
3808 if (service == NULL)
3811 return service->phase2;
3814 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3816 if (service == NULL)
3819 return service->wps;
3823 * __connman_service_set_favorite:
3824 * @service: service structure
3825 * @favorite: favorite value
3827 * Change the favorite setting of service
3829 int __connman_service_set_favorite(struct connman_service *service,
3830 connman_bool_t favorite)
3832 GSequenceIter *iter;
3834 iter = g_hash_table_lookup(service_hash, service->identifier);
3838 if (service->favorite == favorite)
3841 service->favorite = favorite;
3842 service->order = __connman_service_get_order(service);
3844 favorite_changed(service);
3846 g_sequence_sort_changed(iter, service_compare, NULL);
3848 services_changed(FALSE);
3853 int __connman_service_set_immutable(struct connman_service *service,
3854 connman_bool_t immutable)
3856 service->immutable = immutable;
3858 immutable_changed(service);
3863 void __connman_service_set_string(struct connman_service *service,
3864 const char *key, const char *value)
3866 if (g_str_equal(key, "EAP") == TRUE) {
3867 g_free(service->eap);
3868 service->eap = g_strdup(value);
3869 } else if (g_str_equal(key, "Identity") == TRUE) {
3870 g_free(service->identity);
3871 service->identity = g_strdup(value);
3872 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3873 g_free(service->ca_cert_file);
3874 service->ca_cert_file = g_strdup(value);
3875 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3876 g_free(service->client_cert_file);
3877 service->client_cert_file = g_strdup(value);
3878 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3879 g_free(service->private_key_file);
3880 service->private_key_file = g_strdup(value);
3881 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3882 g_free(service->private_key_passphrase);
3883 service->private_key_passphrase = g_strdup(value);
3884 } else if (g_str_equal(key, "Phase2") == TRUE) {
3885 g_free(service->phase2);
3886 service->phase2 = g_strdup(value);
3887 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3888 g_free(service->passphrase);
3889 service->passphrase = g_strdup(value);
3893 static void service_complete(struct connman_service *service)
3895 reply_pending(service, EIO);
3897 if (service->userconnect == FALSE)
3898 __connman_service_auto_connect();
3900 g_get_current_time(&service->modified);
3901 service_save(service);
3904 static void report_error_cb(struct connman_service *service,
3905 gboolean retry, void *user_data)
3908 __connman_service_connect(service);
3910 service_complete(service);
3911 services_changed(FALSE);
3912 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3916 static void request_input_cb (struct connman_service *service,
3917 const char *identity, const char *passphrase,
3920 DBG ("RequestInput return, %p", service);
3922 if (identity == NULL && passphrase == NULL && service->wps == FALSE) {
3923 service_complete(service);
3924 services_changed(FALSE);
3925 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3929 if (identity != NULL)
3930 __connman_service_set_agent_identity(service, identity);
3932 if (passphrase != NULL) {
3933 switch (service->security) {
3934 case CONNMAN_SERVICE_SECURITY_WEP:
3935 case CONNMAN_SERVICE_SECURITY_PSK:
3936 __connman_service_set_passphrase(service, passphrase);
3938 case CONNMAN_SERVICE_SECURITY_8021X:
3939 __connman_service_set_agent_passphrase(service,
3942 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3943 case CONNMAN_SERVICE_SECURITY_NONE:
3944 case CONNMAN_SERVICE_SECURITY_WPA:
3945 case CONNMAN_SERVICE_SECURITY_RSN:
3946 DBG("service security '%s' not handled",
3947 security2string(service->security));
3952 __connman_service_connect(service);
3954 /* Never cache agent provided credentials */
3955 __connman_service_set_agent_identity(service, NULL);
3956 __connman_service_set_agent_passphrase(service, NULL);
3959 static int service_indicate_state(struct connman_service *service)
3961 enum connman_service_state old_state, new_state;
3962 GSequenceIter *iter;
3964 if (service == NULL)
3967 old_state = service->state;
3968 new_state = combine_state(service->state_ipv4, service->state_ipv6);
3970 DBG("service %p old %s - new %s/%s => %s",
3972 state2string(old_state),
3973 state2string(service->state_ipv4),
3974 state2string(service->state_ipv6),
3975 state2string(new_state));
3977 if (old_state == new_state)
3980 service->state = new_state;
3981 state_changed(service);
3983 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
3984 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3985 reply_pending(service, ECONNABORTED);
3987 __connman_service_disconnect(service);
3990 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3991 if (__connman_stats_service_register(service) == 0) {
3992 __connman_stats_get(service, FALSE,
3993 &service->stats.data);
3994 __connman_stats_get(service, TRUE,
3995 &service->stats_roaming.data);
3999 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
4000 if (service->login_required == TRUE) {
4001 service->login_required = FALSE;
4002 login_changed(service);
4005 connman_timeserver_sync();
4008 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
4009 connman_bool_t reconnect;
4011 reconnect = get_reconnect_state(service);
4012 if (reconnect == TRUE)
4013 __connman_service_auto_connect();
4015 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4018 if (new_state == CONNMAN_SERVICE_STATE_READY) {
4019 enum connman_ipconfig_method method;
4021 set_reconnect_state(service, TRUE);
4023 __connman_service_set_favorite(service, TRUE);
4025 reply_pending(service, 0);
4027 service->userconnect = FALSE;
4029 g_get_current_time(&service->modified);
4030 service_save(service);
4032 update_nameservers(service);
4033 dns_changed(service);
4034 domain_changed(service);
4036 __connman_notifier_connect(service->type);
4038 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
4039 connman_network_get_bool(service->network,
4040 "WiFi.UseWPS") == TRUE) {
4043 pass = connman_network_get_string(service->network,
4046 __connman_service_set_passphrase(service, pass);
4048 connman_network_set_bool(service->network,
4049 "WiFi.UseWPS", FALSE);
4054 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4055 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4056 __connman_ipconfig_disable_ipv6(
4057 service->ipconfig_ipv6);
4059 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4060 struct connman_service *def_service = get_default();
4062 if (__connman_notifier_count_connected() == 0 &&
4063 def_service != NULL &&
4064 def_service->provider != NULL)
4065 __connman_provider_disconnect(def_service->provider);
4069 __connman_wispr_stop(service);
4071 __connman_wpad_stop(service);
4073 update_nameservers(service);
4074 dns_changed(service);
4075 domain_changed(service);
4077 __connman_notifier_disconnect(service->type);
4080 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4081 if (service->userconnect == TRUE &&
4082 __connman_agent_report_error(service,
4083 error2string(service->error),
4084 report_error_cb, NULL) == -EIO)
4086 service_complete(service);
4088 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4090 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4092 iter = g_hash_table_lookup(service_hash, service->identifier);
4094 g_sequence_sort_changed(iter, service_compare, NULL);
4096 services_changed(FALSE);
4098 if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
4104 int __connman_service_indicate_error(struct connman_service *service,
4105 enum connman_service_error error)
4107 DBG("service %p error %d", service, error);
4109 if (service == NULL)
4112 service->error = error;
4114 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4115 __connman_service_set_passphrase(service, NULL);
4117 __connman_service_ipconfig_indicate_state(service,
4118 CONNMAN_SERVICE_STATE_FAILURE,
4119 CONNMAN_IPCONFIG_TYPE_IPV4);
4120 __connman_service_ipconfig_indicate_state(service,
4121 CONNMAN_SERVICE_STATE_FAILURE,
4122 CONNMAN_IPCONFIG_TYPE_IPV6);
4126 int __connman_service_clear_error(struct connman_service *service)
4128 DBG("service %p", service);
4130 if (service == NULL)
4133 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4136 service->state_ipv4 = service->state_ipv6 =
4137 CONNMAN_SERVICE_STATE_UNKNOWN;
4138 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
4140 if (service->favorite == TRUE)
4141 set_reconnect_state(service, TRUE);
4143 __connman_service_ipconfig_indicate_state(service,
4144 CONNMAN_SERVICE_STATE_IDLE,
4145 CONNMAN_IPCONFIG_TYPE_IPV6);
4148 * Toggling the IPv6 state to IDLE could trigger the auto connect
4149 * machinery and consequently the IPv4 state.
4151 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4152 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4155 return __connman_service_ipconfig_indicate_state(service,
4156 CONNMAN_SERVICE_STATE_IDLE,
4157 CONNMAN_IPCONFIG_TYPE_IPV4);
4160 int __connman_service_indicate_default(struct connman_service *service)
4162 DBG("service %p", service);
4169 enum connman_service_state __connman_service_ipconfig_get_state(
4170 struct connman_service *service,
4171 enum connman_ipconfig_type type)
4173 if (service == NULL)
4174 return CONNMAN_SERVICE_STATE_UNKNOWN;
4176 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4177 return service->state_ipv4;
4179 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4180 return service->state_ipv6;
4182 return CONNMAN_SERVICE_STATE_UNKNOWN;
4185 static void check_proxy_setup(struct connman_service *service)
4188 * We start WPAD if we haven't got a PAC URL from DHCP and
4189 * if our proxy manual configuration is either empty or set
4190 * to AUTO with an empty URL.
4193 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4196 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4197 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4198 service->pac != NULL))
4201 if (__connman_wpad_start(service) < 0) {
4202 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4203 __connman_notifier_proxy_changed(service);
4210 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
4214 * How many networks are connected at the same time. If more than 1,
4215 * then set the rp_filter setting properly (loose mode routing) so that network
4216 * connectivity works ok. This is only done for IPv4 networks as IPv6
4217 * does not have rp_filter knob.
4219 static int connected_networks_count;
4220 static int original_rp_filter;
4222 static void service_rp_filter(struct connman_service *service,
4225 enum connman_ipconfig_method method;
4227 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4230 case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
4231 case CONNMAN_IPCONFIG_METHOD_OFF:
4232 case CONNMAN_IPCONFIG_METHOD_AUTO:
4234 case CONNMAN_IPCONFIG_METHOD_FIXED:
4235 case CONNMAN_IPCONFIG_METHOD_MANUAL:
4236 case CONNMAN_IPCONFIG_METHOD_DHCP:
4240 if (connected == TRUE) {
4241 if (connected_networks_count == 1) {
4243 filter_value = __connman_ipconfig_set_rp_filter();
4244 if (filter_value < 0)
4247 original_rp_filter = filter_value;
4249 connected_networks_count++;
4252 if (connected_networks_count == 2)
4253 __connman_ipconfig_unset_rp_filter(original_rp_filter);
4255 connected_networks_count--;
4256 if (connected_networks_count < 0)
4257 connected_networks_count = 0;
4260 DBG("%s %s ipconfig %p method %d count %d filter %d",
4261 connected ? "connected" : "disconnected", service->identifier,
4262 service->ipconfig_ipv4, method,
4263 connected_networks_count, original_rp_filter);
4266 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
4267 enum connman_service_state new_state,
4268 enum connman_ipconfig_type type)
4270 struct connman_ipconfig *ipconfig = NULL;
4271 enum connman_service_state old_state;
4274 if (service == NULL)
4277 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4278 old_state = service->state_ipv4;
4279 ipconfig = service->ipconfig_ipv4;
4280 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4281 old_state = service->state_ipv6;
4282 ipconfig = service->ipconfig_ipv6;
4285 if (ipconfig == NULL)
4289 if (old_state == new_state)
4292 DBG("service %p (%s) state %d (%s) type %d (%s)",
4293 service, service ? service->identifier : NULL,
4294 new_state, state2string(new_state),
4295 type, __connman_ipconfig_type2string(type));
4297 switch (new_state) {
4298 case CONNMAN_SERVICE_STATE_UNKNOWN:
4299 case CONNMAN_SERVICE_STATE_IDLE:
4300 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
4303 case CONNMAN_SERVICE_STATE_ASSOCIATION:
4305 case CONNMAN_SERVICE_STATE_CONFIGURATION:
4306 __connman_ipconfig_enable(ipconfig);
4308 case CONNMAN_SERVICE_STATE_READY:
4309 update_nameservers(service);
4311 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4312 check_proxy_setup(service);
4313 service_rp_filter(service, TRUE);
4315 __connman_wispr_start(service, type);
4317 case CONNMAN_SERVICE_STATE_ONLINE:
4319 case CONNMAN_SERVICE_STATE_DISCONNECT:
4320 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
4323 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4324 service_rp_filter(service, FALSE);
4327 case CONNMAN_SERVICE_STATE_FAILURE:
4331 /* We keep that state */
4332 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4333 service->state_ipv4 = new_state;
4334 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4335 service->state_ipv6 = new_state;
4337 ret = service_indicate_state(service);
4340 * If the ipconfig method is OFF, then we set the state to IDLE
4341 * so that it will not affect the combined state in the future.
4343 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4344 enum connman_ipconfig_method method;
4345 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4346 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4347 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4348 service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
4349 ret = service_indicate_state(service);
4352 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4353 enum connman_ipconfig_method method;
4354 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4355 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4356 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4357 service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4358 ret = service_indicate_state(service);
4365 int __connman_service_request_login(struct connman_service *service)
4367 DBG("service %p", service);
4369 if (service == NULL)
4372 service->login_required = TRUE;
4373 login_changed(service);
4378 static connman_bool_t prepare_network(struct connman_service *service)
4380 enum connman_network_type type;
4381 unsigned int ssid_len;
4383 type = connman_network_get_type(service->network);
4386 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4387 case CONNMAN_NETWORK_TYPE_VENDOR:
4389 case CONNMAN_NETWORK_TYPE_WIFI:
4390 if (connman_network_get_blob(service->network, "WiFi.SSID",
4394 if (service->passphrase != NULL)
4395 connman_network_set_string(service->network,
4396 "WiFi.Passphrase", service->passphrase);
4398 case CONNMAN_NETWORK_TYPE_ETHERNET:
4399 case CONNMAN_NETWORK_TYPE_WIMAX:
4400 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4401 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4402 case CONNMAN_NETWORK_TYPE_CELLULAR:
4409 static void prepare_8021x(struct connman_service *service)
4411 if (service->eap != NULL)
4412 connman_network_set_string(service->network, "WiFi.EAP",
4415 if (service->identity != NULL)
4416 connman_network_set_string(service->network, "WiFi.Identity",
4419 if (service->ca_cert_file != NULL)
4420 connman_network_set_string(service->network, "WiFi.CACertFile",
4421 service->ca_cert_file);
4423 if (service->client_cert_file != NULL)
4424 connman_network_set_string(service->network,
4425 "WiFi.ClientCertFile",
4426 service->client_cert_file);
4428 if (service->private_key_file != NULL)
4429 connman_network_set_string(service->network,
4430 "WiFi.PrivateKeyFile",
4431 service->private_key_file);
4433 if (service->private_key_passphrase != NULL)
4434 connman_network_set_string(service->network,
4435 "WiFi.PrivateKeyPassphrase",
4436 service->private_key_passphrase);
4438 if (service->phase2 != NULL)
4439 connman_network_set_string(service->network, "WiFi.Phase2",
4443 static int service_connect(struct connman_service *service)
4447 switch (service->type) {
4448 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4449 case CONNMAN_SERVICE_TYPE_SYSTEM:
4450 case CONNMAN_SERVICE_TYPE_GPS:
4451 case CONNMAN_SERVICE_TYPE_GADGET:
4453 case CONNMAN_SERVICE_TYPE_ETHERNET:
4454 case CONNMAN_SERVICE_TYPE_WIMAX:
4455 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4456 case CONNMAN_SERVICE_TYPE_CELLULAR:
4457 case CONNMAN_SERVICE_TYPE_VPN:
4459 case CONNMAN_SERVICE_TYPE_WIFI:
4460 switch (service->security) {
4461 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4462 case CONNMAN_SERVICE_SECURITY_NONE:
4464 case CONNMAN_SERVICE_SECURITY_WEP:
4465 case CONNMAN_SERVICE_SECURITY_PSK:
4466 case CONNMAN_SERVICE_SECURITY_WPA:
4467 case CONNMAN_SERVICE_SECURITY_RSN:
4468 if (service->passphrase == NULL) {
4469 if (service->network == NULL)
4472 if (service->wps == FALSE ||
4473 connman_network_get_bool(
4475 "WiFi.UseWPS") == FALSE)
4479 case CONNMAN_SERVICE_SECURITY_8021X:
4480 if (service->eap == NULL)
4484 * never request credentials if using EAP-TLS
4485 * (EAP-TLS networks need to be fully provisioned)
4487 if (g_str_equal(service->eap, "tls") == TRUE)
4491 * Return -ENOKEY if either identity or passphrase is
4492 * missing. Agent provided credentials can be used as
4493 * fallback if needed.
4495 if ((service->identity == NULL &&
4496 service->agent_identity == NULL) ||
4497 (service->passphrase == NULL &&
4498 service->agent_passphrase == NULL))
4506 if (service->network != NULL) {
4507 if (prepare_network(service) == FALSE)
4510 switch (service->security) {
4511 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4512 case CONNMAN_SERVICE_SECURITY_NONE:
4513 case CONNMAN_SERVICE_SECURITY_WEP:
4514 case CONNMAN_SERVICE_SECURITY_PSK:
4515 case CONNMAN_SERVICE_SECURITY_WPA:
4516 case CONNMAN_SERVICE_SECURITY_RSN:
4518 case CONNMAN_SERVICE_SECURITY_8021X:
4519 prepare_8021x(service);
4523 if (__connman_stats_service_register(service) == 0) {
4524 __connman_stats_get(service, FALSE,
4525 &service->stats.data);
4526 __connman_stats_get(service, TRUE,
4527 &service->stats_roaming.data);
4530 if (service->ipconfig_ipv4)
4531 __connman_ipconfig_enable(service->ipconfig_ipv4);
4532 if (service->ipconfig_ipv6)
4533 __connman_ipconfig_enable(service->ipconfig_ipv6);
4535 err = __connman_network_connect(service->network);
4536 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4537 service->provider != NULL)
4538 err = __connman_provider_connect(service->provider);
4543 if (err != -EINPROGRESS) {
4544 __connman_ipconfig_disable(service->ipconfig_ipv4);
4545 __connman_ipconfig_disable(service->ipconfig_ipv6);
4546 __connman_stats_service_unregister(service);
4554 int __connman_service_connect(struct connman_service *service)
4558 DBG("service %p state %s", service, state2string(service->state));
4560 if (is_connected(service) == TRUE)
4563 if (is_connecting(service) == TRUE)
4566 switch (service->type) {
4567 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4568 case CONNMAN_SERVICE_TYPE_SYSTEM:
4569 case CONNMAN_SERVICE_TYPE_GPS:
4570 case CONNMAN_SERVICE_TYPE_GADGET:
4573 err = service_connect(service);
4579 if (err == -EINPROGRESS) {
4580 if (service->timeout == 0)
4581 service->timeout = g_timeout_add_seconds(
4582 CONNECT_TIMEOUT, connect_timeout, service);
4584 return -EINPROGRESS;
4587 __connman_service_ipconfig_indicate_state(service,
4588 CONNMAN_SERVICE_STATE_FAILURE,
4589 CONNMAN_IPCONFIG_TYPE_IPV4);
4590 __connman_service_ipconfig_indicate_state(service,
4591 CONNMAN_SERVICE_STATE_FAILURE,
4592 CONNMAN_IPCONFIG_TYPE_IPV6);
4594 if (service->network != NULL)
4595 __connman_network_disconnect(service->network);
4596 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4597 service->provider != NULL)
4598 __connman_provider_disconnect(service->provider);
4600 if (service->userconnect == TRUE) {
4601 if (err == -ENOKEY) {
4602 if (__connman_agent_request_passphrase_input(service,
4605 return -EINPROGRESS;
4607 reply_pending(service, err);
4613 int __connman_service_disconnect(struct connman_service *service)
4617 DBG("service %p", service);
4619 if (service->network != NULL) {
4620 err = __connman_network_disconnect(service->network);
4621 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4622 service->provider != NULL)
4623 err = __connman_provider_disconnect(service->provider);
4627 if (err < 0 && err != -EINPROGRESS)
4630 __connman_6to4_remove(service->ipconfig_ipv4);
4632 if (service->ipconfig_ipv4)
4633 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
4636 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
4639 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
4640 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
4642 __connman_ipconfig_disable(service->ipconfig_ipv4);
4643 __connman_ipconfig_disable(service->ipconfig_ipv6);
4645 __connman_stats_service_unregister(service);
4650 int __connman_service_disconnect_all(void)
4652 GSequenceIter *iter;
4653 GSList *services = NULL, *list;
4657 iter = g_sequence_get_begin_iter(service_list);
4659 while (g_sequence_iter_is_end(iter) == FALSE) {
4660 struct connman_service *service = g_sequence_get(iter);
4662 services = g_slist_prepend(services, service);
4664 iter = g_sequence_iter_next(iter);
4667 for (list = services; list != NULL; list = list->next) {
4668 struct connman_service *service = list->data;
4670 service->ignore = TRUE;
4672 set_reconnect_state(service, FALSE);
4674 __connman_service_disconnect(service);
4684 * __connman_service_lookup:
4685 * @pattern: search pattern
4686 * @path: return object path
4688 * Look up a service path from a search pattern
4690 int __connman_service_lookup(const char *pattern, const char **path)
4692 GHashTableIter iter;
4693 gpointer key, value;
4694 struct connman_device *device;
4697 g_hash_table_iter_init(&iter, service_hash);
4699 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4700 GSequenceIter *iter = value;
4701 struct connman_service *service = g_sequence_get(iter);
4703 if (g_strcmp0(service->identifier, pattern) == 0 ||
4704 g_strcmp0(service->name, pattern) == 0) {
4705 *path = (const char *) service->path;
4709 if (service->network == NULL)
4712 device = connman_network_get_device(service->network);
4716 ifname = connman_device_get_string(device, "Interface");
4717 if (ifname != NULL && g_strcmp0(ifname, pattern) == 0) {
4718 *path = (const char *) service->path;
4728 * lookup_by_identifier:
4729 * @identifier: service identifier
4731 * Look up a service by identifier (reference count will not be increased)
4733 static struct connman_service *lookup_by_identifier(const char *identifier)
4735 GSequenceIter *iter;
4737 iter = g_hash_table_lookup(service_hash, identifier);
4739 return g_sequence_get(iter);
4744 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4745 const char *ssid, const char *mode, const char *security,
4748 struct connman_network *network;
4751 unsigned int i, ssid_len;
4753 ssid_len = strlen(ssid);
4757 network = connman_network_create(group, CONNMAN_NETWORK_TYPE_WIFI);
4758 if (network == NULL)
4761 connman_network_set_blob(network, "WiFi.SSID",
4762 (unsigned char *) ssid, ssid_len);
4764 connman_network_set_string(network, "WiFi.Mode", mode);
4765 connman_network_set_string(network, "WiFi.Security", security);
4767 name = g_try_malloc0(ssid_len + 1);
4769 connman_network_unref(network);
4773 for (i = 0; i < ssid_len; i++) {
4774 if (g_ascii_isprint(ssid[i]))
4780 connman_network_set_name(network, name);
4784 index = connman_device_get_index(device);
4785 connman_network_set_index(network, index);
4787 if (connman_device_add_network(device, network) < 0) {
4788 connman_network_unref(network);
4792 connman_network_set_available(network, TRUE);
4797 int __connman_service_create_and_connect(DBusMessage *msg)
4799 struct connman_service *service;
4800 struct connman_network *network;
4801 struct connman_device *device;
4802 DBusMessageIter iter, array;
4803 const char *mode = "managed", *security = "none", *group_security;
4804 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4805 connman_bool_t network_created = FALSE;
4806 unsigned int ssid_len = 0;
4811 dbus_message_iter_init(msg, &iter);
4812 dbus_message_iter_recurse(&iter, &array);
4814 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4815 DBusMessageIter entry, value;
4818 dbus_message_iter_recurse(&array, &entry);
4819 dbus_message_iter_get_basic(&entry, &key);
4821 dbus_message_iter_next(&entry);
4822 dbus_message_iter_recurse(&entry, &value);
4824 switch (dbus_message_iter_get_arg_type(&value)) {
4825 case DBUS_TYPE_STRING:
4826 if (g_str_equal(key, "Type") == TRUE)
4827 dbus_message_iter_get_basic(&value, &type);
4828 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4829 g_str_equal(key, "Mode") == TRUE)
4830 dbus_message_iter_get_basic(&value, &mode);
4831 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4832 g_str_equal(key, "Security") == TRUE)
4833 dbus_message_iter_get_basic(&value, &security);
4834 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4835 g_str_equal(key, "Passphrase") == TRUE)
4836 dbus_message_iter_get_basic(&value, &passphrase);
4837 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4838 g_str_equal(key, "SSID") == TRUE)
4839 dbus_message_iter_get_basic(&value, &ssid);
4842 dbus_message_iter_next(&array);
4848 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4854 ssid_len = strlen(ssid);
4858 if (g_strcmp0(security, "none") != 0 &&
4859 g_strcmp0(security, "wep") != 0 &&
4860 g_strcmp0(security, "psk") != 0 &&
4861 g_strcmp0(security, "wpa") != 0 &&
4862 g_strcmp0(security, "rsn") != 0 &&
4863 g_strcmp0(security, "ieee8021x") != 0)
4866 device = __connman_device_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4870 ident = connman_device_get_ident(device);
4875 if (!g_strcmp0(security, "wpa") ||
4876 !g_strcmp0(security, "rsn"))
4877 group_security = "psk";
4879 group_security = security;
4881 group = wifi_build_group_name((unsigned char *) ssid,
4882 ssid_len, mode, group_security);
4886 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4888 service = lookup_by_identifier(name);
4890 if (service == NULL) {
4891 network = create_hidden_wifi(device, ssid,
4892 mode, security, group);
4893 if (network != NULL) {
4894 connman_network_set_group(network, group);
4895 network_created = TRUE;
4898 service = lookup_by_identifier(name);
4904 if (service == NULL) {
4909 service->network_created = network_created;
4911 if (is_connected(service) == TRUE) {
4916 if (is_connecting(service) == TRUE) {
4921 set_reconnect_state(service, FALSE);
4923 __connman_device_disconnect(device);
4925 if (passphrase != NULL) {
4926 g_free(service->passphrase);
4927 service->passphrase = g_strdup(passphrase);
4930 service->userconnect = TRUE;
4932 err = __connman_service_connect(service);
4933 if (err < 0 && err != -EINPROGRESS)
4936 service->pending = dbus_message_ref(msg);
4941 if (service != NULL && service->network_created == TRUE) {
4942 struct connman_network *network = service->network;
4944 if (network != NULL) {
4945 connman_network_set_available(network, FALSE);
4946 __connman_device_cleanup_networks(device);
4948 __connman_service_put(service);
4954 static void provision_changed(gpointer value, gpointer user_data)
4956 struct connman_service *service = value;
4957 char *path = user_data;
4959 __connman_config_provision_service_ident(service, path);
4962 void __connman_service_provision_changed(const char *ident)
4964 g_sequence_foreach(service_list, provision_changed, (void *)ident);
4967 int __connman_service_provision(DBusMessage *msg)
4969 GKeyFile *keyfile = NULL;
4970 const char *config_str = NULL;
4971 char *group = NULL, *ident = NULL;
4973 struct connman_service *service;
4977 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
4980 if (config_str == NULL || strlen(config_str) == 0)
4983 keyfile = g_key_file_new();
4985 /* populate GKeyFile with config_str */
4986 if (g_key_file_load_from_data(keyfile, config_str,
4987 strlen(config_str), 0, NULL) == FALSE) {
4993 * read only one group of settings (only one service supported, no
4996 group = g_key_file_get_start_group(keyfile);
4998 if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
5003 err = __connman_config_load_service(keyfile, group, TRUE);
5007 ident = group + strlen("service_");
5009 /* trigger service provisioning if service exists */
5010 service = lookup_by_identifier(ident);
5011 if (service != NULL)
5012 __connman_config_provision_service(service);
5014 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
5020 if (keyfile != NULL)
5021 g_key_file_free(keyfile);
5027 * __connman_service_get:
5028 * @identifier: service identifier
5030 * Look up a service by identifier or create a new one if not found
5032 static struct connman_service *service_get(const char *identifier)
5034 struct connman_service *service;
5035 GSequenceIter *iter;
5037 iter = g_hash_table_lookup(service_hash, identifier);
5039 service = g_sequence_get(iter);
5040 if (service != NULL)
5041 connman_service_ref(service);
5045 service = connman_service_create();
5046 if (service == NULL)
5049 DBG("service %p", service);
5051 service->identifier = g_strdup(identifier);
5053 iter = g_sequence_insert_sorted(service_list, service,
5054 service_compare, NULL);
5056 g_hash_table_insert(service_hash, service->identifier, iter);
5061 static int service_register(struct connman_service *service)
5063 GSequenceIter *iter;
5065 DBG("service %p", service);
5067 if (service->path != NULL)
5070 service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
5071 service->identifier);
5073 DBG("path %s", service->path);
5075 __connman_config_provision_service(service);
5077 service_load(service);
5079 g_dbus_register_interface(connection, service->path,
5080 CONNMAN_SERVICE_INTERFACE,
5081 service_methods, service_signals,
5082 NULL, service, NULL);
5084 iter = g_hash_table_lookup(service_hash, service->identifier);
5086 g_sequence_sort_changed(iter, service_compare, NULL);
5088 services_changed(TRUE);
5093 static void service_up(struct connman_ipconfig *ipconfig)
5095 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5097 DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
5099 link_changed(service);
5101 service->stats.valid = FALSE;
5102 service->stats_roaming.valid = FALSE;
5105 static void service_down(struct connman_ipconfig *ipconfig)
5107 DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
5110 static void service_lower_up(struct connman_ipconfig *ipconfig)
5112 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5114 DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
5116 stats_start(service);
5119 static void service_lower_down(struct connman_ipconfig *ipconfig)
5121 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5123 DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
5125 stats_stop(service);
5126 service_save(service);
5129 static void service_ip_bound(struct connman_ipconfig *ipconfig)
5131 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5132 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5133 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5135 DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
5137 type = __connman_ipconfig_get_config_type(ipconfig);
5138 method = __connman_ipconfig_get_method(ipconfig);
5140 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5143 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5144 method == CONNMAN_IPCONFIG_METHOD_AUTO)
5145 __connman_service_ipconfig_indicate_state(service,
5146 CONNMAN_SERVICE_STATE_READY,
5147 CONNMAN_IPCONFIG_TYPE_IPV6);
5149 settings_changed(service, ipconfig);
5152 static void service_ip_release(struct connman_ipconfig *ipconfig)
5154 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5155 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5156 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5158 DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
5160 type = __connman_ipconfig_get_config_type(ipconfig);
5161 method = __connman_ipconfig_get_method(ipconfig);
5163 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5166 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5167 method == CONNMAN_IPCONFIG_METHOD_OFF)
5168 __connman_service_ipconfig_indicate_state(service,
5169 CONNMAN_SERVICE_STATE_DISCONNECT,
5170 CONNMAN_IPCONFIG_TYPE_IPV6);
5172 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
5173 method == CONNMAN_IPCONFIG_METHOD_OFF)
5174 __connman_service_ipconfig_indicate_state(service,
5175 CONNMAN_SERVICE_STATE_DISCONNECT,
5176 CONNMAN_IPCONFIG_TYPE_IPV4);
5178 settings_changed(service, ipconfig);
5181 static const struct connman_ipconfig_ops service_ops = {
5183 .down = service_down,
5184 .lower_up = service_lower_up,
5185 .lower_down = service_lower_down,
5186 .ip_bound = service_ip_bound,
5187 .ip_release = service_ip_release,
5190 static void setup_ip4config(struct connman_service *service, int index,
5191 enum connman_ipconfig_method method)
5196 service->ipconfig_ipv4 = connman_ipconfig_create(index,
5197 CONNMAN_IPCONFIG_TYPE_IPV4);
5198 if (service->ipconfig_ipv4 == NULL)
5201 connman_ipconfig_set_method(service->ipconfig_ipv4, method);
5203 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
5205 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
5208 static void setup_ip6config(struct connman_service *service, int index)
5213 service->ipconfig_ipv6 = connman_ipconfig_create(index,
5214 CONNMAN_IPCONFIG_TYPE_IPV6);
5215 if (service->ipconfig_ipv6 == NULL)
5218 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
5220 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
5223 void __connman_service_read_ip4config(struct connman_service *service)
5227 if (service->ipconfig_ipv4 == NULL)
5230 keyfile = connman_storage_load_service(service->identifier);
5231 if (keyfile == NULL)
5234 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5235 service->identifier, "IPv4.");
5237 g_key_file_free(keyfile);
5240 void __connman_service_create_ip4config(struct connman_service *service,
5243 DBG("ipv4 %p", service->ipconfig_ipv4);
5245 if (service->ipconfig_ipv4 != NULL)
5248 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5249 __connman_service_read_ip4config(service);
5252 void __connman_service_read_ip6config(struct connman_service *service)
5256 if (service->ipconfig_ipv6 == NULL)
5259 keyfile = connman_storage_load_service(service->identifier);
5260 if (keyfile == NULL)
5263 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5264 service->identifier, "IPv6.");
5266 g_key_file_free(keyfile);
5269 void __connman_service_create_ip6config(struct connman_service *service,
5272 DBG("ipv6 %p", service->ipconfig_ipv6);
5274 if (service->ipconfig_ipv6 != NULL)
5277 setup_ip6config(service, index);
5279 __connman_service_read_ip6config(service);
5283 * __connman_service_lookup_from_network:
5284 * @network: network structure
5286 * Look up a service by network (reference count will not be increased)
5288 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
5290 struct connman_service *service;
5291 const char *ident, *group;
5294 DBG("network %p", network);
5296 if (network == NULL)
5299 ident = __connman_network_get_ident(network);
5303 group = connman_network_get_group(network);
5307 name = g_strdup_printf("%s_%s_%s",
5308 __connman_network_get_type(network), ident, group);
5309 service = lookup_by_identifier(name);
5315 struct connman_service *__connman_service_lookup_from_index(int index)
5317 struct connman_service *service;
5318 GSequenceIter *iter;
5320 iter = g_sequence_get_begin_iter(service_list);
5322 while (g_sequence_iter_is_end(iter) == FALSE) {
5323 service = g_sequence_get(iter);
5325 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
5329 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
5333 iter = g_sequence_iter_next(iter);
5339 const char *__connman_service_get_ident(struct connman_service *service)
5341 return service->identifier;
5344 const char *__connman_service_get_path(struct connman_service *service)
5346 return service->path;
5349 unsigned int __connman_service_get_order(struct connman_service *service)
5351 GSequenceIter *iter;
5353 if (service == NULL)
5356 if (service->favorite == FALSE) {
5361 iter = g_hash_table_lookup(service_hash, service->identifier);
5363 if (g_sequence_iter_get_position(iter) == 0)
5365 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
5366 service->order = 10;
5372 return service->order;
5375 static enum connman_service_type convert_network_type(struct connman_network *network)
5377 enum connman_network_type type = connman_network_get_type(network);
5380 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5381 case CONNMAN_NETWORK_TYPE_VENDOR:
5383 case CONNMAN_NETWORK_TYPE_ETHERNET:
5384 return CONNMAN_SERVICE_TYPE_ETHERNET;
5385 case CONNMAN_NETWORK_TYPE_WIFI:
5386 return CONNMAN_SERVICE_TYPE_WIFI;
5387 case CONNMAN_NETWORK_TYPE_WIMAX:
5388 return CONNMAN_SERVICE_TYPE_WIMAX;
5389 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5390 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5391 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5392 case CONNMAN_NETWORK_TYPE_CELLULAR:
5393 return CONNMAN_SERVICE_TYPE_CELLULAR;
5396 return CONNMAN_SERVICE_TYPE_UNKNOWN;
5399 static enum connman_service_security convert_wifi_security(const char *security)
5401 if (security == NULL)
5402 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5403 else if (g_str_equal(security, "none") == TRUE)
5404 return CONNMAN_SERVICE_SECURITY_NONE;
5405 else if (g_str_equal(security, "wep") == TRUE)
5406 return CONNMAN_SERVICE_SECURITY_WEP;
5407 else if (g_str_equal(security, "psk") == TRUE)
5408 return CONNMAN_SERVICE_SECURITY_PSK;
5409 else if (g_str_equal(security, "ieee8021x") == TRUE)
5410 return CONNMAN_SERVICE_SECURITY_8021X;
5411 else if (g_str_equal(security, "wpa") == TRUE)
5412 return CONNMAN_SERVICE_SECURITY_WPA;
5413 else if (g_str_equal(security, "rsn") == TRUE)
5414 return CONNMAN_SERVICE_SECURITY_RSN;
5416 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5419 static void update_from_network(struct connman_service *service,
5420 struct connman_network *network)
5422 connman_uint8_t strength = service->strength;
5423 GSequenceIter *iter;
5426 DBG("service %p network %p", service, network);
5428 if (is_connected(service) == TRUE)
5431 if (is_connecting(service) == TRUE)
5434 str = connman_network_get_string(network, "Name");
5436 g_free(service->name);
5437 service->name = g_strdup(str);
5438 service->hidden = FALSE;
5440 g_free(service->name);
5441 service->name = NULL;
5442 service->hidden = TRUE;
5445 service->strength = connman_network_get_strength(network);
5446 service->roaming = connman_network_get_bool(network, "Roaming");
5448 if (service->strength == 0) {
5450 * Filter out 0-values; it's unclear what they mean
5451 * and they cause anomalous sorting of the priority list.
5453 service->strength = strength;
5456 str = connman_network_get_string(network, "WiFi.Security");
5457 service->security = convert_wifi_security(str);
5459 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5460 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5462 if (service->strength > strength && service->network != NULL) {
5463 service->network = network;
5465 strength_changed(service);
5468 if (service->network == NULL)
5469 service->network = network;
5471 iter = g_hash_table_lookup(service_hash, service->identifier);
5473 g_sequence_sort_changed(iter, service_compare, NULL);
5477 * __connman_service_create_from_network:
5478 * @network: network structure
5480 * Look up service by network and if not found, create one
5482 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5484 struct connman_service *service;
5485 struct connman_device *device;
5486 const char *ident, *group;
5490 DBG("network %p", network);
5492 if (network == NULL)
5495 ident = __connman_network_get_ident(network);
5499 group = connman_network_get_group(network);
5503 name = g_strdup_printf("%s_%s_%s",
5504 __connman_network_get_type(network), ident, group);
5505 service = service_get(name);
5508 if (service == NULL)
5511 if (__connman_network_get_weakness(network) == TRUE)
5514 if (service->path != NULL) {
5515 update_from_network(service, network);
5516 services_changed(TRUE);
5520 service->type = convert_network_type(network);
5522 switch (service->type) {
5523 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5524 case CONNMAN_SERVICE_TYPE_SYSTEM:
5525 case CONNMAN_SERVICE_TYPE_ETHERNET:
5526 case CONNMAN_SERVICE_TYPE_WIMAX:
5527 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5528 case CONNMAN_SERVICE_TYPE_GPS:
5529 case CONNMAN_SERVICE_TYPE_VPN:
5530 case CONNMAN_SERVICE_TYPE_GADGET:
5531 service->autoconnect = FALSE;
5533 case CONNMAN_SERVICE_TYPE_WIFI:
5534 case CONNMAN_SERVICE_TYPE_CELLULAR:
5535 service->autoconnect = TRUE;
5539 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5540 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5542 update_from_network(service, network);
5544 index = connman_network_get_index(network);
5546 if (service->ipconfig_ipv4 == NULL)
5547 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5549 if (service->ipconfig_ipv6 == NULL)
5550 setup_ip6config(service, index);
5552 service_register(service);
5554 if (service->favorite == TRUE) {
5555 device = connman_network_get_device(service->network);
5556 if (device && __connman_device_scanning(device) == FALSE)
5557 __connman_service_auto_connect();
5560 __connman_notifier_service_add(service, service->name);
5565 void __connman_service_update_from_network(struct connman_network *network)
5567 struct connman_service *service;
5568 connman_uint8_t strength;
5569 connman_bool_t roaming;
5570 GSequenceIter *iter;
5572 connman_bool_t stats_enable;
5574 DBG("network %p", network);
5576 service = __connman_service_lookup_from_network(network);
5577 if (service == NULL)
5580 if (service->network == NULL)
5583 name = connman_network_get_string(service->network, "Name");
5584 if (g_strcmp0(service->name, name) != 0) {
5585 g_free(service->name);
5586 service->name = g_strdup(name);
5587 connman_dbus_property_changed_basic(service->path,
5588 CONNMAN_SERVICE_INTERFACE, "Name",
5589 DBUS_TYPE_STRING, &service->name);
5592 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5593 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5595 strength = connman_network_get_strength(service->network);
5596 if (strength == service->strength)
5599 service->strength = strength;
5601 strength_changed(service);
5604 roaming = connman_network_get_bool(service->network, "Roaming");
5605 if (roaming == service->roaming)
5608 stats_enable = stats_enabled(service);
5609 if (stats_enable == TRUE)
5610 stats_stop(service);
5612 service->roaming = roaming;
5614 if (stats_enable == TRUE)
5615 stats_start(service);
5617 roaming_changed(service);
5619 iter = g_hash_table_lookup(service_hash, service->identifier);
5621 g_sequence_sort_changed(iter, service_compare, NULL);
5624 void __connman_service_remove_from_network(struct connman_network *network)
5626 struct connman_service *service;
5628 DBG("network %p", network);
5630 service = __connman_service_lookup_from_network(network);
5631 if (service == NULL)
5634 __connman_connection_gateway_remove(service,
5635 CONNMAN_IPCONFIG_TYPE_ALL);
5637 __connman_service_put(service);
5641 * __connman_service_create_from_provider:
5642 * @provider: provider structure
5644 * Look up service by provider and if not found, create one
5646 struct connman_service *
5647 __connman_service_create_from_provider(struct connman_provider *provider)
5649 struct connman_service *service;
5650 const char *ident, *str;
5652 int index = connman_provider_get_index(provider);
5654 DBG("provider %p", provider);
5656 ident = __connman_provider_get_ident(provider);
5660 name = g_strdup_printf("vpn_%s", ident);
5661 service = service_get(name);
5664 if (service == NULL)
5667 service->type = CONNMAN_SERVICE_TYPE_VPN;
5668 service->provider = connman_provider_ref(provider);
5669 service->autoconnect = FALSE;
5670 service->userconnect = TRUE;
5672 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5674 str = connman_provider_get_string(provider, "Name");
5676 g_free(service->name);
5677 service->name = g_strdup(str);
5678 service->hidden = FALSE;
5680 g_free(service->name);
5681 service->name = NULL;
5682 service->hidden = TRUE;
5685 service->strength = 0;
5687 if (service->ipconfig_ipv4 == NULL)
5688 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5690 if (service->ipconfig_ipv6 == NULL)
5691 setup_ip6config(service, index);
5693 service_register(service);
5695 __connman_notifier_service_add(service, service->name);
5700 void __connman_service_downgrade_state(struct connman_service *service)
5702 if (service == NULL)
5705 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
5706 service->state_ipv6);
5708 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
5709 __connman_service_ipconfig_indicate_state(service,
5710 CONNMAN_SERVICE_STATE_READY,
5711 CONNMAN_IPCONFIG_TYPE_IPV4);
5713 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
5714 __connman_service_ipconfig_indicate_state(service,
5715 CONNMAN_SERVICE_STATE_READY,
5716 CONNMAN_IPCONFIG_TYPE_IPV6);
5719 int __connman_service_init(void)
5723 connection = connman_dbus_get_connection();
5725 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5728 service_list = g_sequence_new(service_free);
5733 void __connman_service_cleanup(void)
5739 list = service_list;
5740 service_list = NULL;
5741 g_sequence_free(list);
5743 g_hash_table_destroy(service_hash);
5744 service_hash = NULL;
5746 g_slist_free(counter_list);
5747 counter_list = NULL;
5749 dbus_connection_unref(connection);