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 gint 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;
93 /* 802.1x settings from the config files */
98 char *client_cert_file;
99 char *private_key_file;
100 char *private_key_passphrase;
102 DBusMessage *pending;
104 struct connman_stats stats;
105 struct connman_stats stats_roaming;
106 GHashTable *counter_table;
107 enum connman_service_proxy_method proxy;
108 enum connman_service_proxy_method proxy_config;
115 static void append_path(gpointer value, gpointer user_data)
117 struct connman_service *service = value;
118 DBusMessageIter *iter = user_data;
120 if (service->path == NULL || service->hidden == TRUE)
123 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
127 void __connman_service_list(DBusMessageIter *iter, void *user_data)
129 if (service_list == NULL)
132 g_sequence_foreach(service_list, append_path, iter);
137 struct connman_service *service;
140 static void compare_path(gpointer value, gpointer user_data)
142 struct connman_service *service = value;
143 struct find_data *data = user_data;
145 if (data->service != NULL)
148 if (g_strcmp0(service->path, data->path) == 0)
149 data->service = service;
152 static struct connman_service *find_service(const char *path)
154 struct find_data data = { .path = path, .service = NULL };
156 DBG("path %s", path);
158 g_sequence_foreach(service_list, compare_path, &data);
163 const char *__connman_service_type2string(enum connman_service_type type)
166 case CONNMAN_SERVICE_TYPE_UNKNOWN:
168 case CONNMAN_SERVICE_TYPE_SYSTEM:
170 case CONNMAN_SERVICE_TYPE_ETHERNET:
172 case CONNMAN_SERVICE_TYPE_WIFI:
174 case CONNMAN_SERVICE_TYPE_WIMAX:
176 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
178 case CONNMAN_SERVICE_TYPE_CELLULAR:
180 case CONNMAN_SERVICE_TYPE_GPS:
182 case CONNMAN_SERVICE_TYPE_VPN:
184 case CONNMAN_SERVICE_TYPE_GADGET:
191 static const char *security2string(enum connman_service_security security)
194 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
196 case CONNMAN_SERVICE_SECURITY_NONE:
198 case CONNMAN_SERVICE_SECURITY_WEP:
200 case CONNMAN_SERVICE_SECURITY_PSK:
202 case CONNMAN_SERVICE_SECURITY_8021X:
204 case CONNMAN_SERVICE_SECURITY_WPA:
206 case CONNMAN_SERVICE_SECURITY_RSN:
213 static const char *state2string(enum connman_service_state state)
216 case CONNMAN_SERVICE_STATE_UNKNOWN:
218 case CONNMAN_SERVICE_STATE_IDLE:
220 case CONNMAN_SERVICE_STATE_ASSOCIATION:
221 return "association";
222 case CONNMAN_SERVICE_STATE_CONFIGURATION:
223 return "configuration";
224 case CONNMAN_SERVICE_STATE_READY:
226 case CONNMAN_SERVICE_STATE_ONLINE:
228 case CONNMAN_SERVICE_STATE_DISCONNECT:
230 case CONNMAN_SERVICE_STATE_FAILURE:
237 static const char *error2string(enum connman_service_error error)
240 case CONNMAN_SERVICE_ERROR_UNKNOWN:
242 case CONNMAN_SERVICE_ERROR_OUT_OF_RANGE:
243 return "out-of-range";
244 case CONNMAN_SERVICE_ERROR_PIN_MISSING:
245 return "pin-missing";
246 case CONNMAN_SERVICE_ERROR_DHCP_FAILED:
247 return "dhcp-failed";
248 case CONNMAN_SERVICE_ERROR_CONNECT_FAILED:
249 return "connect-failed";
250 case CONNMAN_SERVICE_ERROR_LOGIN_FAILED:
251 return "login-failed";
252 case CONNMAN_SERVICE_ERROR_AUTH_FAILED:
253 return "auth-failed";
254 case CONNMAN_SERVICE_ERROR_INVALID_KEY:
255 return "invalid-key";
261 static enum connman_service_error string2error(const char *error)
263 if (g_strcmp0(error, "dhcp-failed") == 0)
264 return CONNMAN_SERVICE_ERROR_DHCP_FAILED;
265 else if (g_strcmp0(error, "pin-missing") == 0)
266 return CONNMAN_SERVICE_ERROR_PIN_MISSING;
267 else if (g_strcmp0(error, "invalid-key") == 0)
268 return CONNMAN_SERVICE_ERROR_INVALID_KEY;
270 return CONNMAN_SERVICE_ERROR_UNKNOWN;
273 static const char *proxymethod2string(enum connman_service_proxy_method method)
276 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
278 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
280 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
282 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
289 static enum connman_service_proxy_method string2proxymethod(const char *method)
291 if (g_strcmp0(method, "direct") == 0)
292 return CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
293 else if (g_strcmp0(method, "auto") == 0)
294 return CONNMAN_SERVICE_PROXY_METHOD_AUTO;
295 else if (g_strcmp0(method, "manual") == 0)
296 return CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
298 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
301 static int service_load(struct connman_service *service)
304 GError *error = NULL;
307 connman_bool_t autoconnect;
308 unsigned int ssid_len;
311 DBG("service %p", service);
313 keyfile = connman_storage_load_service(service->identifier);
317 switch (service->type) {
318 case CONNMAN_SERVICE_TYPE_UNKNOWN:
319 case CONNMAN_SERVICE_TYPE_SYSTEM:
320 case CONNMAN_SERVICE_TYPE_ETHERNET:
321 case CONNMAN_SERVICE_TYPE_GPS:
322 case CONNMAN_SERVICE_TYPE_VPN:
323 case CONNMAN_SERVICE_TYPE_GADGET:
325 case CONNMAN_SERVICE_TYPE_WIFI:
326 if (service->name == NULL) {
329 name = g_key_file_get_string(keyfile,
330 service->identifier, "Name", NULL);
332 g_free(service->name);
333 service->name = name;
336 if (service->network != NULL)
337 connman_network_set_name(service->network,
341 if (service->network &&
342 connman_network_get_blob(service->network,
343 "WiFi.SSID", &ssid_len) == NULL) {
346 hex_ssid = g_key_file_get_string(keyfile,
350 if (hex_ssid != NULL) {
352 unsigned int i, j = 0, hex;
353 size_t hex_ssid_len = strlen(hex_ssid);
355 ssid = g_try_malloc0(hex_ssid_len / 2);
362 for (i = 0; i < hex_ssid_len; i += 2) {
363 sscanf(hex_ssid + i, "%02x", &hex);
367 connman_network_set_blob(service->network,
368 "WiFi.SSID", ssid, hex_ssid_len / 2);
375 case CONNMAN_SERVICE_TYPE_WIMAX:
376 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
377 case CONNMAN_SERVICE_TYPE_CELLULAR:
378 service->favorite = g_key_file_get_boolean(keyfile,
379 service->identifier, "Favorite", NULL);
381 autoconnect = g_key_file_get_boolean(keyfile,
382 service->identifier, "AutoConnect", &error);
384 service->autoconnect = autoconnect;
385 g_clear_error(&error);
387 str = g_key_file_get_string(keyfile,
388 service->identifier, "Failure", NULL);
390 if (service->favorite == FALSE)
391 service->state_ipv4 = service->state_ipv6 =
392 CONNMAN_SERVICE_STATE_FAILURE;
393 service->error = string2error(str);
398 str = g_key_file_get_string(keyfile,
399 service->identifier, "Modified", NULL);
401 g_time_val_from_iso8601(str, &service->modified);
405 str = g_key_file_get_string(keyfile,
406 service->identifier, "Passphrase", NULL);
408 g_free(service->passphrase);
409 service->passphrase = str;
412 if (service->ipconfig_ipv4 != NULL)
413 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
414 service->identifier, "IPv4.");
416 if (service->ipconfig_ipv6 != NULL)
417 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
418 service->identifier, "IPv6.");
420 service->nameservers_config = g_key_file_get_string_list(keyfile,
421 service->identifier, "Nameservers", &length, NULL);
422 if (service->nameservers_config != NULL && length == 0) {
423 g_strfreev(service->nameservers_config);
424 service->nameservers_config = NULL;
427 service->domains = g_key_file_get_string_list(keyfile,
428 service->identifier, "Domains", &length, NULL);
429 if (service->domains != NULL && length == 0) {
430 g_strfreev(service->domains);
431 service->domains = NULL;
434 str = g_key_file_get_string(keyfile,
435 service->identifier, "Proxy.Method", NULL);
437 service->proxy_config = string2proxymethod(str);
441 service->proxies = g_key_file_get_string_list(keyfile,
442 service->identifier, "Proxy.Servers", &length, NULL);
443 if (service->proxies != NULL && length == 0) {
444 g_strfreev(service->proxies);
445 service->proxies = NULL;
448 service->excludes = g_key_file_get_string_list(keyfile,
449 service->identifier, "Proxy.Excludes", &length, NULL);
450 if (service->excludes != NULL && length == 0) {
451 g_strfreev(service->excludes);
452 service->excludes = NULL;
455 str = g_key_file_get_string(keyfile,
456 service->identifier, "Proxy.URL", NULL);
458 g_free(service->pac);
463 g_key_file_free(keyfile);
468 static int service_save(struct connman_service *service)
473 const char *cst_str = NULL;
476 DBG("service %p", service);
478 keyfile = __connman_storage_open_service(service->identifier);
482 if (service->name != NULL)
483 g_key_file_set_string(keyfile, service->identifier,
484 "Name", service->name);
486 switch (service->type) {
487 case CONNMAN_SERVICE_TYPE_UNKNOWN:
488 case CONNMAN_SERVICE_TYPE_SYSTEM:
489 case CONNMAN_SERVICE_TYPE_ETHERNET:
490 case CONNMAN_SERVICE_TYPE_GPS:
491 case CONNMAN_SERVICE_TYPE_VPN:
492 case CONNMAN_SERVICE_TYPE_GADGET:
494 case CONNMAN_SERVICE_TYPE_WIFI:
495 if (service->network) {
496 const unsigned char *ssid;
497 unsigned int ssid_len = 0;
499 ssid = connman_network_get_blob(service->network,
500 "WiFi.SSID", &ssid_len);
502 if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
503 char *identifier = service->identifier;
507 str = g_string_sized_new(ssid_len * 2);
513 for (i = 0; i < ssid_len; i++)
514 g_string_append_printf(str,
517 g_key_file_set_string(keyfile, identifier,
520 g_string_free(str, TRUE);
523 freq = connman_network_get_frequency(service->network);
524 g_key_file_set_integer(keyfile, service->identifier,
529 case CONNMAN_SERVICE_TYPE_WIMAX:
530 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
531 case CONNMAN_SERVICE_TYPE_CELLULAR:
532 g_key_file_set_boolean(keyfile, service->identifier,
533 "Favorite", service->favorite);
535 if (service->favorite == TRUE)
536 g_key_file_set_boolean(keyfile, service->identifier,
537 "AutoConnect", service->autoconnect);
539 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_FAILURE ||
540 service->state_ipv6 == CONNMAN_SERVICE_STATE_FAILURE) {
541 const char *failure = error2string(service->error);
543 g_key_file_set_string(keyfile,
547 g_key_file_remove_key(keyfile, service->identifier,
553 str = g_time_val_to_iso8601(&service->modified);
555 g_key_file_set_string(keyfile, service->identifier,
560 if (service->passphrase != NULL && strlen(service->passphrase) > 0)
561 g_key_file_set_string(keyfile, service->identifier,
562 "Passphrase", service->passphrase);
564 g_key_file_remove_key(keyfile, service->identifier,
567 if (service->ipconfig_ipv4 != NULL)
568 __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
569 service->identifier, "IPv4.");
571 if (service->ipconfig_ipv6 != NULL)
572 __connman_ipconfig_save(service->ipconfig_ipv6, keyfile,
573 service->identifier, "IPv6.");
575 if (service->nameservers_config != NULL) {
576 guint len = g_strv_length(service->nameservers_config);
578 g_key_file_set_string_list(keyfile, service->identifier,
580 (const gchar **) service->nameservers_config, len);
582 g_key_file_remove_key(keyfile, service->identifier,
583 "Nameservers", NULL);
585 if (service->domains != NULL) {
586 guint len = g_strv_length(service->domains);
588 g_key_file_set_string_list(keyfile, service->identifier,
590 (const gchar **) service->domains, len);
592 g_key_file_remove_key(keyfile, service->identifier,
595 cst_str = proxymethod2string(service->proxy_config);
597 g_key_file_set_string(keyfile, service->identifier,
598 "Proxy.Method", cst_str);
600 if (service->proxies != NULL) {
601 guint len = g_strv_length(service->proxies);
603 g_key_file_set_string_list(keyfile, service->identifier,
605 (const gchar **) service->proxies, len);
607 g_key_file_remove_key(keyfile, service->identifier,
608 "Proxy.Servers", NULL);
610 if (service->excludes != NULL) {
611 guint len = g_strv_length(service->excludes);
613 g_key_file_set_string_list(keyfile, service->identifier,
615 (const gchar **) service->excludes, len);
617 g_key_file_remove_key(keyfile, service->identifier,
618 "Proxy.Excludes", NULL);
620 if (service->pac != NULL && strlen(service->pac) > 0)
621 g_key_file_set_string(keyfile, service->identifier,
622 "Proxy.URL", service->pac);
624 g_key_file_remove_key(keyfile, service->identifier,
628 __connman_storage_save_service(keyfile, service->identifier);
630 g_key_file_free(keyfile);
635 static guint changed_timeout = 0;
637 static gboolean notify_services_changed(gpointer user_data)
641 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
642 CONNMAN_MANAGER_INTERFACE, "Services",
643 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
649 static void services_changed(gboolean delayed)
653 if (changed_timeout > 0) {
654 g_source_remove(changed_timeout);
658 if (__connman_connection_update_gateway() == TRUE) {
659 notify_services_changed(NULL);
663 if (delayed == FALSE) {
664 notify_services_changed(NULL);
668 changed_timeout = g_timeout_add_seconds(1, notify_services_changed,
672 static enum connman_service_state combine_state(
673 enum connman_service_state state_a,
674 enum connman_service_state state_b)
676 enum connman_service_state result;
678 if (state_a == state_b) {
683 if (state_a == CONNMAN_SERVICE_STATE_UNKNOWN) {
688 if (state_b == CONNMAN_SERVICE_STATE_UNKNOWN) {
693 if (state_a == CONNMAN_SERVICE_STATE_IDLE) {
698 if (state_b == CONNMAN_SERVICE_STATE_IDLE) {
703 if (state_a == CONNMAN_SERVICE_STATE_ONLINE) {
708 if (state_b == CONNMAN_SERVICE_STATE_ONLINE) {
713 if (state_a == CONNMAN_SERVICE_STATE_READY) {
718 if (state_b == CONNMAN_SERVICE_STATE_READY) {
723 if (state_a == CONNMAN_SERVICE_STATE_CONFIGURATION) {
728 if (state_b == CONNMAN_SERVICE_STATE_CONFIGURATION) {
733 if (state_a == CONNMAN_SERVICE_STATE_ASSOCIATION) {
738 if (state_b == CONNMAN_SERVICE_STATE_ASSOCIATION) {
743 if (state_a == CONNMAN_SERVICE_STATE_DISCONNECT) {
748 if (state_b == CONNMAN_SERVICE_STATE_DISCONNECT) {
753 result = CONNMAN_SERVICE_STATE_FAILURE;
759 static connman_bool_t is_connecting_state(struct connman_service *service,
760 enum connman_service_state state)
763 case CONNMAN_SERVICE_STATE_UNKNOWN:
764 case CONNMAN_SERVICE_STATE_IDLE:
765 case CONNMAN_SERVICE_STATE_FAILURE:
766 if (service->network != NULL)
767 return connman_network_get_connecting(service->network);
768 case CONNMAN_SERVICE_STATE_DISCONNECT:
769 case CONNMAN_SERVICE_STATE_READY:
770 case CONNMAN_SERVICE_STATE_ONLINE:
772 case CONNMAN_SERVICE_STATE_ASSOCIATION:
773 case CONNMAN_SERVICE_STATE_CONFIGURATION:
780 static connman_bool_t is_connected_state(const struct connman_service *service,
781 enum connman_service_state state)
784 case CONNMAN_SERVICE_STATE_UNKNOWN:
785 case CONNMAN_SERVICE_STATE_IDLE:
786 case CONNMAN_SERVICE_STATE_ASSOCIATION:
787 case CONNMAN_SERVICE_STATE_CONFIGURATION:
788 case CONNMAN_SERVICE_STATE_DISCONNECT:
789 case CONNMAN_SERVICE_STATE_FAILURE:
791 case CONNMAN_SERVICE_STATE_READY:
792 case CONNMAN_SERVICE_STATE_ONLINE:
799 static connman_bool_t is_connecting(struct connman_service *service)
801 return is_connecting_state(service, service->state);
804 static connman_bool_t is_connected(struct connman_service *service)
806 return is_connected_state(service, service->state);
809 static void update_nameservers(struct connman_service *service)
813 if (service->ipconfig_ipv4)
814 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv4);
815 else if (service->ipconfig_ipv6)
816 ifname = connman_ipconfig_get_ifname(service->ipconfig_ipv6);
823 switch (combine_state(service->state_ipv4, service->state_ipv6)) {
824 case CONNMAN_SERVICE_STATE_UNKNOWN:
825 case CONNMAN_SERVICE_STATE_IDLE:
826 case CONNMAN_SERVICE_STATE_ASSOCIATION:
827 case CONNMAN_SERVICE_STATE_CONFIGURATION:
829 case CONNMAN_SERVICE_STATE_FAILURE:
830 case CONNMAN_SERVICE_STATE_DISCONNECT:
831 connman_resolver_remove_all(ifname);
833 case CONNMAN_SERVICE_STATE_READY:
834 case CONNMAN_SERVICE_STATE_ONLINE:
838 connman_resolver_remove_all(ifname);
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();
868 int __connman_service_nameserver_append(struct connman_service *service,
869 const char *nameserver)
873 DBG("service %p nameserver %s", service, nameserver);
875 if (nameserver == NULL)
878 if (service->nameservers != NULL) {
881 for (i = 0; service->nameservers[i] != NULL; i++)
882 if (g_strcmp0(service->nameservers[i], nameserver) == 0)
885 len = g_strv_length(service->nameservers);
886 service->nameservers = g_try_renew(char *, service->nameservers,
890 service->nameservers = g_try_new0(char *, len + 2);
893 if (service->nameservers == NULL)
896 service->nameservers[len] = g_strdup(nameserver);
897 service->nameservers[len + 1] = NULL;
899 update_nameservers(service);
904 int __connman_service_nameserver_remove(struct connman_service *service,
905 const char *nameserver)
910 DBG("service %p nameserver %s", service, nameserver);
912 if (nameserver == NULL)
915 if (service->nameservers == NULL)
918 len = g_strv_length(service->nameservers);
920 if (g_strcmp0(service->nameservers[0], nameserver) != 0)
923 g_strfreev(service->nameservers);
924 service->nameservers = NULL;
929 servers = g_try_new0(char *, len);
933 for (i = 0, j = 0; i < len; i++) {
934 if (g_strcmp0(service->nameservers[i], nameserver) != 0) {
935 servers[j] = g_strdup(service->nameservers[i]);
939 servers[len - 1] = NULL;
941 g_strfreev(service->nameservers);
942 service->nameservers = servers;
944 update_nameservers(service);
949 void __connman_service_nameserver_clear(struct connman_service *service)
951 g_strfreev(service->nameservers);
952 service->nameservers = NULL;
954 update_nameservers(service);
957 static void nameserver_add_routes(int index, char **nameservers,
961 struct addrinfo hints;
962 struct addrinfo *addr;
964 for (i = 0; nameservers[i] != NULL; i++) {
965 memset(&hints, 0, sizeof(struct addrinfo));
966 hints.ai_flags = AI_NUMERICHOST;
969 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
970 if (ret == EAI_NONAME)
971 family = AF_INET; /* use the IPv4 as a default */
975 family = addr->ai_family;
977 if (family == AF_INET) {
978 if (connman_inet_compare_subnet(index,
979 nameservers[i]) != TRUE)
980 connman_inet_add_host_route(index,
982 } else if (family == AF_INET6)
983 connman_inet_add_ipv6_host_route(index,
990 static void nameserver_del_routes(int index, char **nameservers)
993 struct addrinfo hints;
994 struct addrinfo *addr;
996 for (i = 0; nameservers[i] != NULL; i++) {
997 memset(&hints, 0, sizeof(struct addrinfo));
998 hints.ai_flags = AI_NUMERICHOST;
1001 ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
1002 if (ret == EAI_NONAME)
1003 family = AF_INET; /* use the IPv4 as a default */
1007 family = addr->ai_family;
1009 if (family == AF_INET)
1010 connman_inet_del_host_route(index, nameservers[i]);
1011 else if (family == AF_INET6)
1012 connman_inet_del_ipv6_host_route(index,
1019 void __connman_service_nameserver_add_routes(struct connman_service *service,
1024 if (service == NULL)
1027 if (service->network != NULL)
1028 index = connman_network_get_index(service->network);
1029 else if (service->provider != NULL)
1030 index = connman_provider_get_index(service->provider);
1032 if (service->nameservers_config != NULL) {
1034 * Configured nameserver takes preference over the
1035 * discoverd nameserver gathered from DHCP, VPN, etc.
1037 nameserver_add_routes(index, service->nameservers_config, gw);
1038 } else if (service->nameservers != NULL) {
1040 * We add nameservers host routes for nameservers that
1041 * are not on our subnet. For those who are, the subnet
1042 * route will be installed by the time the dns proxy code
1043 * tries to reach them. The subnet route is installed
1044 * when setting the interface IP address.
1046 nameserver_add_routes(index, service->nameservers, gw);
1050 void __connman_service_nameserver_del_routes(struct connman_service *service)
1054 if (service == NULL)
1057 if (service->network != NULL)
1058 index = connman_network_get_index(service->network);
1059 else if (service->provider != NULL)
1060 index = connman_provider_get_index(service->provider);
1062 if (service->nameservers_config != NULL)
1063 nameserver_del_routes(index, service->nameservers_config);
1064 else if (service->nameservers != NULL)
1065 nameserver_del_routes(index, service->nameservers);
1068 static struct connman_stats *stats_get(struct connman_service *service)
1070 if (service->roaming == TRUE)
1071 return &service->stats_roaming;
1073 return &service->stats;
1076 static connman_bool_t stats_enabled(struct connman_service *service)
1078 struct connman_stats *stats = stats_get(service);
1080 return stats->enabled;
1083 static void stats_start(struct connman_service *service)
1085 struct connman_stats *stats = stats_get(service);
1087 DBG("service %p", service);
1089 if (stats->timer == NULL)
1092 stats->enabled = TRUE;
1093 stats->data_last.time = stats->data.time;
1095 g_timer_start(stats->timer);
1098 static void stats_stop(struct connman_service *service)
1100 struct connman_stats *stats = stats_get(service);
1101 unsigned int seconds;
1103 DBG("service %p", service);
1105 if (stats->timer == NULL)
1108 if (stats->enabled == FALSE)
1111 g_timer_stop(stats->timer);
1113 seconds = g_timer_elapsed(stats->timer, NULL);
1114 stats->data.time = stats->data_last.time + seconds;
1116 stats->enabled = FALSE;
1119 static void reset_stats(struct connman_service *service)
1121 DBG("service %p", service);
1124 service->stats.valid = FALSE;
1126 service->stats.data.rx_packets = 0;
1127 service->stats.data.tx_packets = 0;
1128 service->stats.data.rx_bytes = 0;
1129 service->stats.data.tx_bytes = 0;
1130 service->stats.data.rx_errors = 0;
1131 service->stats.data.tx_errors = 0;
1132 service->stats.data.rx_dropped = 0;
1133 service->stats.data.tx_dropped = 0;
1134 service->stats.data.time = 0;
1135 service->stats.data_last.time = 0;
1137 g_timer_reset(service->stats.timer);
1140 service->stats_roaming.valid = FALSE;
1142 service->stats_roaming.data.rx_packets = 0;
1143 service->stats_roaming.data.tx_packets = 0;
1144 service->stats_roaming.data.rx_bytes = 0;
1145 service->stats_roaming.data.tx_bytes = 0;
1146 service->stats_roaming.data.rx_errors = 0;
1147 service->stats_roaming.data.tx_errors = 0;
1148 service->stats_roaming.data.rx_dropped = 0;
1149 service->stats_roaming.data.tx_dropped = 0;
1150 service->stats_roaming.data.time = 0;
1151 service->stats_roaming.data_last.time = 0;
1153 g_timer_reset(service->stats_roaming.timer);
1156 static struct connman_service *get_default(void)
1158 struct connman_service *service;
1159 GSequenceIter *iter;
1161 iter = g_sequence_get_begin_iter(service_list);
1163 if (g_sequence_iter_is_end(iter) == TRUE)
1166 service = g_sequence_get(iter);
1168 if (is_connected(service) == FALSE)
1174 static void default_changed(void)
1176 struct connman_service *service = get_default();
1178 __connman_notifier_default_changed(service);
1181 const char *__connman_service_default(void)
1183 struct connman_service *service;
1185 service = get_default();
1186 if (service == NULL)
1189 return __connman_service_type2string(service->type);
1192 static void state_changed(struct connman_service *service)
1196 __connman_notifier_service_state_changed(service, service->state);
1198 str = state2string(service->state);
1202 connman_dbus_property_changed_basic(service->path,
1203 CONNMAN_SERVICE_INTERFACE, "State",
1204 DBUS_TYPE_STRING, &str);
1207 static void strength_changed(struct connman_service *service)
1209 if (service->strength == 0)
1212 connman_dbus_property_changed_basic(service->path,
1213 CONNMAN_SERVICE_INTERFACE, "Strength",
1214 DBUS_TYPE_BYTE, &service->strength);
1217 static void favorite_changed(struct connman_service *service)
1219 if (service->path == NULL)
1222 connman_dbus_property_changed_basic(service->path,
1223 CONNMAN_SERVICE_INTERFACE, "Favorite",
1224 DBUS_TYPE_BOOLEAN, &service->favorite);
1227 static void immutable_changed(struct connman_service *service)
1229 if (service->path == NULL)
1232 connman_dbus_property_changed_basic(service->path,
1233 CONNMAN_SERVICE_INTERFACE, "Immutable",
1234 DBUS_TYPE_BOOLEAN, &service->immutable);
1237 static void roaming_changed(struct connman_service *service)
1239 if (service->path == NULL)
1242 connman_dbus_property_changed_basic(service->path,
1243 CONNMAN_SERVICE_INTERFACE, "Roaming",
1244 DBUS_TYPE_BOOLEAN, &service->roaming);
1247 static void autoconnect_changed(struct connman_service *service)
1249 if (service->path == NULL)
1252 connman_dbus_property_changed_basic(service->path,
1253 CONNMAN_SERVICE_INTERFACE, "AutoConnect",
1254 DBUS_TYPE_BOOLEAN, &service->autoconnect);
1257 static void passphrase_changed(struct connman_service *service)
1259 dbus_bool_t required;
1261 switch (service->type) {
1262 case CONNMAN_SERVICE_TYPE_UNKNOWN:
1263 case CONNMAN_SERVICE_TYPE_SYSTEM:
1264 case CONNMAN_SERVICE_TYPE_ETHERNET:
1265 case CONNMAN_SERVICE_TYPE_WIMAX:
1266 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1267 case CONNMAN_SERVICE_TYPE_CELLULAR:
1268 case CONNMAN_SERVICE_TYPE_GPS:
1269 case CONNMAN_SERVICE_TYPE_VPN:
1270 case CONNMAN_SERVICE_TYPE_GADGET:
1272 case CONNMAN_SERVICE_TYPE_WIFI:
1275 switch (service->security) {
1276 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1277 case CONNMAN_SERVICE_SECURITY_NONE:
1279 case CONNMAN_SERVICE_SECURITY_WEP:
1280 case CONNMAN_SERVICE_SECURITY_PSK:
1281 case CONNMAN_SERVICE_SECURITY_WPA:
1282 case CONNMAN_SERVICE_SECURITY_RSN:
1283 if (service->passphrase == NULL)
1286 case CONNMAN_SERVICE_SECURITY_8021X:
1292 connman_dbus_property_changed_basic(service->path,
1293 CONNMAN_SERVICE_INTERFACE, "PassphraseRequired",
1294 DBUS_TYPE_BOOLEAN, &required);
1297 static void login_changed(struct connman_service *service)
1299 dbus_bool_t required = service->login_required;
1301 if (service->path == NULL)
1304 connman_dbus_property_changed_basic(service->path,
1305 CONNMAN_SERVICE_INTERFACE, "LoginRequired",
1306 DBUS_TYPE_BOOLEAN, &required);
1309 static void append_security(DBusMessageIter *iter, void *user_data)
1311 struct connman_service *service = user_data;
1314 str = security2string(service->security);
1316 dbus_message_iter_append_basic(iter,
1317 DBUS_TYPE_STRING, &str);
1320 * Some access points incorrectly advertise WPS even when they
1321 * are configured as open or no security, so filter
1324 if (service->wps == TRUE) {
1325 switch (service->security) {
1326 case CONNMAN_SERVICE_SECURITY_PSK:
1327 case CONNMAN_SERVICE_SECURITY_WPA:
1328 case CONNMAN_SERVICE_SECURITY_RSN:
1330 dbus_message_iter_append_basic(iter,
1331 DBUS_TYPE_STRING, &str);
1333 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
1334 case CONNMAN_SERVICE_SECURITY_NONE:
1335 case CONNMAN_SERVICE_SECURITY_WEP:
1336 case CONNMAN_SERVICE_SECURITY_8021X:
1342 static void append_ethernet(DBusMessageIter *iter, void *user_data)
1344 struct connman_service *service = user_data;
1346 if (service->ipconfig_ipv4 != NULL)
1347 __connman_ipconfig_append_ethernet(service->ipconfig_ipv4,
1349 else if (service->ipconfig_ipv6 != NULL)
1350 __connman_ipconfig_append_ethernet(service->ipconfig_ipv6,
1354 static void append_ipv4(DBusMessageIter *iter, void *user_data)
1356 struct connman_service *service = user_data;
1358 DBG("ipv4 %p state %s", service->ipconfig_ipv4,
1359 state2string(service->state_ipv4));
1361 if (is_connected_state(service, service->state_ipv4) == FALSE)
1364 if (service->ipconfig_ipv4 != NULL)
1365 __connman_ipconfig_append_ipv4(service->ipconfig_ipv4, iter);
1368 static void append_ipv6(DBusMessageIter *iter, void *user_data)
1370 struct connman_service *service = user_data;
1372 DBG("ipv6 %p state %s", service->ipconfig_ipv6,
1373 state2string(service->state_ipv6));
1375 if (is_connected_state(service, service->state_ipv6) == FALSE)
1378 if (service->ipconfig_ipv6 != NULL)
1379 __connman_ipconfig_append_ipv6(service->ipconfig_ipv6, iter,
1380 service->ipconfig_ipv4);
1383 static void append_ipv4config(DBusMessageIter *iter, void *user_data)
1385 struct connman_service *service = user_data;
1387 if (service->ipconfig_ipv4 != NULL)
1388 __connman_ipconfig_append_ipv4config(service->ipconfig_ipv4,
1392 static void append_ipv6config(DBusMessageIter *iter, void *user_data)
1394 struct connman_service *service = user_data;
1396 if (service->ipconfig_ipv6 != NULL)
1397 __connman_ipconfig_append_ipv6config(service->ipconfig_ipv6,
1401 static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
1406 servers = *nameservers;
1408 for (i = 0; servers[i] != NULL; i++) {
1409 dbus_message_iter_append_basic(iter,
1410 DBUS_TYPE_STRING, &servers[i]);
1414 static void append_dns(DBusMessageIter *iter, void *user_data)
1416 struct connman_service *service = user_data;
1418 if (is_connected(service) == FALSE)
1421 if (service->nameservers_config != NULL) {
1422 append_nameserver(iter, &service->nameservers_config);
1424 } else if (service->nameservers != NULL) {
1425 append_nameserver(iter, &service->nameservers);
1430 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
1432 struct connman_service *service = user_data;
1435 if (service->nameservers_config == NULL)
1438 for (i = 0; service->nameservers_config[i]; i++) {
1439 dbus_message_iter_append_basic(iter,
1441 &service->nameservers_config[i]);
1445 static void append_domain(DBusMessageIter *iter, void *user_data)
1447 struct connman_service *service = user_data;
1449 if (is_connected(service) == FALSE &&
1450 is_connecting(service) == FALSE)
1453 if (service->domainname == NULL)
1456 dbus_message_iter_append_basic(iter,
1457 DBUS_TYPE_STRING, &service->domainname);
1460 static void append_domainconfig(DBusMessageIter *iter, void *user_data)
1462 struct connman_service *service = user_data;
1465 if (service->domains == NULL)
1468 for (i = 0; service->domains[i]; i++)
1469 dbus_message_iter_append_basic(iter,
1470 DBUS_TYPE_STRING, &service->domains[i]);
1473 static void append_proxies(DBusMessageIter *iter, void *user_data)
1475 struct connman_service *service = user_data;
1478 if (service->proxies == NULL)
1481 for (i = 0; service->proxies[i]; i++)
1482 dbus_message_iter_append_basic(iter,
1483 DBUS_TYPE_STRING, &service->proxies[i]);
1486 static void append_excludes(DBusMessageIter *iter, void *user_data)
1488 struct connman_service *service = user_data;
1491 if (service->excludes == NULL)
1494 for (i = 0; service->excludes[i]; i++)
1495 dbus_message_iter_append_basic(iter,
1496 DBUS_TYPE_STRING, &service->excludes[i]);
1499 static void append_proxy(DBusMessageIter *iter, void *user_data)
1501 struct connman_service *service = user_data;
1502 enum connman_service_proxy_method proxy;
1503 const char *pac = NULL;
1504 const char *method = proxymethod2string(
1505 CONNMAN_SERVICE_PROXY_METHOD_DIRECT);
1509 if (is_connected(service) == FALSE)
1512 proxy = connman_service_get_proxy_method(service);
1515 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1517 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1519 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1520 connman_dbus_dict_append_array(iter, "Servers",
1521 DBUS_TYPE_STRING, append_proxies,
1524 connman_dbus_dict_append_array(iter, "Excludes",
1525 DBUS_TYPE_STRING, append_excludes,
1528 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1529 /* Maybe DHCP, or WPAD, has provided an url for a pac file */
1530 if (service->ipconfig_ipv4 != NULL)
1531 pac = __connman_ipconfig_get_proxy_autoconfig(
1532 service->ipconfig_ipv4);
1533 else if (service->ipconfig_ipv6 != NULL)
1534 pac = __connman_ipconfig_get_proxy_autoconfig(
1535 service->ipconfig_ipv6);
1537 if (service->pac == NULL && pac == NULL)
1540 if (service->pac != NULL)
1543 connman_dbus_dict_append_basic(iter, "URL",
1544 DBUS_TYPE_STRING, &pac);
1548 method = proxymethod2string(proxy);
1551 connman_dbus_dict_append_basic(iter, "Method",
1552 DBUS_TYPE_STRING, &method);
1555 static void append_proxyconfig(DBusMessageIter *iter, void *user_data)
1557 struct connman_service *service = user_data;
1560 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
1563 switch (service->proxy_config) {
1564 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
1566 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
1568 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
1569 if (service->proxies != NULL)
1570 connman_dbus_dict_append_array(iter, "Servers",
1572 append_proxies, service);
1574 if (service->excludes != NULL)
1575 connman_dbus_dict_append_array(iter, "Excludes",
1577 append_excludes, service);
1579 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
1580 if (service->pac != NULL)
1581 connman_dbus_dict_append_basic(iter, "URL",
1582 DBUS_TYPE_STRING, &service->pac);
1586 method = proxymethod2string(service->proxy_config);
1588 connman_dbus_dict_append_basic(iter, "Method",
1589 DBUS_TYPE_STRING, &method);
1592 static void append_provider(DBusMessageIter *iter, void *user_data)
1594 struct connman_service *service = user_data;
1596 DBG("%p %p", service, service->provider);
1598 if (is_connected(service) == FALSE)
1601 if (service->provider != NULL)
1602 __connman_provider_append_properties(service->provider, iter);
1606 static void settings_changed(struct connman_service *service,
1607 struct connman_ipconfig *ipconfig)
1609 connman_dbus_property_changed_dict(service->path,
1610 CONNMAN_SERVICE_INTERFACE, "IPv4",
1611 append_ipv4, service);
1613 connman_dbus_property_changed_dict(service->path,
1614 CONNMAN_SERVICE_INTERFACE, "IPv6",
1615 append_ipv6, service);
1617 __connman_notifier_ipconfig_changed(service, ipconfig);
1620 static void ipv4_configuration_changed(struct connman_service *service)
1622 connman_dbus_property_changed_dict(service->path,
1623 CONNMAN_SERVICE_INTERFACE,
1624 "IPv4.Configuration",
1629 static void ipv6_configuration_changed(struct connman_service *service)
1631 connman_dbus_property_changed_dict(service->path,
1632 CONNMAN_SERVICE_INTERFACE,
1633 "IPv6.Configuration",
1638 static void dns_changed(struct connman_service *service)
1640 connman_dbus_property_changed_array(service->path,
1641 CONNMAN_SERVICE_INTERFACE, "Nameservers",
1642 DBUS_TYPE_STRING, append_dns, service);
1645 static void dns_configuration_changed(struct connman_service *service)
1647 connman_dbus_property_changed_array(service->path,
1648 CONNMAN_SERVICE_INTERFACE,
1649 "Nameservers.Configuration",
1650 DBUS_TYPE_STRING, append_dnsconfig, service);
1652 dns_changed(service);
1655 static void domain_changed(struct connman_service *service)
1657 connman_dbus_property_changed_array(service->path,
1658 CONNMAN_SERVICE_INTERFACE, "Domains",
1659 DBUS_TYPE_STRING, append_domain, service);
1662 static void domain_configuration_changed(struct connman_service *service)
1664 connman_dbus_property_changed_array(service->path,
1665 CONNMAN_SERVICE_INTERFACE,
1666 "Domains.Configuration",
1667 DBUS_TYPE_STRING, append_domainconfig, service);
1670 static void proxy_changed(struct connman_service *service)
1672 connman_dbus_property_changed_dict(service->path,
1673 CONNMAN_SERVICE_INTERFACE, "Proxy",
1674 append_proxy, service);
1677 static void proxy_configuration_changed(struct connman_service *service)
1679 connman_dbus_property_changed_dict(service->path,
1680 CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
1681 append_proxyconfig, service);
1683 proxy_changed(service);
1686 static void link_changed(struct connman_service *service)
1688 connman_dbus_property_changed_dict(service->path,
1689 CONNMAN_SERVICE_INTERFACE, "Ethernet",
1690 append_ethernet, service);
1693 static void stats_append_counters(DBusMessageIter *dict,
1694 struct connman_stats_data *stats,
1695 struct connman_stats_data *counters,
1696 connman_bool_t append_all)
1698 if (counters->rx_packets != stats->rx_packets || append_all) {
1699 counters->rx_packets = stats->rx_packets;
1700 connman_dbus_dict_append_basic(dict, "RX.Packets",
1701 DBUS_TYPE_UINT32, &stats->rx_packets);
1704 if (counters->tx_packets != stats->tx_packets || append_all) {
1705 counters->tx_packets = stats->tx_packets;
1706 connman_dbus_dict_append_basic(dict, "TX.Packets",
1707 DBUS_TYPE_UINT32, &stats->tx_packets);
1710 if (counters->rx_bytes != stats->rx_bytes || append_all) {
1711 counters->rx_bytes = stats->rx_bytes;
1712 connman_dbus_dict_append_basic(dict, "RX.Bytes",
1713 DBUS_TYPE_UINT32, &stats->rx_bytes);
1716 if (counters->tx_bytes != stats->tx_bytes || append_all) {
1717 counters->tx_bytes = stats->tx_bytes;
1718 connman_dbus_dict_append_basic(dict, "TX.Bytes",
1719 DBUS_TYPE_UINT32, &stats->tx_bytes);
1722 if (counters->rx_errors != stats->rx_errors || append_all) {
1723 counters->rx_errors = stats->rx_errors;
1724 connman_dbus_dict_append_basic(dict, "RX.Errors",
1725 DBUS_TYPE_UINT32, &stats->rx_errors);
1728 if (counters->tx_errors != stats->tx_errors || append_all) {
1729 counters->tx_errors = stats->tx_errors;
1730 connman_dbus_dict_append_basic(dict, "TX.Errors",
1731 DBUS_TYPE_UINT32, &stats->tx_errors);
1734 if (counters->rx_dropped != stats->rx_dropped || append_all) {
1735 counters->rx_dropped = stats->rx_dropped;
1736 connman_dbus_dict_append_basic(dict, "RX.Dropped",
1737 DBUS_TYPE_UINT32, &stats->rx_dropped);
1740 if (counters->tx_dropped != stats->tx_dropped || append_all) {
1741 counters->tx_dropped = stats->tx_dropped;
1742 connman_dbus_dict_append_basic(dict, "TX.Dropped",
1743 DBUS_TYPE_UINT32, &stats->tx_dropped);
1746 if (counters->time != stats->time || append_all) {
1747 counters->time = stats->time;
1748 connman_dbus_dict_append_basic(dict, "Time",
1749 DBUS_TYPE_UINT32, &stats->time);
1753 static void stats_append(struct connman_service *service,
1754 const char *counter,
1755 struct connman_stats_counter *counters,
1756 connman_bool_t append_all)
1758 DBusMessageIter array, dict;
1761 DBG("service %p counter %s", service, counter);
1763 msg = dbus_message_new(DBUS_MESSAGE_TYPE_METHOD_CALL);
1767 dbus_message_append_args(msg, DBUS_TYPE_OBJECT_PATH,
1768 &service->path, DBUS_TYPE_INVALID);
1770 dbus_message_iter_init_append(msg, &array);
1773 connman_dbus_dict_open(&array, &dict);
1775 stats_append_counters(&dict, &service->stats.data,
1776 &counters->stats.data, append_all);
1778 connman_dbus_dict_close(&array, &dict);
1780 /* roaming counter */
1781 connman_dbus_dict_open(&array, &dict);
1783 stats_append_counters(&dict, &service->stats_roaming.data,
1784 &counters->stats_roaming.data, append_all);
1786 connman_dbus_dict_close(&array, &dict);
1788 __connman_counter_send_usage(counter, msg);
1791 static void stats_update(struct connman_service *service,
1792 unsigned int rx_packets, unsigned int tx_packets,
1793 unsigned int rx_bytes, unsigned int tx_bytes,
1794 unsigned int rx_errors, unsigned int tx_errors,
1795 unsigned int rx_dropped, unsigned int tx_dropped)
1797 struct connman_stats *stats = stats_get(service);
1798 struct connman_stats_data *data_last = &stats->data_last;
1799 struct connman_stats_data *data = &stats->data;
1800 unsigned int seconds;
1802 DBG("service %p", service);
1804 if (stats->valid == TRUE) {
1806 rx_packets - data_last->rx_packets;
1808 tx_packets - data_last->tx_packets;
1810 rx_bytes - data_last->rx_bytes;
1812 tx_bytes - data_last->tx_bytes;
1814 rx_errors - data_last->rx_errors;
1816 tx_errors - data_last->tx_errors;
1818 rx_dropped - data_last->rx_dropped;
1820 tx_dropped - data_last->tx_dropped;
1822 stats->valid = TRUE;
1825 data_last->rx_packets = rx_packets;
1826 data_last->tx_packets = tx_packets;
1827 data_last->rx_bytes = rx_bytes;
1828 data_last->tx_bytes = tx_bytes;
1829 data_last->rx_errors = rx_errors;
1830 data_last->tx_errors = tx_errors;
1831 data_last->rx_dropped = rx_dropped;
1832 data_last->tx_dropped = tx_dropped;
1834 seconds = g_timer_elapsed(stats->timer, NULL);
1835 stats->data.time = stats->data_last.time + seconds;
1838 static char *wifi_build_group_name(const unsigned char *ssid,
1839 unsigned int ssid_len,
1841 const char *security)
1846 /* the last 3 is for the 2 '_' and '\0' */
1847 str = g_string_sized_new((ssid_len * 2) + strlen(mode)
1848 + strlen(security) + 3);
1852 for (i = 0; i < ssid_len; i++)
1853 g_string_append_printf(str, "%02x", ssid[i]);
1855 g_string_append_printf(str, "_%s_%s", mode, security);
1857 return g_string_free(str, FALSE);
1860 void __connman_service_notify(struct connman_service *service,
1861 unsigned int rx_packets, unsigned int tx_packets,
1862 unsigned int rx_bytes, unsigned int tx_bytes,
1863 unsigned int rx_errors, unsigned int tx_errors,
1864 unsigned int rx_dropped, unsigned int tx_dropped)
1866 GHashTableIter iter;
1867 gpointer key, value;
1868 const char *counter;
1869 struct connman_stats_counter *counters;
1870 struct connman_stats_data *data;
1873 if (service == NULL)
1876 if (is_connected(service) == FALSE)
1879 stats_update(service,
1880 rx_packets, tx_packets,
1882 rx_errors, tx_errors,
1883 rx_dropped, tx_dropped);
1885 data = &stats_get(service)->data;
1886 err = __connman_stats_update(service, service->roaming, data);
1888 connman_error("Failed to store statistics for %s",
1889 service->identifier);
1891 g_hash_table_iter_init(&iter, service->counter_table);
1892 while (g_hash_table_iter_next(&iter, &key, &value)) {
1896 stats_append(service, counter, counters, counters->append_all);
1897 counters->append_all = FALSE;
1901 int __connman_service_counter_register(const char *counter)
1903 struct connman_service *service;
1904 GSequenceIter *iter;
1905 struct connman_stats_counter *counters;
1907 DBG("counter %s", counter);
1909 counter_list = g_slist_append(counter_list, (gpointer)counter);
1911 iter = g_sequence_get_begin_iter(service_list);
1913 while (g_sequence_iter_is_end(iter) == FALSE) {
1914 service = g_sequence_get(iter);
1916 counters = g_try_new0(struct connman_stats_counter, 1);
1917 if (counters == NULL)
1920 counters->append_all = TRUE;
1922 g_hash_table_replace(service->counter_table, (gpointer)counter,
1925 iter = g_sequence_iter_next(iter);
1931 void __connman_service_counter_unregister(const char *counter)
1933 struct connman_service *service;
1934 GSequenceIter *iter;
1936 DBG("counter %s", counter);
1938 iter = g_sequence_get_begin_iter(service_list);
1940 while (g_sequence_iter_is_end(iter) == FALSE) {
1941 service = g_sequence_get(iter);
1943 g_hash_table_remove(service->counter_table, counter);
1945 iter = g_sequence_iter_next(iter);
1948 counter_list = g_slist_remove(counter_list, counter);
1951 GSequence *__connman_service_get_list(struct connman_session *session,
1952 service_match_cb service_match,
1953 create_service_entry_cb create_service_entry,
1954 GDestroyNotify destroy_service_entry)
1957 GSequenceIter *iter;
1958 struct connman_service *service;
1959 struct service_entry *entry;
1961 list = g_sequence_new(destroy_service_entry);
1965 iter = g_sequence_get_begin_iter(service_list);
1967 while (g_sequence_iter_is_end(iter) == FALSE) {
1968 service = g_sequence_get(iter);
1970 if (service_match(session, service) == TRUE) {
1971 entry = create_service_entry(service, service->name,
1976 g_sequence_append(list, entry);
1979 iter = g_sequence_iter_next(iter);
1985 void __connman_service_session_inc(struct connman_service *service)
1987 DBG("service %p ref count %d", service,
1988 g_atomic_int_get(&service->session_usage_count) + 1);
1990 g_atomic_int_inc(&service->session_usage_count);
1993 connman_bool_t __connman_service_session_dec(struct connman_service *service)
1995 DBG("service %p ref count %d", service,
1996 g_atomic_int_get(&service->session_usage_count) - 1);
1998 if (g_atomic_int_dec_and_test(&service->session_usage_count) == FALSE)
2004 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
2005 struct connman_service *service)
2007 dbus_bool_t required;
2010 str = __connman_service_type2string(service->type);
2012 connman_dbus_dict_append_basic(dict, "Type",
2013 DBUS_TYPE_STRING, &str);
2015 connman_dbus_dict_append_array(dict, "Security",
2016 DBUS_TYPE_STRING, append_security, service);
2018 str = state2string(service->state);
2020 connman_dbus_dict_append_basic(dict, "State",
2021 DBUS_TYPE_STRING, &str);
2023 str = error2string(service->error);
2025 connman_dbus_dict_append_basic(dict, "Error",
2026 DBUS_TYPE_STRING, &str);
2028 if (service->strength > 0)
2029 connman_dbus_dict_append_basic(dict, "Strength",
2030 DBUS_TYPE_BYTE, &service->strength);
2032 connman_dbus_dict_append_basic(dict, "Favorite",
2033 DBUS_TYPE_BOOLEAN, &service->favorite);
2035 connman_dbus_dict_append_basic(dict, "Immutable",
2036 DBUS_TYPE_BOOLEAN, &service->immutable);
2038 if (service->favorite == TRUE)
2039 connman_dbus_dict_append_basic(dict, "AutoConnect",
2040 DBUS_TYPE_BOOLEAN, &service->autoconnect);
2042 connman_dbus_dict_append_basic(dict, "AutoConnect",
2043 DBUS_TYPE_BOOLEAN, &service->favorite);
2045 if (service->name != NULL)
2046 connman_dbus_dict_append_basic(dict, "Name",
2047 DBUS_TYPE_STRING, &service->name);
2049 connman_dbus_dict_append_basic(dict, "LoginRequired",
2050 DBUS_TYPE_BOOLEAN, &service->login_required);
2052 switch (service->type) {
2053 case CONNMAN_SERVICE_TYPE_UNKNOWN:
2054 case CONNMAN_SERVICE_TYPE_SYSTEM:
2055 case CONNMAN_SERVICE_TYPE_GPS:
2056 case CONNMAN_SERVICE_TYPE_VPN:
2057 case CONNMAN_SERVICE_TYPE_GADGET:
2059 case CONNMAN_SERVICE_TYPE_CELLULAR:
2060 connman_dbus_dict_append_basic(dict, "Roaming",
2061 DBUS_TYPE_BOOLEAN, &service->roaming);
2064 connman_dbus_dict_append_basic(dict, "SetupRequired",
2065 DBUS_TYPE_BOOLEAN, &required);
2066 connman_dbus_dict_append_dict(dict, "Ethernet",
2067 append_ethernet, service);
2069 case CONNMAN_SERVICE_TYPE_WIFI:
2070 if (service->passphrase != NULL && limited == FALSE)
2071 connman_dbus_dict_append_basic(dict, "Passphrase",
2072 DBUS_TYPE_STRING, &service->passphrase);
2076 switch (service->security) {
2077 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
2078 case CONNMAN_SERVICE_SECURITY_NONE:
2080 case CONNMAN_SERVICE_SECURITY_WEP:
2081 case CONNMAN_SERVICE_SECURITY_PSK:
2082 case CONNMAN_SERVICE_SECURITY_WPA:
2083 case CONNMAN_SERVICE_SECURITY_RSN:
2084 if (service->passphrase == NULL)
2087 case CONNMAN_SERVICE_SECURITY_8021X:
2091 connman_dbus_dict_append_basic(dict, "PassphraseRequired",
2092 DBUS_TYPE_BOOLEAN, &required);
2095 case CONNMAN_SERVICE_TYPE_ETHERNET:
2096 case CONNMAN_SERVICE_TYPE_WIMAX:
2097 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
2098 connman_dbus_dict_append_dict(dict, "Ethernet",
2099 append_ethernet, service);
2103 connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
2105 connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
2106 append_ipv4config, service);
2108 connman_dbus_dict_append_dict(dict, "IPv6", append_ipv6, service);
2110 connman_dbus_dict_append_dict(dict, "IPv6.Configuration",
2111 append_ipv6config, service);
2113 connman_dbus_dict_append_array(dict, "Nameservers",
2114 DBUS_TYPE_STRING, append_dns, service);
2116 connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
2117 DBUS_TYPE_STRING, append_dnsconfig, service);
2119 connman_dbus_dict_append_array(dict, "Domains",
2120 DBUS_TYPE_STRING, append_domain, service);
2122 connman_dbus_dict_append_array(dict, "Domains.Configuration",
2123 DBUS_TYPE_STRING, append_domainconfig, service);
2125 connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
2127 connman_dbus_dict_append_dict(dict, "Proxy.Configuration",
2128 append_proxyconfig, service);
2130 connman_dbus_dict_append_dict(dict, "Provider",
2131 append_provider, service);
2134 static void append_struct(gpointer value, gpointer user_data)
2136 struct connman_service *service = value;
2137 DBusMessageIter *iter = user_data;
2138 DBusMessageIter entry, dict;
2140 if (service->path == NULL || service->hidden == TRUE)
2143 dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
2145 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2148 connman_dbus_dict_open(&entry, &dict);
2149 append_properties(&dict, TRUE, service);
2150 connman_dbus_dict_close(&entry, &dict);
2152 dbus_message_iter_close_container(iter, &entry);
2155 void __connman_service_list_struct(DBusMessageIter *iter)
2157 g_sequence_foreach(service_list, append_struct, iter);
2160 int __connman_service_get_index(struct connman_service *service)
2162 if (service == NULL)
2165 if (service->network != NULL)
2166 return connman_network_get_index(service->network);
2167 else if (service->provider != NULL)
2168 return connman_provider_get_index(service->provider);
2173 void __connman_service_set_domainname(struct connman_service *service,
2174 const char *domainname)
2176 if (service == NULL)
2179 g_free(service->domainname);
2180 service->domainname = g_strdup(domainname);
2182 domain_changed(service);
2185 const char *connman_service_get_domainname(struct connman_service *service)
2187 if (service == NULL)
2190 if (service->domains != NULL)
2191 return service->domains[0];
2193 return service->domainname;
2196 char **connman_service_get_nameservers(struct connman_service *service)
2198 if (service == NULL)
2201 if (service->nameservers_config != NULL)
2202 return service->nameservers_config;
2203 else if (service->nameservers != NULL)
2204 return service->nameservers;
2209 void connman_service_set_proxy_method(struct connman_service *service,
2210 enum connman_service_proxy_method method)
2212 if (service == NULL)
2215 service->proxy = method;
2217 proxy_changed(service);
2219 if (method != CONNMAN_SERVICE_PROXY_METHOD_AUTO)
2220 __connman_notifier_proxy_changed(service);
2223 enum connman_service_proxy_method connman_service_get_proxy_method(
2224 struct connman_service *service)
2226 if (service == NULL)
2227 return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2229 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN) {
2230 if (service->proxy_config == CONNMAN_SERVICE_PROXY_METHOD_AUTO &&
2231 service->pac == NULL)
2232 return service->proxy;
2234 return service->proxy_config;
2237 return service->proxy;
2240 char **connman_service_get_proxy_servers(struct connman_service *service)
2242 return g_strdupv(service->proxies);
2245 char **connman_service_get_proxy_excludes(struct connman_service *service)
2247 return g_strdupv(service->excludes);
2250 const char *connman_service_get_proxy_url(struct connman_service *service)
2252 if (service == NULL)
2255 return service->pac;
2258 void __connman_service_set_proxy_autoconfig(struct connman_service *service,
2261 if (service == NULL)
2264 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_AUTO;
2266 if (service->ipconfig_ipv4) {
2267 if (__connman_ipconfig_set_proxy_autoconfig(
2268 service->ipconfig_ipv4, url) < 0)
2270 } else if (service->ipconfig_ipv6) {
2271 if (__connman_ipconfig_set_proxy_autoconfig(
2272 service->ipconfig_ipv6, url) < 0)
2277 proxy_changed(service);
2279 __connman_notifier_proxy_changed(service);
2282 const char *connman_service_get_proxy_autoconfig(struct connman_service *service)
2284 if (service == NULL)
2287 if (service->ipconfig_ipv4)
2288 return __connman_ipconfig_get_proxy_autoconfig(
2289 service->ipconfig_ipv4);
2290 else if (service->ipconfig_ipv6)
2291 return __connman_ipconfig_get_proxy_autoconfig(
2292 service->ipconfig_ipv6);
2296 static void update_timeservers(struct connman_service *service)
2300 if (service->timeservers == NULL)
2303 switch (service->state) {
2304 case CONNMAN_SERVICE_STATE_UNKNOWN:
2305 case CONNMAN_SERVICE_STATE_IDLE:
2306 case CONNMAN_SERVICE_STATE_ASSOCIATION:
2307 case CONNMAN_SERVICE_STATE_CONFIGURATION:
2309 case CONNMAN_SERVICE_STATE_FAILURE:
2310 case CONNMAN_SERVICE_STATE_DISCONNECT:
2311 for (i = 0; service->timeservers[i] != NULL; i++)
2312 connman_timeserver_remove(service->timeservers[i]);
2314 case CONNMAN_SERVICE_STATE_READY:
2315 case CONNMAN_SERVICE_STATE_ONLINE:
2319 for (i = 0; service->timeservers[i] != NULL; i++)
2320 connman_timeserver_append(service->timeservers[i]);
2323 int __connman_service_timeserver_append(struct connman_service *service,
2324 const char *timeserver)
2328 DBG("service %p timeserver %s", service, timeserver);
2330 if (timeserver == NULL)
2333 if (service->timeservers != NULL) {
2336 for (i = 0; service->timeservers[i] != NULL; i++)
2337 if (g_strcmp0(service->timeservers[i], timeserver) == 0)
2340 len = g_strv_length(service->timeservers);
2341 service->timeservers = g_try_renew(char *, service->timeservers,
2345 service->timeservers = g_try_new0(char *, len + 2);
2348 if (service->timeservers == NULL)
2351 service->timeservers[len] = g_strdup(timeserver);
2352 service->timeservers[len + 1] = NULL;
2354 update_timeservers(service);
2359 int __connman_service_timeserver_remove(struct connman_service *service,
2360 const char *timeserver)
2365 DBG("service %p timeserver %s", service, timeserver);
2367 if (timeserver == NULL)
2370 if (service->timeservers == NULL)
2373 len = g_strv_length(service->timeservers);
2375 if (g_strcmp0(service->timeservers[0], timeserver) != 0)
2378 g_strfreev(service->timeservers);
2379 service->timeservers = NULL;
2384 servers = g_try_new0(char *, len - 1);
2385 if (servers == NULL)
2388 for (i = 0, j = 0; i < len; i++) {
2389 if (g_strcmp0(service->timeservers[i], timeserver) != 0) {
2390 servers[j] = g_strdup(service->timeservers[i]);
2394 servers[len - 2] = NULL;
2396 g_strfreev(service->timeservers);
2397 service->timeservers = servers;
2399 update_timeservers(service);
2404 void __connman_service_set_pac(struct connman_service *service,
2407 g_free(service->pac);
2408 service->pac = g_strdup(pac);
2410 proxy_changed(service);
2413 void __connman_service_set_identity(struct connman_service *service,
2414 const char *identity)
2416 if (service->immutable)
2419 g_free(service->identity);
2420 service->identity = g_strdup(identity);
2422 if (service->network != NULL)
2423 connman_network_set_string(service->network,
2428 void __connman_service_set_agent_identity(struct connman_service *service,
2429 const char *agent_identity)
2431 g_free(service->agent_identity);
2432 service->agent_identity = g_strdup(agent_identity);
2434 if (service->network != NULL)
2435 connman_network_set_string(service->network,
2436 "WiFi.AgentIdentity",
2437 service->agent_identity);
2440 void __connman_service_set_passphrase(struct connman_service *service,
2441 const char* passphrase)
2443 if (service->immutable == TRUE)
2446 g_free(service->passphrase);
2447 service->passphrase = g_strdup(passphrase);
2449 passphrase_changed(service);
2451 if (service->network != NULL)
2452 connman_network_set_string(service->network,
2454 service->passphrase);
2456 service_save(service);
2459 void __connman_service_set_agent_passphrase(struct connman_service *service,
2460 const char *agent_passphrase)
2462 g_free(service->agent_passphrase);
2463 service->agent_passphrase = g_strdup(agent_passphrase);
2465 if (service->network != NULL)
2466 connman_network_set_string(service->network,
2467 "WiFi.AgentPassphrase",
2468 service->agent_passphrase);
2471 static DBusMessage *get_properties(DBusConnection *conn,
2472 DBusMessage *msg, void *user_data)
2474 struct connman_service *service = user_data;
2476 DBusMessageIter array, dict;
2478 DBG("service %p", service);
2480 reply = dbus_message_new_method_return(msg);
2484 dbus_message_iter_init_append(reply, &array);
2486 connman_dbus_dict_open(&array, &dict);
2487 append_properties(&dict, FALSE, service);
2488 connman_dbus_dict_close(&array, &dict);
2493 static int update_proxy_configuration(struct connman_service *service,
2494 DBusMessageIter *array)
2496 DBusMessageIter dict;
2497 enum connman_service_proxy_method method;
2498 GString *servers_str = NULL;
2499 GString *excludes_str = NULL;
2500 const char *url = NULL;
2502 method = CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
2504 dbus_message_iter_recurse(array, &dict);
2506 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2507 DBusMessageIter entry, variant;
2511 dbus_message_iter_recurse(&dict, &entry);
2513 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2516 dbus_message_iter_get_basic(&entry, &key);
2517 dbus_message_iter_next(&entry);
2519 if (dbus_message_iter_get_arg_type(&entry) !=
2523 dbus_message_iter_recurse(&entry, &variant);
2524 type = dbus_message_iter_get_arg_type(&variant);
2526 if (g_str_equal(key, "Method") == TRUE) {
2529 if (type != DBUS_TYPE_STRING)
2532 dbus_message_iter_get_basic(&variant, &val);
2533 method = string2proxymethod(val);
2534 } else if (g_str_equal(key, "URL") == TRUE) {
2535 if (type != DBUS_TYPE_STRING)
2538 dbus_message_iter_get_basic(&variant, &url);
2539 } else if (g_str_equal(key, "Servers") == TRUE) {
2540 DBusMessageIter str_array;
2542 if (type != DBUS_TYPE_ARRAY)
2545 servers_str = g_string_new(NULL);
2546 if (servers_str == NULL)
2549 dbus_message_iter_recurse(&variant, &str_array);
2551 while (dbus_message_iter_get_arg_type(&str_array) ==
2555 dbus_message_iter_get_basic(&str_array, &val);
2557 if (servers_str->len > 0)
2558 g_string_append_printf(servers_str,
2561 g_string_append(servers_str, val);
2563 dbus_message_iter_next(&str_array);
2565 } else if (g_str_equal(key, "Excludes") == TRUE) {
2566 DBusMessageIter str_array;
2568 if (type != DBUS_TYPE_ARRAY)
2571 excludes_str = g_string_new(NULL);
2572 if (excludes_str == NULL)
2575 dbus_message_iter_recurse(&variant, &str_array);
2577 while (dbus_message_iter_get_arg_type(&str_array) ==
2581 dbus_message_iter_get_basic(&str_array, &val);
2583 if (excludes_str->len > 0)
2584 g_string_append_printf(excludes_str,
2587 g_string_append(excludes_str, val);
2589 dbus_message_iter_next(&str_array);
2593 dbus_message_iter_next(&dict);
2597 case CONNMAN_SERVICE_PROXY_METHOD_DIRECT:
2599 case CONNMAN_SERVICE_PROXY_METHOD_MANUAL:
2600 if (servers_str == NULL && service->proxies == NULL)
2603 if (servers_str != NULL) {
2604 g_strfreev(service->proxies);
2606 if (servers_str->len > 0)
2607 service->proxies = g_strsplit_set(
2608 servers_str->str, " ", 0);
2610 service->proxies = NULL;
2613 if (excludes_str != NULL) {
2614 g_strfreev(service->excludes);
2616 if (excludes_str->len > 0)
2617 service->excludes = g_strsplit_set(
2618 excludes_str->str, " ", 0);
2620 service->excludes = NULL;
2623 if (service->proxies == NULL)
2624 method = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
2627 case CONNMAN_SERVICE_PROXY_METHOD_AUTO:
2628 g_free(service->pac);
2630 if (url != NULL && strlen(url) > 0)
2631 service->pac = g_strdup(url);
2633 service->pac = NULL;
2635 /* if we are connected:
2636 - if service->pac == NULL
2637 - if __connman_ipconfig_get_proxy_autoconfig(
2638 service->ipconfig) == NULL
2639 --> We should start WPAD */
2642 case CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN:
2646 if (servers_str != NULL)
2647 g_string_free(servers_str, TRUE);
2649 if (excludes_str != NULL)
2650 g_string_free(excludes_str, TRUE);
2652 service->proxy_config = method;
2657 if (servers_str != NULL)
2658 g_string_free(servers_str, TRUE);
2660 if (excludes_str != NULL)
2661 g_string_free(excludes_str, TRUE);
2666 static int set_ipconfig(struct connman_service *service,
2667 struct connman_ipconfig *ipconfig,
2668 DBusMessageIter *array,
2669 enum connman_service_state state,
2670 enum connman_service_state *new_state)
2672 enum connman_ipconfig_method old_method;
2673 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2674 enum connman_ipconfig_type type;
2677 if (ipconfig == NULL)
2680 old_method = __connman_ipconfig_get_method(ipconfig);
2682 if (is_connecting_state(service, state) ||
2683 is_connected_state(service, state))
2684 __connman_network_clear_ipconfig(service->network, ipconfig);
2686 err = __connman_ipconfig_set_config(ipconfig, array);
2687 method = __connman_ipconfig_get_method(ipconfig);
2688 type = __connman_ipconfig_get_config_type(ipconfig);
2690 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
2691 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2692 method == CONNMAN_IPCONFIG_METHOD_DHCP) {
2693 *new_state = service->state_ipv4 =
2694 CONNMAN_SERVICE_STATE_CONFIGURATION;
2695 __connman_ipconfig_enable(ipconfig);
2698 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2699 if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
2700 method == CONNMAN_IPCONFIG_METHOD_AUTO) {
2701 *new_state = service->state_ipv6;
2702 __connman_ipconfig_enable(ipconfig);
2706 DBG("err %d ipconfig %p type %d method %d state %s", err, ipconfig,
2707 type, method, state2string(*new_state));
2712 static DBusMessage *set_property(DBusConnection *conn,
2713 DBusMessage *msg, void *user_data)
2715 struct connman_service *service = user_data;
2716 DBusMessageIter iter, value;
2720 DBG("service %p", service);
2722 if (dbus_message_iter_init(msg, &iter) == FALSE)
2723 return __connman_error_invalid_arguments(msg);
2725 dbus_message_iter_get_basic(&iter, &name);
2726 dbus_message_iter_next(&iter);
2727 dbus_message_iter_recurse(&iter, &value);
2729 type = dbus_message_iter_get_arg_type(&value);
2731 if (g_str_equal(name, "AutoConnect") == TRUE) {
2732 connman_bool_t autoconnect;
2734 if (type != DBUS_TYPE_BOOLEAN)
2735 return __connman_error_invalid_arguments(msg);
2737 if (service->favorite == FALSE)
2738 return __connman_error_invalid_service(msg);
2740 dbus_message_iter_get_basic(&value, &autoconnect);
2742 if (service->autoconnect == autoconnect)
2743 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2745 service->autoconnect = autoconnect;
2747 autoconnect_changed(service);
2749 service_save(service);
2750 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2751 const char *passphrase;
2753 if (type != DBUS_TYPE_STRING)
2754 return __connman_error_invalid_arguments(msg);
2756 if (service->immutable == TRUE)
2757 return __connman_error_not_supported(msg);
2759 dbus_message_iter_get_basic(&value, &passphrase);
2761 __connman_service_set_passphrase(service, passphrase);
2762 } else if (g_str_equal(name, "Nameservers.Configuration") == TRUE) {
2763 DBusMessageIter entry;
2768 if (type != DBUS_TYPE_ARRAY)
2769 return __connman_error_invalid_arguments(msg);
2771 str = g_string_new(NULL);
2773 return __connman_error_invalid_arguments(msg);
2775 index = connman_network_get_index(service->network);
2776 gw = __connman_ipconfig_get_gateway_from_index(index);
2778 if (gw && strlen(gw))
2779 __connman_service_nameserver_del_routes(service);
2781 dbus_message_iter_recurse(&value, &entry);
2783 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2785 dbus_message_iter_get_basic(&entry, &val);
2786 dbus_message_iter_next(&entry);
2788 g_string_append_printf(str, " %s", val);
2790 g_string_append(str, val);
2793 g_strfreev(service->nameservers_config);
2796 service->nameservers_config =
2797 g_strsplit_set(str->str, " ", 0);
2799 service->nameservers_config = NULL;
2802 g_string_free(str, TRUE);
2804 if (gw && strlen(gw))
2805 __connman_service_nameserver_add_routes(service, gw);
2807 update_nameservers(service);
2808 dns_configuration_changed(service);
2810 service_save(service);
2811 } else if (g_str_equal(name, "Domains.Configuration") == TRUE) {
2812 DBusMessageIter entry;
2815 if (type != DBUS_TYPE_ARRAY)
2816 return __connman_error_invalid_arguments(msg);
2818 str = g_string_new(NULL);
2820 return __connman_error_invalid_arguments(msg);
2822 dbus_message_iter_recurse(&value, &entry);
2824 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
2826 dbus_message_iter_get_basic(&entry, &val);
2827 dbus_message_iter_next(&entry);
2829 g_string_append_printf(str, " %s", val);
2831 g_string_append(str, val);
2834 g_strfreev(service->domains);
2837 service->domains = g_strsplit_set(str->str, " ", 0);
2839 service->domains = NULL;
2841 g_string_free(str, TRUE);
2843 update_nameservers(service);
2844 domain_configuration_changed(service);
2846 service_save(service);
2847 } else if (g_str_equal(name, "Proxy.Configuration") == TRUE) {
2850 if (type != DBUS_TYPE_ARRAY)
2851 return __connman_error_invalid_arguments(msg);
2853 err = update_proxy_configuration(service, &value);
2856 return __connman_error_failed(msg, -err);
2858 proxy_configuration_changed(service);
2860 __connman_notifier_proxy_changed(service);
2862 service_save(service);
2863 } else if (g_str_equal(name, "IPv4.Configuration") == TRUE ||
2864 g_str_equal(name, "IPv6.Configuration")) {
2866 struct connman_ipconfig *ipv4 = NULL, *ipv6 = NULL;
2867 enum connman_service_state state =
2868 CONNMAN_SERVICE_STATE_UNKNOWN;
2873 if (service->ipconfig_ipv4 == NULL &&
2874 service->ipconfig_ipv6 == NULL)
2875 return __connman_error_invalid_property(msg);
2877 if (g_str_equal(name, "IPv4.Configuration") == TRUE) {
2878 ipv4 = service->ipconfig_ipv4;
2879 err = set_ipconfig(service, ipv4, &value,
2880 service->state_ipv4, &state);
2882 } else if (g_str_equal(name, "IPv6.Configuration") == TRUE) {
2883 ipv6 = service->ipconfig_ipv6;
2884 err = set_ipconfig(service, ipv6, &value,
2885 service->state_ipv6, &state);
2889 if (is_connected_state(service, state) ||
2890 is_connecting_state(service, state))
2891 __connman_network_set_ipconfig(service->network,
2893 return __connman_error_failed(msg, -err);
2897 ipv4_configuration_changed(service);
2899 ipv6_configuration_changed(service);
2901 if (is_connecting(service) || is_connected(service))
2902 __connman_network_set_ipconfig(service->network,
2905 service_save(service);
2907 return __connman_error_invalid_property(msg);
2909 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2912 static void set_idle(struct connman_service *service)
2914 service->state = service->state_ipv4 = service->state_ipv6 =
2915 CONNMAN_SERVICE_STATE_IDLE;
2916 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
2917 state_changed(service);
2920 static DBusMessage *clear_property(DBusConnection *conn,
2921 DBusMessage *msg, void *user_data)
2923 struct connman_service *service = user_data;
2926 DBG("service %p", service);
2928 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
2931 if (g_str_equal(name, "Error") == TRUE) {
2934 g_get_current_time(&service->modified);
2935 service_save(service);
2936 } else if (g_str_equal(name, "Passphrase") == TRUE) {
2937 if (service->immutable == TRUE)
2938 return __connman_error_not_supported(msg);
2940 g_free(service->passphrase);
2941 service->passphrase = NULL;
2943 passphrase_changed(service);
2945 service_save(service);
2947 return __connman_error_invalid_property(msg);
2949 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
2952 static connman_bool_t is_ignore(struct connman_service *service)
2954 if (service->autoconnect == FALSE)
2957 if (service->roaming == TRUE)
2960 if (service->ignore == TRUE)
2963 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
2969 void __connman_service_auto_connect(void)
2971 struct connman_service *service = NULL;
2972 GSequenceIter *iter;
2976 if (__connman_session_mode() == TRUE) {
2977 DBG("Session mode enabled: auto connect disabled");
2981 iter = g_sequence_get_begin_iter(service_list);
2983 while (g_sequence_iter_is_end(iter) == FALSE) {
2984 service = g_sequence_get(iter);
2986 if (service->pending != NULL)
2989 if (is_connecting(service) == TRUE)
2992 if (service->favorite == FALSE)
2995 if (is_connected(service) == TRUE)
2998 if (is_ignore(service) == FALSE && service->state ==
2999 CONNMAN_SERVICE_STATE_IDLE)
3004 iter = g_sequence_iter_next(iter);
3007 if (service != NULL) {
3008 service->userconnect = FALSE;
3009 __connman_service_connect(service);
3013 static void remove_timeout(struct connman_service *service)
3015 if (service->timeout > 0) {
3016 g_source_remove(service->timeout);
3017 service->timeout = 0;
3021 static void reply_pending(struct connman_service *service, int error)
3023 remove_timeout(service);
3025 if (service->pending != NULL) {
3029 reply = __connman_error_failed(service->pending,
3032 g_dbus_send_message(connection, reply);
3036 sender = dbus_message_get_interface(service->pending);
3038 DBG("sender %s", sender);
3040 if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
3041 g_dbus_send_reply(connection, service->pending,
3042 DBUS_TYPE_OBJECT_PATH, &service->path,
3045 g_dbus_send_reply(connection, service->pending,
3049 dbus_message_unref(service->pending);
3050 service->pending = NULL;
3054 static gboolean connect_timeout(gpointer user_data)
3056 struct connman_service *service = user_data;
3057 connman_bool_t autoconnect = FALSE;
3059 DBG("service %p", service);
3061 service->timeout = 0;
3063 if (service->network != NULL)
3064 __connman_network_disconnect(service->network);
3066 __connman_ipconfig_disable(service->ipconfig_ipv4);
3067 __connman_ipconfig_disable(service->ipconfig_ipv6);
3069 __connman_stats_service_unregister(service);
3071 if (service->pending != NULL) {
3074 reply = __connman_error_operation_timeout(service->pending);
3076 g_dbus_send_message(connection, reply);
3078 dbus_message_unref(service->pending);
3079 service->pending = NULL;
3083 __connman_service_ipconfig_indicate_state(service,
3084 CONNMAN_SERVICE_STATE_FAILURE,
3085 CONNMAN_IPCONFIG_TYPE_IPV4);
3086 __connman_service_ipconfig_indicate_state(service,
3087 CONNMAN_SERVICE_STATE_FAILURE,
3088 CONNMAN_IPCONFIG_TYPE_IPV6);
3090 if (autoconnect == TRUE && service->userconnect == FALSE)
3091 __connman_service_auto_connect();
3096 static void set_reconnect_state(struct connman_service *service,
3097 connman_bool_t reconnect)
3099 struct connman_device *device;
3101 if (service->network == NULL)
3104 device = connman_network_get_device(service->network);
3108 __connman_device_set_reconnect(device, reconnect);
3111 static connman_bool_t get_reconnect_state(struct connman_service *service)
3113 struct connman_device *device;
3115 if (service->network == NULL)
3118 device = connman_network_get_device(service->network);
3122 return __connman_device_get_reconnect(device);
3125 static DBusMessage *connect_service(DBusConnection *conn,
3126 DBusMessage *msg, void *user_data)
3128 struct connman_service *service = user_data;
3129 GSequenceIter *iter;
3132 DBG("service %p", service);
3134 if (service->pending != NULL)
3135 return __connman_error_in_progress(msg);
3137 iter = g_sequence_get_begin_iter(service_list);
3139 while (g_sequence_iter_is_end(iter) == FALSE) {
3140 struct connman_service *temp = g_sequence_get(iter);
3142 if (service->type == temp->type && is_connecting(temp) == TRUE)
3143 return __connman_error_in_progress(msg);
3145 iter = g_sequence_iter_next(iter);
3148 service->ignore = FALSE;
3150 service->userconnect = TRUE;
3152 service->pending = dbus_message_ref(msg);
3154 set_reconnect_state(service, FALSE);
3156 err = __connman_service_connect(service);
3158 if (service->pending == NULL)
3161 if (err != -EINPROGRESS) {
3162 dbus_message_unref(service->pending);
3163 service->pending = NULL;
3165 return __connman_error_failed(msg, -err);
3171 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3174 static DBusMessage *disconnect_service(DBusConnection *conn,
3175 DBusMessage *msg, void *user_data)
3177 struct connman_service *service = user_data;
3180 DBG("service %p", service);
3182 reply_pending(service, ECONNABORTED);
3184 service->ignore = TRUE;
3186 set_reconnect_state(service, FALSE);
3188 err = __connman_service_disconnect(service);
3190 if (err != -EINPROGRESS)
3191 return __connman_error_failed(msg, -err);
3196 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3199 static DBusMessage *remove_service(DBusConnection *conn,
3200 DBusMessage *msg, void *user_data)
3202 struct connman_service *service = user_data;
3204 DBG("service %p", service);
3206 if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
3207 return __connman_error_not_supported(msg);
3209 if (service->immutable == TRUE)
3210 return __connman_error_not_supported(msg);
3212 if (service->favorite == FALSE && service->state !=
3213 CONNMAN_SERVICE_STATE_FAILURE)
3214 return __connman_error_not_supported(msg);
3216 set_reconnect_state(service, FALSE);
3218 __connman_service_disconnect(service);
3220 g_free(service->passphrase);
3221 service->passphrase = NULL;
3223 passphrase_changed(service);
3227 __connman_service_set_favorite(service, FALSE);
3228 service_save(service);
3230 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3233 static gboolean check_suitable_state(enum connman_service_state a,
3234 enum connman_service_state b)
3237 * Special check so that "ready" service can be moved before
3240 if ((a == CONNMAN_SERVICE_STATE_ONLINE &&
3241 b == CONNMAN_SERVICE_STATE_READY) ||
3242 (b == CONNMAN_SERVICE_STATE_ONLINE &&
3243 a == CONNMAN_SERVICE_STATE_READY))
3249 static DBusMessage *move_service(DBusConnection *conn,
3250 DBusMessage *msg, void *user_data,
3253 struct connman_service *service = user_data;
3254 struct connman_service *target;
3256 GSequenceIter *src, *dst;
3257 enum connman_ipconfig_method target4, target6;
3258 enum connman_ipconfig_method service4, service6;
3260 DBG("service %p", service);
3262 dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
3265 if (service->favorite == FALSE)
3266 return __connman_error_not_supported(msg);
3268 target = find_service(path);
3269 if (target == NULL || target->favorite == FALSE || target == service ||
3270 target->type == CONNMAN_SERVICE_TYPE_VPN)
3271 return __connman_error_invalid_service(msg);
3273 target4 = __connman_ipconfig_get_method(target->ipconfig_ipv4);
3274 target6 = __connman_ipconfig_get_method(target->ipconfig_ipv6);
3275 service4 = __connman_ipconfig_get_method(service->ipconfig_ipv4);
3276 service6 = __connman_ipconfig_get_method(service->ipconfig_ipv6);
3278 DBG("target %s method %d/%d state %d/%d", target->identifier,
3280 target->state_ipv4, target->state_ipv6);
3282 DBG("service %s method %d/%d state %d/%d", service->identifier,
3284 service->state_ipv4, service->state_ipv6);
3287 * If method is OFF, then we do not need to check the corresponding
3290 if (target4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3291 if (service6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3292 if (check_suitable_state(target->state_ipv6,
3293 service->state_ipv6) == FALSE)
3294 return __connman_error_invalid_service(msg);
3298 if (target6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3299 if (service4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3300 if (check_suitable_state(target->state_ipv4,
3301 service->state_ipv4) == FALSE)
3302 return __connman_error_invalid_service(msg);
3306 if (service4 == CONNMAN_IPCONFIG_METHOD_OFF) {
3307 if (target6 != CONNMAN_IPCONFIG_METHOD_OFF) {
3308 if (check_suitable_state(target->state_ipv6,
3309 service->state_ipv6) == FALSE)
3310 return __connman_error_invalid_service(msg);
3314 if (service6 == CONNMAN_IPCONFIG_METHOD_OFF) {
3315 if (target4 != CONNMAN_IPCONFIG_METHOD_OFF) {
3316 if (check_suitable_state(target->state_ipv4,
3317 service->state_ipv4) == FALSE)
3318 return __connman_error_invalid_service(msg);
3322 g_get_current_time(&service->modified);
3323 service_save(service);
3325 src = g_hash_table_lookup(service_hash, service->identifier);
3326 dst = g_hash_table_lookup(service_hash, target->identifier);
3328 before ? g_sequence_move(src, dst) : g_sequence_move(dst, src);
3330 services_changed(FALSE);
3332 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3335 static DBusMessage *move_before(DBusConnection *conn,
3336 DBusMessage *msg, void *user_data)
3338 return move_service(conn, msg, user_data, TRUE);
3341 static DBusMessage *move_after(DBusConnection *conn,
3342 DBusMessage *msg, void *user_data)
3344 return move_service(conn, msg, user_data, FALSE);
3347 static DBusMessage *reset_counters(DBusConnection *conn,
3348 DBusMessage *msg, void *user_data)
3350 struct connman_service *service = user_data;
3352 reset_stats(service);
3354 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
3357 static GDBusMethodTable service_methods[] = {
3358 { "GetProperties", "", "a{sv}", get_properties },
3359 { "SetProperty", "sv", "", set_property },
3360 { "ClearProperty", "s", "", clear_property },
3361 { "Connect", "", "", connect_service,
3362 G_DBUS_METHOD_FLAG_ASYNC },
3363 { "Disconnect", "", "", disconnect_service },
3364 { "Remove", "", "", remove_service },
3365 { "MoveBefore", "o", "", move_before },
3366 { "MoveAfter", "o", "", move_after },
3367 { "ResetCounters", "", "", reset_counters },
3371 static GDBusSignalTable service_signals[] = {
3372 { "PropertyChanged", "sv" },
3376 static void service_free(gpointer user_data)
3378 struct connman_service *service = user_data;
3379 char *path = service->path;
3381 DBG("service %p", service);
3383 reply_pending(service, ENOENT);
3385 g_hash_table_remove(service_hash, service->identifier);
3387 __connman_notifier_service_remove(service);
3389 stats_stop(service);
3391 service->path = NULL;
3394 services_changed(FALSE);
3396 g_dbus_unregister_interface(connection, path,
3397 CONNMAN_SERVICE_INTERFACE);
3401 g_hash_table_destroy(service->counter_table);
3403 if (service->network != NULL) {
3404 if (service->network_created == TRUE)
3405 connman_network_unref(service->network);
3408 if (service->provider != NULL)
3409 connman_provider_unref(service->provider);
3411 if (service->ipconfig_ipv4 != NULL) {
3412 connman_ipconfig_set_ops(service->ipconfig_ipv4, NULL);
3413 connman_ipconfig_set_data(service->ipconfig_ipv4, NULL);
3414 connman_ipconfig_unref(service->ipconfig_ipv4);
3415 service->ipconfig_ipv4 = NULL;
3418 if (service->ipconfig_ipv6 != NULL) {
3419 connman_ipconfig_set_ops(service->ipconfig_ipv6, NULL);
3420 connman_ipconfig_set_data(service->ipconfig_ipv6, NULL);
3421 connman_ipconfig_unref(service->ipconfig_ipv6);
3422 service->ipconfig_ipv6 = NULL;
3425 g_strfreev(service->nameservers);
3426 g_strfreev(service->nameservers_config);
3427 g_strfreev(service->domains);
3428 g_strfreev(service->proxies);
3429 g_strfreev(service->excludes);
3431 g_free(service->domainname);
3432 g_free(service->pac);
3433 g_free(service->name);
3434 g_free(service->passphrase);
3435 g_free(service->agent_passphrase);
3436 g_free(service->identifier);
3437 g_free(service->eap);
3438 g_free(service->identity);
3439 g_free(service->agent_identity);
3440 g_free(service->ca_cert_file);
3441 g_free(service->client_cert_file);
3442 g_free(service->private_key_file);
3443 g_free(service->private_key_passphrase);
3444 g_free(service->phase2);
3446 if (service->stats.timer != NULL)
3447 g_timer_destroy(service->stats.timer);
3448 if (service->stats_roaming.timer != NULL)
3449 g_timer_destroy(service->stats_roaming.timer);
3455 * __connman_service_put:
3456 * @service: service structure
3458 * Release service if no longer needed
3460 void __connman_service_put(struct connman_service *service)
3462 DBG("service %p", service);
3464 if (g_atomic_int_dec_and_test(&service->refcount) == TRUE) {
3465 GSequenceIter *iter;
3467 iter = g_hash_table_lookup(service_hash, service->identifier);
3469 reply_pending(service, ECONNABORTED);
3471 __connman_service_disconnect(service);
3473 g_sequence_remove(iter);
3475 service_free(service);
3479 static void stats_init(struct connman_service *service)
3482 service->stats.valid = FALSE;
3483 service->stats.enabled = FALSE;
3484 service->stats.timer = g_timer_new();
3487 service->stats_roaming.valid = FALSE;
3488 service->stats_roaming.enabled = FALSE;
3489 service->stats_roaming.timer = g_timer_new();
3492 static void service_initialize(struct connman_service *service)
3494 DBG("service %p", service);
3496 service->refcount = 1;
3497 service->session_usage_count = 0;
3499 service->network_created = FALSE;
3501 service->type = CONNMAN_SERVICE_TYPE_UNKNOWN;
3502 service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
3504 service->state = CONNMAN_SERVICE_STATE_UNKNOWN;
3505 service->state_ipv4 = CONNMAN_SERVICE_STATE_UNKNOWN;
3506 service->state_ipv6 = CONNMAN_SERVICE_STATE_UNKNOWN;
3508 service->favorite = FALSE;
3509 service->immutable = FALSE;
3510 service->hidden = FALSE;
3512 service->ignore = FALSE;
3514 service->userconnect = FALSE;
3518 stats_init(service);
3520 service->provider = NULL;
3522 service->wps = FALSE;
3526 * connman_service_create:
3528 * Allocate a new service.
3530 * Returns: a newly-allocated #connman_service structure
3532 struct connman_service *connman_service_create(void)
3535 struct connman_stats_counter *counters;
3536 const char *counter;
3538 struct connman_service *service;
3540 service = g_try_new0(struct connman_service, 1);
3541 if (service == NULL)
3544 DBG("service %p", service);
3546 service->counter_table = g_hash_table_new_full(g_str_hash,
3547 g_str_equal, NULL, g_free);
3549 for (list = counter_list; list; list = list->next) {
3550 counter = list->data;
3552 counters = g_try_new0(struct connman_stats_counter, 1);
3553 if (counters == NULL) {
3554 g_hash_table_destroy(service->counter_table);
3559 counters->append_all = TRUE;
3561 g_hash_table_replace(service->counter_table, (gpointer)counter,
3565 service_initialize(service);
3571 * connman_service_ref:
3572 * @service: service structure
3574 * Increase reference counter of service
3576 struct connman_service *connman_service_ref(struct connman_service *service)
3580 g_atomic_int_inc(&service->refcount);
3586 * connman_service_unref:
3587 * @service: service structure
3589 * Decrease reference counter of service
3591 void connman_service_unref(struct connman_service *service)
3593 __connman_service_put(service);
3596 static gint service_compare(gconstpointer a, gconstpointer b,
3599 struct connman_service *service_a = (void *) a;
3600 struct connman_service *service_b = (void *) b;
3601 enum connman_service_state state_a, state_b;
3603 state_a = service_a->state;
3604 state_b = service_b->state;
3606 if (state_a != state_b) {
3607 gboolean a_connected = is_connected(service_a);
3608 gboolean b_connected = is_connected(service_b);
3610 if (a_connected == TRUE && b_connected == TRUE) {
3611 /* We prefer online over ready state */
3612 if (state_a == CONNMAN_SERVICE_STATE_ONLINE)
3615 if (state_b == CONNMAN_SERVICE_STATE_ONLINE)
3619 if (a_connected == TRUE)
3621 if (b_connected == TRUE)
3624 if (is_connecting(service_a) == TRUE)
3626 if (is_connecting(service_b) == TRUE)
3630 if (service_a->order > service_b->order)
3633 if (service_a->order < service_b->order)
3636 if (service_a->favorite == TRUE && service_b->favorite == FALSE)
3639 if (service_a->favorite == FALSE && service_b->favorite == TRUE)
3642 if (service_a->type != service_b->type) {
3643 switch (service_a->type) {
3644 case CONNMAN_SERVICE_TYPE_UNKNOWN:
3645 case CONNMAN_SERVICE_TYPE_SYSTEM:
3646 case CONNMAN_SERVICE_TYPE_ETHERNET:
3647 case CONNMAN_SERVICE_TYPE_GPS:
3648 case CONNMAN_SERVICE_TYPE_VPN:
3649 case CONNMAN_SERVICE_TYPE_GADGET:
3651 case CONNMAN_SERVICE_TYPE_WIFI:
3653 case CONNMAN_SERVICE_TYPE_WIMAX:
3654 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
3655 case CONNMAN_SERVICE_TYPE_CELLULAR:
3660 return (gint) service_b->strength - (gint) service_a->strength;
3664 * connman_service_get_type:
3665 * @service: service structure
3667 * Get the type of service
3669 enum connman_service_type connman_service_get_type(struct connman_service *service)
3671 if (service == NULL)
3672 return CONNMAN_SERVICE_TYPE_UNKNOWN;
3674 return service->type;
3678 * connman_service_get_interface:
3679 * @service: service structure
3681 * Get network interface of service
3683 char *connman_service_get_interface(struct connman_service *service)
3687 if (service == NULL)
3690 if (service->type == CONNMAN_SERVICE_TYPE_VPN) {
3691 if (service->ipconfig_ipv4)
3692 index = connman_ipconfig_get_index(
3693 service->ipconfig_ipv4);
3694 else if (service->ipconfig_ipv6)
3695 index = connman_ipconfig_get_index(
3696 service->ipconfig_ipv6);
3700 return connman_inet_ifname(index);
3703 if (service->network == NULL)
3706 index = connman_network_get_index(service->network);
3708 return connman_inet_ifname(index);
3712 * connman_service_get_network:
3713 * @service: service structure
3715 * Get the service network
3717 struct connman_network *
3718 __connman_service_get_network(struct connman_service *service)
3720 if (service == NULL)
3723 return service->network;
3726 struct connman_ipconfig *
3727 __connman_service_get_ip4config(struct connman_service *service)
3729 if (service == NULL)
3732 return service->ipconfig_ipv4;
3735 struct connman_ipconfig *
3736 __connman_service_get_ip6config(struct connman_service *service)
3738 if (service == NULL)
3741 return service->ipconfig_ipv6;
3744 struct connman_ipconfig *
3745 __connman_service_get_ipconfig(struct connman_service *service, int family)
3747 if (family == AF_INET)
3748 return __connman_service_get_ip4config(service);
3749 else if (family == AF_INET6)
3750 return __connman_service_get_ip6config(service);
3756 enum connman_service_security __connman_service_get_security(struct connman_service *service)
3758 if (service == NULL)
3759 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
3761 return service->security;
3764 const char *__connman_service_get_phase2(struct connman_service *service)
3766 if (service == NULL)
3769 return service->phase2;
3772 connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
3774 if (service == NULL)
3777 return service->wps;
3781 * __connman_service_set_favorite:
3782 * @service: service structure
3783 * @favorite: favorite value
3785 * Change the favorite setting of service
3787 int __connman_service_set_favorite(struct connman_service *service,
3788 connman_bool_t favorite)
3790 GSequenceIter *iter;
3792 iter = g_hash_table_lookup(service_hash, service->identifier);
3796 if (service->favorite == favorite)
3799 service->favorite = favorite;
3800 service->order = __connman_service_get_order(service);
3802 favorite_changed(service);
3804 g_sequence_sort_changed(iter, service_compare, NULL);
3806 services_changed(FALSE);
3811 int __connman_service_set_immutable(struct connman_service *service,
3812 connman_bool_t immutable)
3814 service->immutable = immutable;
3816 immutable_changed(service);
3821 void __connman_service_set_string(struct connman_service *service,
3822 const char *key, const char *value)
3824 if (g_str_equal(key, "EAP") == TRUE) {
3825 g_free(service->eap);
3826 service->eap = g_strdup(value);
3827 } else if (g_str_equal(key, "Identity") == TRUE) {
3828 g_free(service->identity);
3829 service->identity = g_strdup(value);
3830 } else if (g_str_equal(key, "CACertFile") == TRUE) {
3831 g_free(service->ca_cert_file);
3832 service->ca_cert_file = g_strdup(value);
3833 } else if (g_str_equal(key, "ClientCertFile") == TRUE) {
3834 g_free(service->client_cert_file);
3835 service->client_cert_file = g_strdup(value);
3836 } else if (g_str_equal(key, "PrivateKeyFile") == TRUE) {
3837 g_free(service->private_key_file);
3838 service->private_key_file = g_strdup(value);
3839 } else if (g_str_equal(key, "PrivateKeyPassphrase") == TRUE) {
3840 g_free(service->private_key_passphrase);
3841 service->private_key_passphrase = g_strdup(value);
3842 } else if (g_str_equal(key, "Phase2") == TRUE) {
3843 g_free(service->phase2);
3844 service->phase2 = g_strdup(value);
3845 } else if (g_str_equal(key, "Passphrase") == TRUE) {
3846 g_free(service->passphrase);
3847 service->passphrase = g_strdup(value);
3851 static void service_complete(struct connman_service *service)
3853 reply_pending(service, EIO);
3855 if (service->userconnect == FALSE)
3856 __connman_service_auto_connect();
3858 g_get_current_time(&service->modified);
3859 service_save(service);
3862 static void report_error_cb(struct connman_service *service,
3863 gboolean retry, void *user_data)
3866 __connman_service_connect(service);
3868 service_complete(service);
3869 services_changed(FALSE);
3870 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3874 static void request_input_cb (struct connman_service *service,
3875 const char *identity, const char *passphrase,
3878 DBG ("RequestInput return, %p", service);
3880 if (identity == NULL && passphrase == NULL && service->wps == FALSE) {
3881 service_complete(service);
3882 services_changed(FALSE);
3883 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3887 if (identity != NULL)
3888 __connman_service_set_agent_identity(service, identity);
3890 if (passphrase != NULL) {
3891 switch (service->security) {
3892 case CONNMAN_SERVICE_SECURITY_WEP:
3893 case CONNMAN_SERVICE_SECURITY_PSK:
3894 __connman_service_set_passphrase(service, passphrase);
3896 case CONNMAN_SERVICE_SECURITY_8021X:
3897 __connman_service_set_agent_passphrase(service,
3900 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
3901 case CONNMAN_SERVICE_SECURITY_NONE:
3902 case CONNMAN_SERVICE_SECURITY_WPA:
3903 case CONNMAN_SERVICE_SECURITY_RSN:
3904 DBG("service security '%s' not handled",
3905 security2string(service->security));
3910 __connman_service_connect(service);
3912 /* Never cache agent provided credentials */
3913 __connman_service_set_agent_identity(service, NULL);
3914 __connman_service_set_agent_passphrase(service, NULL);
3917 static int service_indicate_state(struct connman_service *service)
3919 enum connman_service_state old_state, new_state;
3920 GSequenceIter *iter;
3922 if (service == NULL)
3925 old_state = service->state;
3926 new_state = combine_state(service->state_ipv4, service->state_ipv6);
3928 DBG("service %p old %s - new %s/%s => %s",
3930 state2string(old_state),
3931 state2string(service->state_ipv4),
3932 state2string(service->state_ipv6),
3933 state2string(new_state));
3935 if (old_state == new_state)
3938 service->state = new_state;
3939 state_changed(service);
3941 if (new_state == CONNMAN_SERVICE_STATE_IDLE &&
3942 old_state != CONNMAN_SERVICE_STATE_DISCONNECT) {
3943 reply_pending(service, ECONNABORTED);
3945 __connman_service_disconnect(service);
3948 if (new_state == CONNMAN_SERVICE_STATE_CONFIGURATION) {
3949 if (__connman_stats_service_register(service) == 0) {
3950 __connman_stats_get(service, FALSE,
3951 &service->stats.data);
3952 __connman_stats_get(service, TRUE,
3953 &service->stats_roaming.data);
3957 if (new_state == CONNMAN_SERVICE_STATE_ONLINE) {
3958 if (service->login_required == TRUE) {
3959 service->login_required = FALSE;
3960 login_changed(service);
3963 connman_timeserver_sync();
3966 if (new_state == CONNMAN_SERVICE_STATE_IDLE) {
3967 connman_bool_t reconnect;
3969 reconnect = get_reconnect_state(service);
3970 if (reconnect == TRUE)
3971 __connman_service_auto_connect();
3973 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
3976 if (new_state == CONNMAN_SERVICE_STATE_READY) {
3977 enum connman_ipconfig_method method;
3979 set_reconnect_state(service, TRUE);
3981 __connman_service_set_favorite(service, TRUE);
3983 reply_pending(service, 0);
3985 service->userconnect = FALSE;
3987 g_get_current_time(&service->modified);
3988 service_save(service);
3990 update_nameservers(service);
3991 dns_changed(service);
3992 domain_changed(service);
3994 __connman_notifier_connect(service->type);
3996 if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
3997 connman_network_get_bool(service->network,
3998 "WiFi.UseWPS") == TRUE) {
4001 pass = connman_network_get_string(service->network,
4004 __connman_service_set_passphrase(service, pass);
4006 connman_network_set_bool(service->network,
4007 "WiFi.UseWPS", FALSE);
4012 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4013 if (method == CONNMAN_IPCONFIG_METHOD_OFF)
4014 __connman_ipconfig_disable_ipv6(
4015 service->ipconfig_ipv6);
4017 } else if (new_state == CONNMAN_SERVICE_STATE_DISCONNECT) {
4018 struct connman_service *def_service = get_default();
4020 if (__connman_notifier_count_connected() == 0 &&
4021 def_service != NULL &&
4022 def_service->provider != NULL)
4023 __connman_provider_disconnect(def_service->provider);
4027 __connman_wispr_stop(service);
4029 __connman_wpad_stop(service);
4031 update_nameservers(service);
4032 dns_changed(service);
4033 domain_changed(service);
4035 __connman_notifier_disconnect(service->type);
4038 if (new_state == CONNMAN_SERVICE_STATE_FAILURE) {
4039 if (service->userconnect == TRUE &&
4040 __connman_agent_report_error(service,
4041 error2string(service->error),
4042 report_error_cb, NULL) == -EIO)
4044 service_complete(service);
4046 __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
4048 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
4050 iter = g_hash_table_lookup(service_hash, service->identifier);
4052 g_sequence_sort_changed(iter, service_compare, NULL);
4054 services_changed(FALSE);
4056 if (new_state == CONNMAN_SERVICE_STATE_ONLINE)
4062 int __connman_service_indicate_error(struct connman_service *service,
4063 enum connman_service_error error)
4065 DBG("service %p error %d", service, error);
4067 if (service == NULL)
4070 service->error = error;
4072 if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
4073 __connman_service_set_passphrase(service, NULL);
4075 __connman_service_ipconfig_indicate_state(service,
4076 CONNMAN_SERVICE_STATE_FAILURE,
4077 CONNMAN_IPCONFIG_TYPE_IPV4);
4078 __connman_service_ipconfig_indicate_state(service,
4079 CONNMAN_SERVICE_STATE_FAILURE,
4080 CONNMAN_IPCONFIG_TYPE_IPV6);
4084 int __connman_service_clear_error(struct connman_service *service)
4086 DBG("service %p", service);
4088 if (service == NULL)
4091 if (service->state != CONNMAN_SERVICE_STATE_FAILURE)
4094 service->state_ipv4 = service->state_ipv6 =
4095 CONNMAN_SERVICE_STATE_UNKNOWN;
4096 service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
4098 if (service->favorite == TRUE)
4099 set_reconnect_state(service, TRUE);
4101 __connman_service_ipconfig_indicate_state(service,
4102 CONNMAN_SERVICE_STATE_IDLE,
4103 CONNMAN_IPCONFIG_TYPE_IPV6);
4106 * Toggling the IPv6 state to IDLE could trigger the auto connect
4107 * machinery and consequently the IPv4 state.
4109 if (service->state_ipv4 != CONNMAN_SERVICE_STATE_UNKNOWN &&
4110 service->state_ipv4 != CONNMAN_SERVICE_STATE_FAILURE)
4113 return __connman_service_ipconfig_indicate_state(service,
4114 CONNMAN_SERVICE_STATE_IDLE,
4115 CONNMAN_IPCONFIG_TYPE_IPV4);
4118 int __connman_service_indicate_default(struct connman_service *service)
4120 DBG("service %p", service);
4127 enum connman_service_state __connman_service_ipconfig_get_state(
4128 struct connman_service *service,
4129 enum connman_ipconfig_type type)
4131 if (service == NULL)
4132 return CONNMAN_SERVICE_STATE_UNKNOWN;
4134 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4135 return service->state_ipv4;
4137 if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4138 return service->state_ipv6;
4140 return CONNMAN_SERVICE_STATE_UNKNOWN;
4143 static void check_proxy_setup(struct connman_service *service)
4146 * We start WPAD if we haven't got a PAC URL from DHCP and
4147 * if our proxy manual configuration is either empty or set
4148 * to AUTO with an empty URL.
4151 if (service->proxy != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN)
4154 if (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN &&
4155 (service->proxy_config != CONNMAN_SERVICE_PROXY_METHOD_AUTO ||
4156 service->pac != NULL))
4159 if (__connman_wpad_start(service) < 0) {
4160 service->proxy = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
4161 __connman_notifier_proxy_changed(service);
4168 __connman_wispr_start(service, CONNMAN_IPCONFIG_TYPE_IPV4);
4171 int __connman_service_ipconfig_indicate_state(struct connman_service *service,
4172 enum connman_service_state new_state,
4173 enum connman_ipconfig_type type)
4175 struct connman_ipconfig *ipconfig = NULL;
4176 enum connman_service_state old_state;
4179 if (service == NULL)
4182 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4183 old_state = service->state_ipv4;
4184 ipconfig = service->ipconfig_ipv4;
4185 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4186 old_state = service->state_ipv6;
4187 ipconfig = service->ipconfig_ipv6;
4190 if (ipconfig == NULL)
4194 if (old_state == new_state)
4197 DBG("service %p (%s) state %d (%s) type %d (%s)",
4198 service, service ? service->identifier : NULL,
4199 new_state, state2string(new_state),
4200 type, __connman_ipconfig_type2string(type));
4202 switch (new_state) {
4203 case CONNMAN_SERVICE_STATE_UNKNOWN:
4204 case CONNMAN_SERVICE_STATE_IDLE:
4205 if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
4208 case CONNMAN_SERVICE_STATE_ASSOCIATION:
4210 case CONNMAN_SERVICE_STATE_CONFIGURATION:
4211 __connman_ipconfig_enable(ipconfig);
4213 case CONNMAN_SERVICE_STATE_READY:
4214 update_nameservers(service);
4216 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4217 check_proxy_setup(service);
4219 __connman_wispr_start(service, type);
4221 case CONNMAN_SERVICE_STATE_ONLINE:
4223 case CONNMAN_SERVICE_STATE_DISCONNECT:
4224 if (service->state == CONNMAN_SERVICE_STATE_IDLE)
4227 case CONNMAN_SERVICE_STATE_FAILURE:
4231 /* We keep that state */
4232 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
4233 service->state_ipv4 = new_state;
4234 else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
4235 service->state_ipv6 = new_state;
4237 ret = service_indicate_state(service);
4240 * If the ipconfig method is OFF, then we set the state to IDLE
4241 * so that it will not affect the combined state in the future.
4243 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
4244 enum connman_ipconfig_method method;
4245 method = __connman_ipconfig_get_method(service->ipconfig_ipv4);
4246 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4247 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4248 service->state_ipv4 = CONNMAN_SERVICE_STATE_IDLE;
4249 ret = service_indicate_state(service);
4252 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
4253 enum connman_ipconfig_method method;
4254 method = __connman_ipconfig_get_method(service->ipconfig_ipv6);
4255 if (method == CONNMAN_IPCONFIG_METHOD_OFF ||
4256 method == CONNMAN_IPCONFIG_METHOD_UNKNOWN) {
4257 service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
4258 ret = service_indicate_state(service);
4265 int __connman_service_request_login(struct connman_service *service)
4267 DBG("service %p", service);
4269 if (service == NULL)
4272 service->login_required = TRUE;
4273 login_changed(service);
4278 static connman_bool_t prepare_network(struct connman_service *service)
4280 enum connman_network_type type;
4281 unsigned int ssid_len;
4283 type = connman_network_get_type(service->network);
4286 case CONNMAN_NETWORK_TYPE_UNKNOWN:
4287 case CONNMAN_NETWORK_TYPE_VENDOR:
4289 case CONNMAN_NETWORK_TYPE_WIFI:
4290 if (connman_network_get_blob(service->network, "WiFi.SSID",
4294 if (service->passphrase != NULL)
4295 connman_network_set_string(service->network,
4296 "WiFi.Passphrase", service->passphrase);
4298 case CONNMAN_NETWORK_TYPE_ETHERNET:
4299 case CONNMAN_NETWORK_TYPE_WIMAX:
4300 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
4301 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
4302 case CONNMAN_NETWORK_TYPE_CELLULAR:
4309 static void prepare_8021x(struct connman_service *service)
4311 if (service->eap != NULL)
4312 connman_network_set_string(service->network, "WiFi.EAP",
4315 if (service->identity != NULL)
4316 connman_network_set_string(service->network, "WiFi.Identity",
4319 if (service->ca_cert_file != NULL)
4320 connman_network_set_string(service->network, "WiFi.CACertFile",
4321 service->ca_cert_file);
4323 if (service->client_cert_file != NULL)
4324 connman_network_set_string(service->network,
4325 "WiFi.ClientCertFile",
4326 service->client_cert_file);
4328 if (service->private_key_file != NULL)
4329 connman_network_set_string(service->network,
4330 "WiFi.PrivateKeyFile",
4331 service->private_key_file);
4333 if (service->private_key_passphrase != NULL)
4334 connman_network_set_string(service->network,
4335 "WiFi.PrivateKeyPassphrase",
4336 service->private_key_passphrase);
4338 if (service->phase2 != NULL)
4339 connman_network_set_string(service->network, "WiFi.Phase2",
4343 static int service_connect(struct connman_service *service)
4347 switch (service->type) {
4348 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4349 case CONNMAN_SERVICE_TYPE_SYSTEM:
4350 case CONNMAN_SERVICE_TYPE_GPS:
4351 case CONNMAN_SERVICE_TYPE_GADGET:
4353 case CONNMAN_SERVICE_TYPE_ETHERNET:
4354 case CONNMAN_SERVICE_TYPE_WIMAX:
4355 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
4356 case CONNMAN_SERVICE_TYPE_CELLULAR:
4357 case CONNMAN_SERVICE_TYPE_VPN:
4359 case CONNMAN_SERVICE_TYPE_WIFI:
4360 switch (service->security) {
4361 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4362 case CONNMAN_SERVICE_SECURITY_NONE:
4364 case CONNMAN_SERVICE_SECURITY_WEP:
4365 case CONNMAN_SERVICE_SECURITY_PSK:
4366 case CONNMAN_SERVICE_SECURITY_WPA:
4367 case CONNMAN_SERVICE_SECURITY_RSN:
4368 if (service->passphrase == NULL) {
4369 if (service->network == NULL)
4372 if (service->wps == FALSE ||
4373 connman_network_get_bool(
4375 "WiFi.UseWPS") == FALSE)
4379 case CONNMAN_SERVICE_SECURITY_8021X:
4380 if (service->eap == NULL)
4384 * never request credentials if using EAP-TLS
4385 * (EAP-TLS networks need to be fully provisioned)
4387 if (g_str_equal(service->eap, "tls") == TRUE)
4391 * Return -ENOKEY if either identity or passphrase is
4392 * missing. Agent provided credentials can be used as
4393 * fallback if needed.
4395 if ((service->identity == NULL &&
4396 service->agent_identity == NULL) ||
4397 (service->passphrase == NULL &&
4398 service->agent_passphrase == NULL))
4406 if (service->network != NULL) {
4407 if (prepare_network(service) == FALSE)
4410 switch (service->security) {
4411 case CONNMAN_SERVICE_SECURITY_UNKNOWN:
4412 case CONNMAN_SERVICE_SECURITY_NONE:
4413 case CONNMAN_SERVICE_SECURITY_WEP:
4414 case CONNMAN_SERVICE_SECURITY_PSK:
4415 case CONNMAN_SERVICE_SECURITY_WPA:
4416 case CONNMAN_SERVICE_SECURITY_RSN:
4418 case CONNMAN_SERVICE_SECURITY_8021X:
4419 prepare_8021x(service);
4423 if (__connman_stats_service_register(service) == 0) {
4424 __connman_stats_get(service, FALSE,
4425 &service->stats.data);
4426 __connman_stats_get(service, TRUE,
4427 &service->stats_roaming.data);
4430 if (service->ipconfig_ipv4)
4431 __connman_ipconfig_enable(service->ipconfig_ipv4);
4432 if (service->ipconfig_ipv6)
4433 __connman_ipconfig_enable(service->ipconfig_ipv6);
4435 err = __connman_network_connect(service->network);
4436 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4437 service->provider != NULL)
4438 err = __connman_provider_connect(service->provider);
4443 if (err != -EINPROGRESS) {
4444 __connman_ipconfig_disable(service->ipconfig_ipv4);
4445 __connman_ipconfig_disable(service->ipconfig_ipv6);
4446 __connman_stats_service_unregister(service);
4454 int __connman_service_connect(struct connman_service *service)
4458 DBG("service %p state %s", service, state2string(service->state));
4460 if (is_connected(service) == TRUE)
4463 if (is_connecting(service) == TRUE)
4466 switch (service->type) {
4467 case CONNMAN_SERVICE_TYPE_UNKNOWN:
4468 case CONNMAN_SERVICE_TYPE_SYSTEM:
4469 case CONNMAN_SERVICE_TYPE_GPS:
4470 case CONNMAN_SERVICE_TYPE_GADGET:
4473 err = service_connect(service);
4479 if (err == -EINPROGRESS) {
4480 if (service->timeout == 0)
4481 service->timeout = g_timeout_add_seconds(
4482 CONNECT_TIMEOUT, connect_timeout, service);
4484 return -EINPROGRESS;
4487 __connman_service_ipconfig_indicate_state(service,
4488 CONNMAN_SERVICE_STATE_FAILURE,
4489 CONNMAN_IPCONFIG_TYPE_IPV4);
4490 __connman_service_ipconfig_indicate_state(service,
4491 CONNMAN_SERVICE_STATE_FAILURE,
4492 CONNMAN_IPCONFIG_TYPE_IPV6);
4494 if (service->network != NULL)
4495 __connman_network_disconnect(service->network);
4496 else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4497 service->provider != NULL)
4498 __connman_provider_disconnect(service->provider);
4500 if (service->userconnect == TRUE) {
4501 if (err == -ENOKEY) {
4502 if (__connman_agent_request_passphrase_input(service,
4505 return -EINPROGRESS;
4507 reply_pending(service, err);
4513 int __connman_service_disconnect(struct connman_service *service)
4517 DBG("service %p", service);
4519 if (service->network != NULL) {
4520 err = __connman_network_disconnect(service->network);
4521 } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
4522 service->provider != NULL)
4523 err = __connman_provider_disconnect(service->provider);
4527 if (err < 0 && err != -EINPROGRESS)
4530 __connman_6to4_remove(service->ipconfig_ipv4);
4532 if (service->ipconfig_ipv4)
4533 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv4,
4536 __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
4539 __connman_ipconfig_address_remove(service->ipconfig_ipv4);
4540 __connman_ipconfig_address_remove(service->ipconfig_ipv6);
4542 __connman_ipconfig_disable(service->ipconfig_ipv4);
4543 __connman_ipconfig_disable(service->ipconfig_ipv6);
4545 __connman_stats_service_unregister(service);
4550 int __connman_service_disconnect_all(void)
4552 GSequenceIter *iter;
4553 GSList *services = NULL, *list;
4557 iter = g_sequence_get_begin_iter(service_list);
4559 while (g_sequence_iter_is_end(iter) == FALSE) {
4560 struct connman_service *service = g_sequence_get(iter);
4562 services = g_slist_prepend(services, service);
4564 iter = g_sequence_iter_next(iter);
4567 for (list = services; list != NULL; list = list->next) {
4568 struct connman_service *service = list->data;
4570 service->ignore = TRUE;
4572 set_reconnect_state(service, FALSE);
4574 __connman_service_disconnect(service);
4584 * __connman_service_lookup:
4585 * @pattern: search pattern
4586 * @path: return object path
4588 * Look up a service path from a search pattern
4590 int __connman_service_lookup(const char *pattern, const char **path)
4592 GHashTableIter iter;
4593 gpointer key, value;
4594 struct connman_device *device;
4597 g_hash_table_iter_init(&iter, service_hash);
4599 while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
4600 GSequenceIter *iter = value;
4601 struct connman_service *service = g_sequence_get(iter);
4603 if (g_strcmp0(service->identifier, pattern) == 0 ||
4604 g_strcmp0(service->name, pattern) == 0) {
4605 *path = (const char *) service->path;
4609 if (service->network == NULL)
4612 device = connman_network_get_device(service->network);
4616 ifname = connman_device_get_string(device, "Interface");
4617 if (ifname != NULL && g_strcmp0(ifname, pattern) == 0) {
4618 *path = (const char *) service->path;
4628 * lookup_by_identifier:
4629 * @identifier: service identifier
4631 * Look up a service by identifier (reference count will not be increased)
4633 static struct connman_service *lookup_by_identifier(const char *identifier)
4635 GSequenceIter *iter;
4637 iter = g_hash_table_lookup(service_hash, identifier);
4639 return g_sequence_get(iter);
4644 static struct connman_network *create_hidden_wifi(struct connman_device *device,
4645 const char *ssid, const char *mode, const char *security,
4648 struct connman_network *network;
4651 unsigned int i, ssid_len;
4653 ssid_len = strlen(ssid);
4657 network = connman_network_create(group, CONNMAN_NETWORK_TYPE_WIFI);
4658 if (network == NULL)
4661 connman_network_set_blob(network, "WiFi.SSID",
4662 (unsigned char *) ssid, ssid_len);
4664 connman_network_set_string(network, "WiFi.Mode", mode);
4665 connman_network_set_string(network, "WiFi.Security", security);
4667 name = g_try_malloc0(ssid_len + 1);
4669 connman_network_unref(network);
4673 for (i = 0; i < ssid_len; i++) {
4674 if (g_ascii_isprint(ssid[i]))
4680 connman_network_set_name(network, name);
4684 index = connman_device_get_index(device);
4685 connman_network_set_index(network, index);
4687 if (connman_device_add_network(device, network) < 0) {
4688 connman_network_unref(network);
4692 connman_network_set_available(network, TRUE);
4697 int __connman_service_create_and_connect(DBusMessage *msg)
4699 struct connman_service *service;
4700 struct connman_network *network;
4701 struct connman_device *device;
4702 DBusMessageIter iter, array;
4703 const char *mode = "managed", *security = "none", *group_security;
4704 const char *type = NULL, *ssid = NULL, *passphrase = NULL;
4705 unsigned int ssid_len = 0;
4710 dbus_message_iter_init(msg, &iter);
4711 dbus_message_iter_recurse(&iter, &array);
4713 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
4714 DBusMessageIter entry, value;
4717 dbus_message_iter_recurse(&array, &entry);
4718 dbus_message_iter_get_basic(&entry, &key);
4720 dbus_message_iter_next(&entry);
4721 dbus_message_iter_recurse(&entry, &value);
4723 switch (dbus_message_iter_get_arg_type(&value)) {
4724 case DBUS_TYPE_STRING:
4725 if (g_str_equal(key, "Type") == TRUE)
4726 dbus_message_iter_get_basic(&value, &type);
4727 else if (g_str_equal(key, "WiFi.Mode") == TRUE ||
4728 g_str_equal(key, "Mode") == TRUE)
4729 dbus_message_iter_get_basic(&value, &mode);
4730 else if (g_str_equal(key, "WiFi.Security") == TRUE ||
4731 g_str_equal(key, "Security") == TRUE)
4732 dbus_message_iter_get_basic(&value, &security);
4733 else if (g_str_equal(key, "WiFi.Passphrase") == TRUE ||
4734 g_str_equal(key, "Passphrase") == TRUE)
4735 dbus_message_iter_get_basic(&value, &passphrase);
4736 else if (g_str_equal(key, "WiFi.SSID") == TRUE ||
4737 g_str_equal(key, "SSID") == TRUE)
4738 dbus_message_iter_get_basic(&value, &ssid);
4741 dbus_message_iter_next(&array);
4747 if (g_strcmp0(type, "wifi") != 0 || g_strcmp0(mode, "managed") != 0)
4753 ssid_len = strlen(ssid);
4757 if (g_strcmp0(security, "none") != 0 &&
4758 g_strcmp0(security, "wep") != 0 &&
4759 g_strcmp0(security, "psk") != 0 &&
4760 g_strcmp0(security, "wpa") != 0 &&
4761 g_strcmp0(security, "rsn") != 0 &&
4762 g_strcmp0(security, "ieee8021x") != 0)
4765 device = __connman_device_find_device(CONNMAN_SERVICE_TYPE_WIFI);
4769 ident = connman_device_get_ident(device);
4774 if (!g_strcmp0(security, "wpa") ||
4775 !g_strcmp0(security, "rsn"))
4776 group_security = "psk";
4778 group_security = security;
4780 group = wifi_build_group_name((unsigned char *) ssid,
4781 ssid_len, mode, group_security);
4785 name = g_strdup_printf("%s_%s_%s", type, ident, group);
4787 service = lookup_by_identifier(name);
4789 if (service != NULL)
4792 network = create_hidden_wifi(device, ssid, mode, security, group);
4793 if (network != NULL)
4794 connman_network_set_group(network, group);
4796 service = lookup_by_identifier(name);
4802 if (service == NULL) {
4807 service->network_created = TRUE;
4809 if (is_connected(service) == TRUE) {
4814 if (is_connecting(service) == TRUE) {
4819 set_reconnect_state(service, FALSE);
4821 __connman_device_disconnect(device);
4823 if (passphrase != NULL) {
4824 g_free(service->passphrase);
4825 service->passphrase = g_strdup(passphrase);
4828 service->userconnect = TRUE;
4830 err = __connman_service_connect(service);
4831 if (err < 0 && err != -EINPROGRESS)
4834 service->pending = dbus_message_ref(msg);
4839 if (service != NULL && service->network_created == TRUE) {
4840 struct connman_network *network = service->network;
4842 if (network != NULL) {
4843 connman_network_set_available(network, FALSE);
4844 __connman_device_cleanup_networks(device);
4846 __connman_service_put(service);
4852 static void provision_changed(gpointer value, gpointer user_data)
4854 struct connman_service *service = value;
4855 char *path = user_data;
4857 __connman_config_provision_service_ident(service, path);
4860 void __connman_service_provision_changed(const char *ident)
4862 g_sequence_foreach(service_list, provision_changed, (void *)ident);
4865 int __connman_service_provision(DBusMessage *msg)
4867 GKeyFile *keyfile = NULL;
4868 const char *config_str = NULL;
4869 char *group = NULL, *ident = NULL;
4871 struct connman_service *service;
4875 dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &config_str,
4878 if (config_str == NULL || strlen(config_str) == 0)
4881 keyfile = g_key_file_new();
4883 /* populate GKeyFile with config_str */
4884 if (g_key_file_load_from_data(keyfile, config_str,
4885 strlen(config_str), 0, NULL) == FALSE) {
4891 * read only one group of settings (only one service supported, no
4894 group = g_key_file_get_start_group(keyfile);
4896 if (group == NULL || g_str_has_prefix(group, "service_") == FALSE) {
4901 err = __connman_config_load_service(keyfile, group, TRUE);
4905 ident = group + strlen("service_");
4907 /* trigger service provisioning if service exists */
4908 service = lookup_by_identifier(ident);
4909 if (service != NULL)
4910 __connman_config_provision_service(service);
4912 g_dbus_send_reply(connection, msg, DBUS_TYPE_INVALID);
4918 if (keyfile != NULL)
4919 g_key_file_free(keyfile);
4925 * __connman_service_get:
4926 * @identifier: service identifier
4928 * Look up a service by identifier or create a new one if not found
4930 static struct connman_service *service_get(const char *identifier)
4932 struct connman_service *service;
4933 GSequenceIter *iter;
4935 iter = g_hash_table_lookup(service_hash, identifier);
4937 service = g_sequence_get(iter);
4938 if (service != NULL)
4939 connman_service_ref(service);
4943 service = connman_service_create();
4944 if (service == NULL)
4947 DBG("service %p", service);
4949 service->identifier = g_strdup(identifier);
4951 iter = g_sequence_insert_sorted(service_list, service,
4952 service_compare, NULL);
4954 g_hash_table_insert(service_hash, service->identifier, iter);
4959 static int service_register(struct connman_service *service)
4961 GSequenceIter *iter;
4963 DBG("service %p", service);
4965 if (service->path != NULL)
4968 service->path = g_strdup_printf("%s/service/%s", CONNMAN_PATH,
4969 service->identifier);
4971 DBG("path %s", service->path);
4973 __connman_config_provision_service(service);
4975 service_load(service);
4977 g_dbus_register_interface(connection, service->path,
4978 CONNMAN_SERVICE_INTERFACE,
4979 service_methods, service_signals,
4980 NULL, service, NULL);
4982 iter = g_hash_table_lookup(service_hash, service->identifier);
4984 g_sequence_sort_changed(iter, service_compare, NULL);
4986 services_changed(TRUE);
4991 static void service_up(struct connman_ipconfig *ipconfig)
4993 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
4995 DBG("%s up", connman_ipconfig_get_ifname(ipconfig));
4997 link_changed(service);
4999 service->stats.valid = FALSE;
5000 service->stats_roaming.valid = FALSE;
5003 static void service_down(struct connman_ipconfig *ipconfig)
5005 DBG("%s down", connman_ipconfig_get_ifname(ipconfig));
5008 static void service_lower_up(struct connman_ipconfig *ipconfig)
5010 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5012 DBG("%s lower up", connman_ipconfig_get_ifname(ipconfig));
5014 stats_start(service);
5017 static void service_lower_down(struct connman_ipconfig *ipconfig)
5019 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5021 DBG("%s lower down", connman_ipconfig_get_ifname(ipconfig));
5023 stats_stop(service);
5024 service_save(service);
5027 static void service_ip_bound(struct connman_ipconfig *ipconfig)
5029 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5030 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5031 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5033 DBG("%s ip bound", connman_ipconfig_get_ifname(ipconfig));
5035 type = __connman_ipconfig_get_config_type(ipconfig);
5036 method = __connman_ipconfig_get_method(ipconfig);
5038 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5041 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5042 method == CONNMAN_IPCONFIG_METHOD_AUTO)
5043 __connman_service_ipconfig_indicate_state(service,
5044 CONNMAN_SERVICE_STATE_READY,
5045 CONNMAN_IPCONFIG_TYPE_IPV6);
5047 settings_changed(service, ipconfig);
5050 static void service_ip_release(struct connman_ipconfig *ipconfig)
5052 struct connman_service *service = connman_ipconfig_get_data(ipconfig);
5053 enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
5054 enum connman_ipconfig_type type = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
5056 DBG("%s ip release", connman_ipconfig_get_ifname(ipconfig));
5058 type = __connman_ipconfig_get_config_type(ipconfig);
5059 method = __connman_ipconfig_get_method(ipconfig);
5061 DBG("service %p ipconfig %p type %d method %d", service, ipconfig,
5064 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
5065 method == CONNMAN_IPCONFIG_METHOD_OFF)
5066 __connman_service_ipconfig_indicate_state(service,
5067 CONNMAN_SERVICE_STATE_DISCONNECT,
5068 CONNMAN_IPCONFIG_TYPE_IPV6);
5070 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
5071 method == CONNMAN_IPCONFIG_METHOD_OFF)
5072 __connman_service_ipconfig_indicate_state(service,
5073 CONNMAN_SERVICE_STATE_DISCONNECT,
5074 CONNMAN_IPCONFIG_TYPE_IPV4);
5076 settings_changed(service, ipconfig);
5079 static const struct connman_ipconfig_ops service_ops = {
5081 .down = service_down,
5082 .lower_up = service_lower_up,
5083 .lower_down = service_lower_down,
5084 .ip_bound = service_ip_bound,
5085 .ip_release = service_ip_release,
5088 static void setup_ip4config(struct connman_service *service, int index,
5089 enum connman_ipconfig_method method)
5094 service->ipconfig_ipv4 = connman_ipconfig_create(index,
5095 CONNMAN_IPCONFIG_TYPE_IPV4);
5096 if (service->ipconfig_ipv4 == NULL)
5099 connman_ipconfig_set_method(service->ipconfig_ipv4, method);
5101 connman_ipconfig_set_data(service->ipconfig_ipv4, service);
5103 connman_ipconfig_set_ops(service->ipconfig_ipv4, &service_ops);
5106 static void setup_ip6config(struct connman_service *service, int index)
5111 service->ipconfig_ipv6 = connman_ipconfig_create(index,
5112 CONNMAN_IPCONFIG_TYPE_IPV6);
5113 if (service->ipconfig_ipv6 == NULL)
5116 connman_ipconfig_set_data(service->ipconfig_ipv6, service);
5118 connman_ipconfig_set_ops(service->ipconfig_ipv6, &service_ops);
5121 void __connman_service_read_ip4config(struct connman_service *service)
5125 if (service->ipconfig_ipv4 == NULL)
5128 keyfile = connman_storage_load_service(service->identifier);
5129 if (keyfile == NULL)
5132 __connman_ipconfig_load(service->ipconfig_ipv4, keyfile,
5133 service->identifier, "IPv4.");
5135 g_key_file_free(keyfile);
5138 void __connman_service_create_ip4config(struct connman_service *service,
5141 DBG("ipv4 %p", service->ipconfig_ipv4);
5143 if (service->ipconfig_ipv4 != NULL)
5146 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5147 __connman_service_read_ip4config(service);
5150 void __connman_service_read_ip6config(struct connman_service *service)
5154 if (service->ipconfig_ipv6 == NULL)
5157 keyfile = connman_storage_load_service(service->identifier);
5158 if (keyfile == NULL)
5161 __connman_ipconfig_load(service->ipconfig_ipv6, keyfile,
5162 service->identifier, "IPv6.");
5164 g_key_file_free(keyfile);
5167 void __connman_service_create_ip6config(struct connman_service *service,
5170 DBG("ipv6 %p", service->ipconfig_ipv6);
5172 if (service->ipconfig_ipv6 != NULL)
5175 setup_ip6config(service, index);
5177 __connman_service_read_ip6config(service);
5181 * __connman_service_lookup_from_network:
5182 * @network: network structure
5184 * Look up a service by network (reference count will not be increased)
5186 struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
5188 struct connman_service *service;
5189 const char *ident, *group;
5192 DBG("network %p", network);
5194 if (network == NULL)
5197 ident = __connman_network_get_ident(network);
5201 group = connman_network_get_group(network);
5205 name = g_strdup_printf("%s_%s_%s",
5206 __connman_network_get_type(network), ident, group);
5207 service = lookup_by_identifier(name);
5213 struct connman_service *__connman_service_lookup_from_index(int index)
5215 struct connman_service *service;
5216 GSequenceIter *iter;
5218 iter = g_sequence_get_begin_iter(service_list);
5220 while (g_sequence_iter_is_end(iter) == FALSE) {
5221 service = g_sequence_get(iter);
5223 if (connman_ipconfig_get_index(service->ipconfig_ipv4)
5227 if (connman_ipconfig_get_index(service->ipconfig_ipv6)
5231 iter = g_sequence_iter_next(iter);
5237 const char *__connman_service_get_ident(struct connman_service *service)
5239 return service->identifier;
5242 const char *__connman_service_get_path(struct connman_service *service)
5244 return service->path;
5247 unsigned int __connman_service_get_order(struct connman_service *service)
5249 GSequenceIter *iter;
5251 if (service == NULL)
5254 if (service->favorite == FALSE) {
5259 iter = g_hash_table_lookup(service_hash, service->identifier);
5261 if (g_sequence_iter_get_position(iter) == 0)
5263 else if (service->type == CONNMAN_SERVICE_TYPE_VPN)
5264 service->order = 10;
5270 return service->order;
5273 static enum connman_service_type convert_network_type(struct connman_network *network)
5275 enum connman_network_type type = connman_network_get_type(network);
5278 case CONNMAN_NETWORK_TYPE_UNKNOWN:
5279 case CONNMAN_NETWORK_TYPE_VENDOR:
5281 case CONNMAN_NETWORK_TYPE_ETHERNET:
5282 return CONNMAN_SERVICE_TYPE_ETHERNET;
5283 case CONNMAN_NETWORK_TYPE_WIFI:
5284 return CONNMAN_SERVICE_TYPE_WIFI;
5285 case CONNMAN_NETWORK_TYPE_WIMAX:
5286 return CONNMAN_SERVICE_TYPE_WIMAX;
5287 case CONNMAN_NETWORK_TYPE_BLUETOOTH_PAN:
5288 case CONNMAN_NETWORK_TYPE_BLUETOOTH_DUN:
5289 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
5290 case CONNMAN_NETWORK_TYPE_CELLULAR:
5291 return CONNMAN_SERVICE_TYPE_CELLULAR;
5294 return CONNMAN_SERVICE_TYPE_UNKNOWN;
5297 static enum connman_service_security convert_wifi_security(const char *security)
5299 if (security == NULL)
5300 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5301 else if (g_str_equal(security, "none") == TRUE)
5302 return CONNMAN_SERVICE_SECURITY_NONE;
5303 else if (g_str_equal(security, "wep") == TRUE)
5304 return CONNMAN_SERVICE_SECURITY_WEP;
5305 else if (g_str_equal(security, "psk") == TRUE)
5306 return CONNMAN_SERVICE_SECURITY_PSK;
5307 else if (g_str_equal(security, "ieee8021x") == TRUE)
5308 return CONNMAN_SERVICE_SECURITY_8021X;
5309 else if (g_str_equal(security, "wpa") == TRUE)
5310 return CONNMAN_SERVICE_SECURITY_WPA;
5311 else if (g_str_equal(security, "rsn") == TRUE)
5312 return CONNMAN_SERVICE_SECURITY_RSN;
5314 return CONNMAN_SERVICE_SECURITY_UNKNOWN;
5317 static void update_from_network(struct connman_service *service,
5318 struct connman_network *network)
5320 connman_uint8_t strength = service->strength;
5321 GSequenceIter *iter;
5324 DBG("service %p network %p", service, network);
5326 if (is_connected(service) == TRUE)
5329 if (is_connecting(service) == TRUE)
5332 str = connman_network_get_string(network, "Name");
5334 g_free(service->name);
5335 service->name = g_strdup(str);
5336 service->hidden = FALSE;
5338 g_free(service->name);
5339 service->name = NULL;
5340 service->hidden = TRUE;
5343 service->strength = connman_network_get_strength(network);
5344 service->roaming = connman_network_get_bool(network, "Roaming");
5346 if (service->strength == 0) {
5348 * Filter out 0-values; it's unclear what they mean
5349 * and they cause anomalous sorting of the priority list.
5351 service->strength = strength;
5354 str = connman_network_get_string(network, "WiFi.Security");
5355 service->security = convert_wifi_security(str);
5357 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5358 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5360 if (service->strength > strength && service->network != NULL) {
5361 service->network = network;
5363 strength_changed(service);
5366 if (service->network == NULL)
5367 service->network = network;
5369 iter = g_hash_table_lookup(service_hash, service->identifier);
5371 g_sequence_sort_changed(iter, service_compare, NULL);
5375 * __connman_service_create_from_network:
5376 * @network: network structure
5378 * Look up service by network and if not found, create one
5380 struct connman_service * __connman_service_create_from_network(struct connman_network *network)
5382 struct connman_service *service;
5383 struct connman_device *device;
5384 const char *ident, *group;
5388 DBG("network %p", network);
5390 if (network == NULL)
5393 ident = __connman_network_get_ident(network);
5397 group = connman_network_get_group(network);
5401 name = g_strdup_printf("%s_%s_%s",
5402 __connman_network_get_type(network), ident, group);
5403 service = service_get(name);
5406 if (service == NULL)
5409 if (__connman_network_get_weakness(network) == TRUE)
5412 if (service->path != NULL) {
5413 update_from_network(service, network);
5414 services_changed(TRUE);
5418 service->type = convert_network_type(network);
5420 switch (service->type) {
5421 case CONNMAN_SERVICE_TYPE_UNKNOWN:
5422 case CONNMAN_SERVICE_TYPE_SYSTEM:
5423 case CONNMAN_SERVICE_TYPE_ETHERNET:
5424 case CONNMAN_SERVICE_TYPE_WIMAX:
5425 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
5426 case CONNMAN_SERVICE_TYPE_GPS:
5427 case CONNMAN_SERVICE_TYPE_VPN:
5428 case CONNMAN_SERVICE_TYPE_GADGET:
5429 service->autoconnect = FALSE;
5431 case CONNMAN_SERVICE_TYPE_WIFI:
5432 case CONNMAN_SERVICE_TYPE_CELLULAR:
5433 service->autoconnect = TRUE;
5437 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5438 service->state = combine_state(service->state_ipv4, service->state_ipv6);
5440 update_from_network(service, network);
5442 index = connman_network_get_index(network);
5444 if (service->ipconfig_ipv4 == NULL)
5445 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_DHCP);
5447 if (service->ipconfig_ipv6 == NULL)
5448 setup_ip6config(service, index);
5450 service_register(service);
5452 if (service->favorite == TRUE) {
5453 device = connman_network_get_device(service->network);
5454 if (device && __connman_device_scanning(device) == FALSE)
5455 __connman_service_auto_connect();
5458 __connman_notifier_service_add(service, service->name);
5463 void __connman_service_update_from_network(struct connman_network *network)
5465 struct connman_service *service;
5466 connman_uint8_t strength;
5467 connman_bool_t roaming;
5468 GSequenceIter *iter;
5470 connman_bool_t stats_enable;
5472 DBG("network %p", network);
5474 service = __connman_service_lookup_from_network(network);
5475 if (service == NULL)
5478 if (service->network == NULL)
5481 name = connman_network_get_string(service->network, "Name");
5482 if (g_strcmp0(service->name, name) != 0) {
5483 g_free(service->name);
5484 service->name = g_strdup(name);
5485 connman_dbus_property_changed_basic(service->path,
5486 CONNMAN_SERVICE_INTERFACE, "Name",
5487 DBUS_TYPE_STRING, &service->name);
5490 if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
5491 service->wps = connman_network_get_bool(network, "WiFi.WPS");
5493 strength = connman_network_get_strength(service->network);
5494 if (strength == service->strength)
5497 service->strength = strength;
5499 strength_changed(service);
5502 roaming = connman_network_get_bool(service->network, "Roaming");
5503 if (roaming == service->roaming)
5506 stats_enable = stats_enabled(service);
5507 if (stats_enable == TRUE)
5508 stats_stop(service);
5510 service->roaming = roaming;
5512 if (stats_enable == TRUE)
5513 stats_start(service);
5515 roaming_changed(service);
5517 iter = g_hash_table_lookup(service_hash, service->identifier);
5519 g_sequence_sort_changed(iter, service_compare, NULL);
5522 void __connman_service_remove_from_network(struct connman_network *network)
5524 struct connman_service *service;
5526 DBG("network %p", network);
5528 service = __connman_service_lookup_from_network(network);
5529 if (service == NULL)
5532 __connman_service_put(service);
5536 * __connman_service_create_from_provider:
5537 * @provider: provider structure
5539 * Look up service by provider and if not found, create one
5541 struct connman_service *
5542 __connman_service_create_from_provider(struct connman_provider *provider)
5544 struct connman_service *service;
5545 const char *ident, *str;
5547 int index = connman_provider_get_index(provider);
5549 DBG("provider %p", provider);
5551 ident = __connman_provider_get_ident(provider);
5555 name = g_strdup_printf("vpn_%s", ident);
5556 service = service_get(name);
5559 if (service == NULL)
5562 service->type = CONNMAN_SERVICE_TYPE_VPN;
5563 service->provider = connman_provider_ref(provider);
5564 service->autoconnect = FALSE;
5565 service->userconnect = TRUE;
5567 service->state_ipv4 = service->state_ipv6 = CONNMAN_SERVICE_STATE_IDLE;
5569 str = connman_provider_get_string(provider, "Name");
5571 g_free(service->name);
5572 service->name = g_strdup(str);
5573 service->hidden = FALSE;
5575 g_free(service->name);
5576 service->name = NULL;
5577 service->hidden = TRUE;
5580 service->strength = 0;
5582 if (service->ipconfig_ipv4 == NULL)
5583 setup_ip4config(service, index, CONNMAN_IPCONFIG_METHOD_MANUAL);
5585 if (service->ipconfig_ipv6 == NULL)
5586 setup_ip6config(service, index);
5588 service_register(service);
5590 __connman_notifier_service_add(service, service->name);
5595 void __connman_service_downgrade_state(struct connman_service *service)
5597 if (service == NULL)
5600 DBG("service %p state4 %d state6 %d", service, service->state_ipv4,
5601 service->state_ipv6);
5603 if (service->state_ipv4 == CONNMAN_SERVICE_STATE_ONLINE)
5604 __connman_service_ipconfig_indicate_state(service,
5605 CONNMAN_SERVICE_STATE_READY,
5606 CONNMAN_IPCONFIG_TYPE_IPV4);
5608 if (service->state_ipv6 == CONNMAN_SERVICE_STATE_ONLINE)
5609 __connman_service_ipconfig_indicate_state(service,
5610 CONNMAN_SERVICE_STATE_READY,
5611 CONNMAN_IPCONFIG_TYPE_IPV6);
5614 int __connman_service_init(void)
5618 connection = connman_dbus_get_connection();
5620 service_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
5623 service_list = g_sequence_new(service_free);
5628 void __connman_service_cleanup(void)
5634 list = service_list;
5635 service_list = NULL;
5636 g_sequence_free(list);
5638 g_hash_table_destroy(service_hash);
5639 service_hash = NULL;
5641 g_slist_free(counter_list);
5642 counter_list = NULL;
5644 dbus_connection_unref(connection);